import "../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import "../../../store/modules/designStore/designStore.mjs";
import "../../../settings/designSetting.mjs";
import "../../../../node_modules/.pnpm/screenfull@6.0.2/node_modules/screenfull/index.mjs";
import "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.mjs";
import "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.mjs";
import "../../../../node_modules/.pnpm/html2canvas@1.4.1/node_modules/html2canvas/dist/html2canvas.esm.mjs";
import "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseClone.mjs";
import "../../../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.mjs";
import "../../../enums/pageEnum.mjs";
import "../../../router/index.mjs";
import "../../../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.mjs";
import "../../../components/GoSkeleton/index.vue.mjs";
import "../../../components/GoLoading/index.mjs";
import { usePreviewFullScale, usePreviewScrollXScale, usePreviewScrollYScale, usePreviewFitScale } from "../../../hooks/usePreviewScale.hook.mjs";
import "../../../api/axios.mjs";
import "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/merge.mjs";
import "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/pick.mjs";
import "../../../../node_modules/.pnpm/echarts@5.4.3/node_modules/echarts/index.mjs";
import { PreviewScaleEnum } from "../../../enums/styleEnum.mjs";
import "../../../store/modules/langStore/langStore.mjs";
import { ref } from "../../../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
import { provide, onMounted, onUnmounted } from "../../../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.mjs";
const SCALE_KEY = "scale-value";
const useScale = (localStorageInfo) => {
  const entityRef = ref();
  const previewRef = ref();
  const width = ref(localStorageInfo.editCanvasConfig.width);
  const height = ref(localStorageInfo.editCanvasConfig.height);
  const scaleRef = ref({ width: 1, height: 1 });
  provide(SCALE_KEY, scaleRef);
  const useAddWheelHandle = (removeEvent) => {
    addEventListener(
      "wheel",
      (e) => {
        var _a, _b;
        if ((_a = window == null ? void 0 : window.$KeyboardActive) == null ? void 0 : _a.ctrl) {
          e.preventDefault();
          e.stopPropagation();
          removeEvent();
          const transform = (_b = previewRef.value) == null ? void 0 : _b.style.transform;
          const regRes = transform.match(/scale\((\d+\.?\d*)*/);
          const width2 = regRes[1];
          const previewBoxDom = document.querySelector(".go-preview");
          const entityDom = document.querySelector(".go-preview-entity");
          if (previewBoxDom) {
            previewBoxDom.style.overflow = "unset";
            previewBoxDom.style.position = "absolute";
          }
          if (entityDom) {
            entityDom.style.overflow = "unset";
          }
          if (e.wheelDelta > 0) {
            const resNum = parseFloat(Number(width2).toFixed(2));
            previewRef.value.style.transform = `scale(${resNum > 5 ? 5 : resNum + 0.1})`;
          } else {
            const resNum = parseFloat(Number(width2).toFixed(2));
            previewRef.value.style.transform = `scale(${resNum < 0.2 ? 0.2 : resNum - 0.1})`;
          }
        }
      },
      { passive: false }
    );
  };
  const updateScaleRef = (scale) => {
    scaleRef.value = { ...scale };
  };
  onMounted(() => {
    switch (localStorageInfo.editCanvasConfig.previewScaleType) {
      case PreviewScaleEnum.FIT:
        (() => {
          const { calcRate, windowResize, unWindowResize } = usePreviewFitScale(
            width.value,
            height.value,
            previewRef.value,
            updateScaleRef
          );
          calcRate();
          windowResize();
          useAddWheelHandle(unWindowResize);
          onUnmounted(() => {
            unWindowResize();
          });
        })();
        break;
      case PreviewScaleEnum.SCROLL_Y:
        (() => {
          const { calcRate, windowResize, unWindowResize } = usePreviewScrollYScale(
            width.value,
            height.value,
            previewRef.value,
            (scale) => {
              const dom = entityRef.value;
              dom.style.width = `${width.value * scale.width}px`;
              dom.style.height = `${height.value * scale.height}px`;
              updateScaleRef(scale);
            }
          );
          calcRate();
          windowResize();
          useAddWheelHandle(unWindowResize);
          onUnmounted(() => {
            unWindowResize();
          });
        })();
        break;
      case PreviewScaleEnum.SCROLL_X:
        (() => {
          const { calcRate, windowResize, unWindowResize } = usePreviewScrollXScale(
            width.value,
            height.value,
            previewRef.value,
            (scale) => {
              const dom = entityRef.value;
              dom.style.width = `${width.value * scale.width}px`;
              dom.style.height = `${height.value * scale.height}px`;
              updateScaleRef(scale);
            }
          );
          calcRate();
          windowResize();
          useAddWheelHandle(unWindowResize);
          onUnmounted(() => {
            unWindowResize();
          });
        })();
        break;
      case PreviewScaleEnum.FULL:
        (() => {
          const { calcRate, windowResize, unWindowResize } = usePreviewFullScale(
            width.value,
            height.value,
            previewRef.value,
            updateScaleRef
          );
          calcRate();
          windowResize();
          useAddWheelHandle(unWindowResize);
          onUnmounted(() => {
            unWindowResize();
          });
        })();
        break;
    }
  });
  return {
    entityRef,
    previewRef,
    scaleRef
  };
};
export {
  SCALE_KEY,
  useScale
};
