import React from 'react';
import { StageProps } from 'react-konva';

export const SCALE_FACTOR = 1.1;
export const MIN_SCALE = 1 / 5;
export const MAX_SCALE = 5;

export function useScaleStage(initScale: StageProps['scale'] = { x: 1, y: 1 }) {
  const [scale, setScale] = React.useState(initScale);

  const scaleIn = React.useCallback(() => {
    setScale((prev) => ({
      x: Math.min(prev.x * SCALE_FACTOR, MAX_SCALE),
      y: Math.min(prev.y * SCALE_FACTOR, MAX_SCALE),
    }));
  }, []);

  const scaleOut = React.useCallback(() => {
    setScale((prev) => ({
      x: Math.max(prev.x / SCALE_FACTOR, MIN_SCALE),
      y: Math.max(prev.y / SCALE_FACTOR, MIN_SCALE),
    }));
  }, []);

  return {
    scale,
    canZoomIn: scale.x < MAX_SCALE,
    canZoomOut: scale.x > MIN_SCALE,
    scaleIn,
    scaleOut,
    scaleTo: setScale,
  };
}
