import { useChartEditStore } from "../../../store/modules/chartEditStore/chartEditStore.mjs";
import { MenuEnum, WinKeyboard, MacKeyboard } from "../../../enums/editPageEnum.mjs";
import throttle from "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/throttle.mjs";
import debounce from "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/debounce.mjs";
import keymaster from "../../../../node_modules/.pnpm/keymaster@1.6.2/node_modules/keymaster/keymaster.mjs";
import { setKeyboardDressShow } 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 "../../../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.mjs";
import "../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import "../../../components/GoSkeleton/index.vue.mjs";
import "../../../components/GoLoading/index.mjs";
import "../../../settings/designSetting.mjs";
const chartEditStore = useChartEditStore();
const winCtrlMerge = (e) => `${WinKeyboard.CTRL}+${e}`;
const winShiftMerge = (e) => `${WinKeyboard.SHIFT}+${e}`;
const winKeyboardValue = {
  [MenuEnum.ARROW_UP]: winCtrlMerge("up"),
  [MenuEnum.ARROW_RIGHT]: winCtrlMerge("right"),
  [MenuEnum.ARROW_DOWN]: winCtrlMerge("down"),
  [MenuEnum.ARROW_LEFT]: winCtrlMerge("left"),
  [MenuEnum.COPY]: winCtrlMerge("c"),
  [MenuEnum.CUT]: winCtrlMerge("x"),
  [MenuEnum.PARSE]: winCtrlMerge("v"),
  [MenuEnum.DELETE]: "delete",
  [MenuEnum.BACK]: winCtrlMerge("z"),
  [MenuEnum.FORWORD]: winCtrlMerge(winShiftMerge("z")),
  [MenuEnum.GROUP]: winCtrlMerge("g"),
  [MenuEnum.UN_GROUP]: winCtrlMerge(winShiftMerge("g")),
  [MenuEnum.LOCK]: winCtrlMerge("l"),
  [MenuEnum.UNLOCK]: winCtrlMerge(winShiftMerge("l")),
  [MenuEnum.HIDE]: winCtrlMerge("h"),
  [MenuEnum.SHOW]: winCtrlMerge(winShiftMerge("h"))
};
const macCtrlMerge = (e) => `${MacKeyboard.CTRL}+${e}`;
const macShiftMerge = (e) => `${MacKeyboard.SHIFT}+${e}`;
const macKeyboardValue = {
  [MenuEnum.ARROW_UP]: macCtrlMerge("arrowup"),
  [MenuEnum.ARROW_RIGHT]: macCtrlMerge("arrowright"),
  [MenuEnum.ARROW_DOWN]: macCtrlMerge("arrowdown"),
  [MenuEnum.ARROW_LEFT]: macCtrlMerge("arrowleft"),
  [MenuEnum.COPY]: macCtrlMerge("c"),
  [MenuEnum.CUT]: macCtrlMerge("x"),
  [MenuEnum.PARSE]: macCtrlMerge("v"),
  [MenuEnum.DELETE]: macCtrlMerge("backspace"),
  [MenuEnum.BACK]: macCtrlMerge("z"),
  [MenuEnum.FORWORD]: macCtrlMerge(macShiftMerge("z")),
  [MenuEnum.GROUP]: macCtrlMerge("g"),
  [MenuEnum.UN_GROUP]: macCtrlMerge(macShiftMerge("g")),
  [MenuEnum.LOCK]: macCtrlMerge("l"),
  [MenuEnum.UNLOCK]: macCtrlMerge(macShiftMerge("l")),
  [MenuEnum.HIDE]: macCtrlMerge("h"),
  [MenuEnum.SHOW]: macCtrlMerge(macShiftMerge("h"))
};
const winKeyList = [
  winKeyboardValue.up,
  winKeyboardValue.right,
  winKeyboardValue.down,
  winKeyboardValue.left,
  winKeyboardValue.delete,
  winKeyboardValue.copy,
  winKeyboardValue.cut,
  winKeyboardValue.parse,
  winKeyboardValue.back,
  winKeyboardValue.forward,
  winKeyboardValue.group,
  winKeyboardValue.unGroup,
  winKeyboardValue.lock,
  winKeyboardValue.unLock,
  winKeyboardValue.hide,
  winKeyboardValue.show
];
const macKeyList = [
  macKeyboardValue.up,
  macKeyboardValue.right,
  macKeyboardValue.down,
  macKeyboardValue.left,
  macKeyboardValue.delete,
  macKeyboardValue.copy,
  macKeyboardValue.cut,
  macKeyboardValue.parse,
  macKeyboardValue.back,
  macKeyboardValue.forward,
  macKeyboardValue.group,
  macKeyboardValue.unGroup,
  macKeyboardValue.lock,
  macKeyboardValue.unLock,
  macKeyboardValue.hide,
  macKeyboardValue.show
];
const keyRecordHandle = () => {
  window.$KeyboardActive = {
    ctrl: false,
    space: false
  };
  document.onkeydown = (e) => {
    const { keyCode } = e;
    if (keyCode == 32 && e.target == document.body)
      e.preventDefault();
    if ([17, 32].includes(keyCode) && window.$KeyboardActive) {
      setKeyboardDressShow(e.keyCode);
      switch (keyCode) {
        case 17:
          window.$KeyboardActive.ctrl = true;
          break;
        case 32:
          window.$KeyboardActive.space = true;
          break;
      }
    }
  };
  document.onkeyup = (e) => {
    const { keyCode } = e;
    if (keyCode == 32 && e.target == document.body)
      e.preventDefault();
    if ([17, 32].includes(keyCode) && window.$KeyboardActive) {
      setKeyboardDressShow();
      switch (keyCode) {
        case 17:
          window.$KeyboardActive.ctrl = false;
          break;
        case 32:
          window.$KeyboardActive.space = false;
          break;
      }
    }
  };
};
const useAddKeyboard = () => {
  const throttleTime = 50;
  const switchHandle = (keyboardValue, e) => {
    switch (e) {
      case keyboardValue.up:
        keymaster(e, throttle(() => {
          chartEditStore.setMove(MenuEnum.ARROW_UP);
          return false;
        }, throttleTime));
        break;
      case keyboardValue.right:
        keymaster(e, throttle(() => {
          chartEditStore.setMove(MenuEnum.ARROW_RIGHT);
          return false;
        }, throttleTime));
        break;
      case keyboardValue.down:
        keymaster(e, throttle(() => {
          chartEditStore.setMove(MenuEnum.ARROW_DOWN);
          return false;
        }, throttleTime));
        break;
      case keyboardValue.left:
        keymaster(e, throttle(() => {
          chartEditStore.setMove(MenuEnum.ARROW_LEFT);
          return false;
        }, throttleTime));
        break;
      case keyboardValue.delete:
        keymaster(e, debounce(() => {
          chartEditStore.removeComponentList();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.copy:
        keymaster(e, debounce(() => {
          chartEditStore.setCopy();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.cut:
        keymaster(e, debounce(() => {
          chartEditStore.setCut();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.parse:
        keymaster(e, throttle(() => {
          chartEditStore.setParse();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.back:
        keymaster(e, throttle(() => {
          chartEditStore.setBack();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.forward:
        keymaster(e, throttle(() => {
          chartEditStore.setForward();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.group:
        keymaster(e, throttle(() => {
          chartEditStore.setGroup();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.unGroup:
        keymaster(e, throttle(() => {
          chartEditStore.setUnGroup();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.lock:
        keymaster(e, throttle(() => {
          chartEditStore.setLock();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.unLock:
        keymaster(e, throttle(() => {
          chartEditStore.setUnLock();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.hide:
        keymaster(e, throttle(() => {
          chartEditStore.setHide();
          return false;
        }, throttleTime));
        break;
      case keyboardValue.show:
        keymaster(e, throttle(() => {
          chartEditStore.setShow();
          return false;
        }, throttleTime));
        break;
    }
  };
  winKeyList.forEach((key) => {
    switchHandle(winKeyboardValue, key);
  });
  macKeyList.forEach((key) => {
    switchHandle(macKeyboardValue, key);
  });
  keyRecordHandle();
};
export {
  macKeyboardValue,
  useAddKeyboard,
  winKeyboardValue
};
