import { useState, useEffect } from "react";
import { getPaperDimensions } from "@/config/paperSizes";

const SCALE = 0.8;

export const useStageSize = (catalogSize?: string) => {
    const { width: baseWidth, height: baseHeight } = getPaperDimensions(catalogSize ?? "a4");

    const [stageSize, setStageSize] = useState({
        width: baseWidth,
        height: baseHeight,
        scale: SCALE,
    });

    useEffect(() => {
        const { width, height } = getPaperDimensions(catalogSize ?? "a4");

        const handleResize = () => {
            const maxWidth = window.innerWidth - 400;
            const maxHeight = window.innerHeight - 100;
            const scaleX = maxWidth / width;
            const scaleY = maxHeight / height;
            const newScale = Math.min(scaleX, scaleY, SCALE);
            setStageSize({ width, height, scale: newScale });
        };

        handleResize();
        window.addEventListener("resize", handleResize);
        return () => window.removeEventListener("resize", handleResize);
    }, [catalogSize]);

    return stageSize;
};

