import "../../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import { DragKeyEnum, MouseEventButton } from "../../../../enums/editPageEnum.mjs";
import { createComponent } from "../../../../packages/index.mjs";
import { useContextMenu } from "../../hooks/useContextMenu.hook.mjs";
import { useChartEditStore } from "../../../../store/modules/chartEditStore/chartEditStore.mjs";
import { EditCanvasTypeEnum } from "../../../../store/modules/chartEditStore/chartEditStore.d.mjs";
import { JSONParse, setComponentPosition } from "../../../../utils/utils.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 "../../../../store/modules/designStore/designStore.mjs";
import { loadingStart, loadingFinish, loadingError } from "../../../../utils/plugin.mjs";
import "../../../../components/GoLoading/index.mjs";
import { l as lodashExports } from "../../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.mjs";
import { toRaw } from "../../../../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
const chartEditStore = useChartEditStore();
const { onClickOutSide } = useContextMenu();
const dragHandle = async (e) => {
  e.preventDefault();
  try {
    loadingStart();
    const drayDataString = e.dataTransfer.getData(DragKeyEnum.DRAG_KEY);
    if (!drayDataString) {
      loadingFinish();
      return;
    }
    chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, false);
    const dropData = JSONParse(drayDataString);
    if (dropData.disabled)
      return;
    let newComponent = await createComponent(dropData);
    if (dropData.redirectComponent) {
      dropData.dataset && (newComponent.option.dataset = dropData.dataset);
      newComponent.chartConfig.title = dropData.title;
      newComponent.chartConfig.chartFrame = dropData.chartFrame;
    }
    setComponentPosition(newComponent, e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2);
    chartEditStore.addComponentList(newComponent, false, true);
    chartEditStore.setTargetSelectChart(newComponent.id);
    loadingFinish();
  } catch (error) {
    loadingError();
    window["$message"].warning(`图表正在研发中, 敬请期待...`);
  }
};
const dragoverHandle = (e) => {
  e.preventDefault();
  e.stopPropagation();
  if (e.dataTransfer)
    e.dataTransfer.dropEffect = "copy";
};
const mousedownHandleUnStop = (e, item) => {
  if (item) {
    chartEditStore.setTargetSelectChart(item.id);
    return;
  }
  chartEditStore.setTargetSelectChart(void 0);
};
const mousedownBoxSelect = (e, item) => {
  var _a;
  if (e.which == 2)
    return;
  if ((_a = window.$KeyboardActive) == null ? void 0 : _a.space)
    return;
  mousedownHandleUnStop();
  const startOffsetX = e.offsetX;
  const startOffsetY = e.offsetY;
  const startScreenX = e.screenX;
  const startScreenY = e.screenY;
  const scale = chartEditStore.getEditCanvas.scale;
  chartEditStore.setMousePosition(void 0, void 0, startOffsetX, startOffsetY);
  const mousemove = lodashExports.throttle((moveEvent) => {
    chartEditStore.setTargetSelectChart();
    chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, true);
    const currX = startOffsetX + moveEvent.screenX - startScreenX;
    const currY = startOffsetY + moveEvent.screenY - startScreenY;
    chartEditStore.setMousePosition(currX, currY);
    const selectAttr = {
      // 左上角
      x1: 0,
      y1: 0,
      // 右下角
      x2: 0,
      y2: 0
    };
    if (currX > startOffsetX && currY > startOffsetY) {
      selectAttr.x1 = startOffsetX;
      selectAttr.y1 = startOffsetY;
      selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale);
      selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale);
    } else if (currX > startOffsetX && currY < startOffsetY) {
      selectAttr.x1 = startOffsetX;
      selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale);
      selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale);
      selectAttr.y2 = startOffsetY;
    } else if (currX < startOffsetX && currY > startOffsetY) {
      selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale);
      selectAttr.y1 = startOffsetY;
      selectAttr.x2 = startOffsetX;
      selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale);
    } else {
      selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale);
      selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale);
      selectAttr.x2 = startOffsetX;
      selectAttr.y2 = startOffsetY;
    }
    chartEditStore.getComponentList.forEach((item2) => {
      if (!chartEditStore.getTargetChart.selectId.includes(item2.id)) {
        const { x, y, w, h } = item2.attr;
        const targetAttr = {
          // 左上角
          x1: x,
          y1: y,
          // 右下角
          x2: x + w,
          y2: y + h
        };
        if (targetAttr.x1 - selectAttr.x1 >= 0 && targetAttr.y1 - selectAttr.y1 >= 0 && targetAttr.x2 - selectAttr.x2 <= 0 && targetAttr.y2 - selectAttr.y2 <= 0 && !item2.status.lock && !item2.status.hide) {
          chartEditStore.setTargetSelectChart(item2.id, true);
        }
      }
    });
  }, 30);
  const mouseup = () => {
    mousemove.cancel();
    chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, false);
    chartEditStore.setMousePosition(0, 0, 0, 0);
    document.removeEventListener("mousemove", mousemove);
    document.removeEventListener("mouseup", mouseup);
  };
  document.addEventListener("mousemove", mousemove);
  document.addEventListener("mouseup", mouseup);
};
const useMouseHandle = () => {
  const mouseClickHandle = (e, item) => {
    var _a;
    e.preventDefault();
    e.stopPropagation();
    if (item.status.lock)
      return;
    if ((_a = window.$KeyboardActive) == null ? void 0 : _a.ctrl) {
      if (chartEditStore.targetChart.selectId.includes(item.id)) {
        const exList = chartEditStore.targetChart.selectId.filter((e2) => e2 !== item.id);
        chartEditStore.setTargetSelectChart(exList);
      } else {
        chartEditStore.setTargetSelectChart(item.id, true);
      }
    }
  };
  const mousedownHandle = (e, item) => {
    var _a;
    e.preventDefault();
    e.stopPropagation();
    if (item.status.lock)
      return;
    onClickOutSide();
    if (e.buttons === MouseEventButton.LEFT && ((_a = window.$KeyboardActive) == null ? void 0 : _a.ctrl))
      return;
    const selectId = chartEditStore.getTargetChart.selectId;
    if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id))
      return;
    chartEditStore.setTargetSelectChart(item.id);
    if (e.buttons === MouseEventButton.RIGHT)
      return;
    const scale = chartEditStore.getEditCanvas.scale;
    const canvasWidth = chartEditStore.getEditCanvasConfig.width;
    const canvasHeight = chartEditStore.getEditCanvasConfig.height;
    const targetMap = /* @__PURE__ */ new Map();
    chartEditStore.getTargetChart.selectId.forEach((id) => {
      const index = chartEditStore.fetchTargetIndex(id);
      if (index !== -1) {
        const { x, y, w, h } = toRaw(chartEditStore.getComponentList[index]).attr;
        targetMap.set(id, { x, y, w, h });
      }
    });
    const startX = e.screenX;
    const startY = e.screenY;
    let prevComponentInstance = [];
    chartEditStore.getTargetChart.selectId.forEach((id) => {
      if (!targetMap.has(id))
        return;
      const index = chartEditStore.fetchTargetIndex(id);
      prevComponentInstance.push(lodashExports.cloneDeep(chartEditStore.getComponentList[index]));
    });
    chartEditStore.setMousePosition(void 0, void 0, startX, startY);
    const mousemove = lodashExports.throttle((moveEvent) => {
      chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true);
      chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY);
      let offsetX = (moveEvent.screenX - startX) / scale;
      let offsetY = (moveEvent.screenY - startY) / scale;
      chartEditStore.getTargetChart.selectId.forEach((id) => {
        if (!targetMap.has(id))
          return;
        const index = chartEditStore.fetchTargetIndex(id);
        const { x, y, w, h } = targetMap.get(id);
        const componentInstance = chartEditStore.getComponentList[index];
        let currX = Math.round(x + offsetX);
        let currY = Math.round(y + offsetY);
        const distance = 50;
        currX = currX < -w + distance ? -w + distance : currX;
        currY = currY < -h + distance ? -h + distance : currY;
        currX = currX > canvasWidth - distance ? canvasWidth - distance : currX;
        currY = currY > canvasHeight - distance ? canvasHeight - distance : currY;
        if (componentInstance) {
          componentInstance.attr = Object.assign(componentInstance.attr, {
            x: currX,
            y: currY
          });
        }
      });
      return;
    }, 20);
    const mouseup = () => {
      try {
        chartEditStore.setMousePosition(0, 0, 0, 0);
        chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false);
        if (prevComponentInstance.length) {
          chartEditStore.getTargetChart.selectId.forEach((id) => {
            if (!targetMap.has(id))
              return;
            const index = chartEditStore.fetchTargetIndex(id);
            const curComponentInstance = chartEditStore.getComponentList[index];
            prevComponentInstance.forEach((preItem) => {
              if (preItem.id === id) {
                preItem.attr = Object.assign(preItem.attr, {
                  offsetX: curComponentInstance.attr.x - preItem.attr.x,
                  offsetY: curComponentInstance.attr.y - preItem.attr.y
                });
              }
            });
          });
          chartEditStore.moveComponentList(prevComponentInstance);
        }
        document.removeEventListener("mousemove", mousemove);
        document.removeEventListener("mouseup", mouseup);
      } catch (err) {
        console.log(err);
      }
    };
    document.addEventListener("mousemove", mousemove);
    document.addEventListener("mouseup", mouseup);
  };
  const mouseenterHandle = (e, item) => {
    e.preventDefault();
    e.stopPropagation();
    if (!chartEditStore.getEditCanvas.isSelect) {
      chartEditStore.setTargetHoverChart(item.id);
    }
  };
  const mouseleaveHandle = (e, item) => {
    e.preventDefault();
    e.stopPropagation();
    chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false);
    chartEditStore.setTargetHoverChart(void 0);
  };
  return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle };
};
const useMousePointHandle = (e, point, attr) => {
  e.stopPropagation();
  e.preventDefault();
  chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true);
  const scale = chartEditStore.getEditCanvas.scale;
  const itemAttrX = attr.x;
  const itemAttrY = attr.y;
  const itemAttrW = attr.w;
  const itemAttrH = attr.h;
  const startX = e.screenX;
  const startY = e.screenY;
  chartEditStore.setMousePosition(startX, startY);
  const mousemove = lodashExports.throttle((moveEvent) => {
    chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY);
    let currX = Math.round((moveEvent.screenX - startX) / scale);
    let currY = Math.round((moveEvent.screenY - startY) / scale);
    const isTop = /t/.test(point);
    const isBottom = /b/.test(point);
    const isLeft = /l/.test(point);
    const isRight = /r/.test(point);
    const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0);
    const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0);
    attr.h = newHeight > 0 ? newHeight : 0;
    attr.w = newWidth > 0 ? newWidth : 0;
    attr.x = itemAttrX + (isLeft ? currX : 0);
    attr.y = itemAttrY + (isTop ? currY : 0);
  }, 50);
  const mouseup = () => {
    chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false);
    chartEditStore.setMousePosition(0, 0, 0, 0);
    document.removeEventListener("mousemove", mousemove);
    document.removeEventListener("mouseup", mouseup);
  };
  document.addEventListener("mousemove", mousemove);
  document.addEventListener("mouseup", mouseup);
};
export {
  dragHandle,
  dragoverHandle,
  mousedownBoxSelect,
  mousedownHandleUnStop,
  useMouseHandle,
  useMousePointHandle
};
