import { x as commonjsGlobal, w as getDefaultExportFromCjs, u as useChartEditStore, M as MenuEnum, a3 as WinKeyboard, a4 as MacKeyboard, a5 as setKeyboardDressShow, a6 as throttle, a7 as debounce } from "./index-67bc0910.js";
import "vue";
var keymaster$1 = { exports: {} };
(function(module) {
  (function(global) {
    var k, _handlers = {}, _mods = { 16: false, 18: false, 17: false, 91: false }, _scope = "all", _MODIFIERS = {
      "⇧": 16,
      shift: 16,
      "⌥": 18,
      alt: 18,
      option: 18,
      "⌃": 17,
      ctrl: 17,
      control: 17,
      "⌘": 91,
      command: 91
    }, _MAP = {
      backspace: 8,
      tab: 9,
      clear: 12,
      enter: 13,
      "return": 13,
      esc: 27,
      escape: 27,
      space: 32,
      left: 37,
      up: 38,
      right: 39,
      down: 40,
      del: 46,
      "delete": 46,
      home: 36,
      end: 35,
      pageup: 33,
      pagedown: 34,
      ",": 188,
      ".": 190,
      "/": 191,
      "`": 192,
      "-": 189,
      "=": 187,
      ";": 186,
      "'": 222,
      "[": 219,
      "]": 221,
      "\\": 220
    }, code = function(x) {
      return _MAP[x] || x.toUpperCase().charCodeAt(0);
    }, _downKeys = [];
    for (k = 1; k < 20; k++)
      _MAP["f" + k] = 111 + k;
    function index(array, item) {
      var i = array.length;
      while (i--)
        if (array[i] === item)
          return i;
      return -1;
    }
    function compareArray(a1, a2) {
      if (a1.length != a2.length)
        return false;
      for (var i = 0; i < a1.length; i++) {
        if (a1[i] !== a2[i])
          return false;
      }
      return true;
    }
    var modifierMap = {
      16: "shiftKey",
      18: "altKey",
      17: "ctrlKey",
      91: "metaKey"
    };
    function updateModifierKey(event) {
      for (k in _mods)
        _mods[k] = event[modifierMap[k]];
    }
    function dispatch(event) {
      var key, handler, k2, i, modifiersMatch, scope;
      key = event.keyCode;
      if (index(_downKeys, key) == -1) {
        _downKeys.push(key);
      }
      if (key == 93 || key == 224)
        key = 91;
      if (key in _mods) {
        _mods[key] = true;
        for (k2 in _MODIFIERS)
          if (_MODIFIERS[k2] == key)
            assignKey[k2] = true;
        return;
      }
      updateModifierKey(event);
      if (!assignKey.filter.call(this, event))
        return;
      if (!(key in _handlers))
        return;
      scope = getScope();
      for (i = 0; i < _handlers[key].length; i++) {
        handler = _handlers[key][i];
        if (handler.scope == scope || handler.scope == "all") {
          modifiersMatch = handler.mods.length > 0;
          for (k2 in _mods)
            if (!_mods[k2] && index(handler.mods, +k2) > -1 || _mods[k2] && index(handler.mods, +k2) == -1)
              modifiersMatch = false;
          if (handler.mods.length == 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch) {
            if (handler.method(event, handler) === false) {
              if (event.preventDefault)
                event.preventDefault();
              else
                event.returnValue = false;
              if (event.stopPropagation)
                event.stopPropagation();
              if (event.cancelBubble)
                event.cancelBubble = true;
            }
          }
        }
      }
    }
    function clearModifier(event) {
      var key = event.keyCode, k2, i = index(_downKeys, key);
      if (i >= 0) {
        _downKeys.splice(i, 1);
      }
      if (key == 93 || key == 224)
        key = 91;
      if (key in _mods) {
        _mods[key] = false;
        for (k2 in _MODIFIERS)
          if (_MODIFIERS[k2] == key)
            assignKey[k2] = false;
      }
    }
    function resetModifiers() {
      for (k in _mods)
        _mods[k] = false;
      for (k in _MODIFIERS)
        assignKey[k] = false;
    }
    function assignKey(key, scope, method) {
      var keys, mods;
      keys = getKeys(key);
      if (method === void 0) {
        method = scope;
        scope = "all";
      }
      for (var i = 0; i < keys.length; i++) {
        mods = [];
        key = keys[i].split("+");
        if (key.length > 1) {
          mods = getMods(key);
          key = [key[key.length - 1]];
        }
        key = key[0];
        key = code(key);
        if (!(key in _handlers))
          _handlers[key] = [];
        _handlers[key].push({ shortcut: keys[i], scope, method, key: keys[i], mods });
      }
    }
    function unbindKey(key, scope) {
      var multipleKeys, keys, mods = [], i, j, obj;
      multipleKeys = getKeys(key);
      for (j = 0; j < multipleKeys.length; j++) {
        keys = multipleKeys[j].split("+");
        if (keys.length > 1) {
          mods = getMods(keys);
          key = keys[keys.length - 1];
        }
        key = code(key);
        if (scope === void 0) {
          scope = getScope();
        }
        if (!_handlers[key]) {
          return;
        }
        for (i = 0; i < _handlers[key].length; i++) {
          obj = _handlers[key][i];
          if (obj.scope === scope && compareArray(obj.mods, mods)) {
            _handlers[key][i] = {};
          }
        }
      }
    }
    function isPressed(keyCode) {
      if (typeof keyCode == "string") {
        keyCode = code(keyCode);
      }
      return index(_downKeys, keyCode) != -1;
    }
    function getPressedKeyCodes() {
      return _downKeys.slice(0);
    }
    function filter(event) {
      var tagName = (event.target || event.srcElement).tagName;
      return !(tagName == "INPUT" || tagName == "SELECT" || tagName == "TEXTAREA");
    }
    for (k in _MODIFIERS)
      assignKey[k] = false;
    function setScope(scope) {
      _scope = scope || "all";
    }
    function getScope() {
      return _scope || "all";
    }
    function deleteScope(scope) {
      var key, handlers, i;
      for (key in _handlers) {
        handlers = _handlers[key];
        for (i = 0; i < handlers.length; ) {
          if (handlers[i].scope === scope)
            handlers.splice(i, 1);
          else
            i++;
        }
      }
    }
    function getKeys(key) {
      var keys;
      key = key.replace(/\s/g, "");
      keys = key.split(",");
      if (keys[keys.length - 1] == "") {
        keys[keys.length - 2] += ",";
      }
      return keys;
    }
    function getMods(key) {
      var mods = key.slice(0, key.length - 1);
      for (var mi = 0; mi < mods.length; mi++)
        mods[mi] = _MODIFIERS[mods[mi]];
      return mods;
    }
    function addEvent(object, event, method) {
      if (object.addEventListener)
        object.addEventListener(event, method, false);
      else if (object.attachEvent)
        object.attachEvent("on" + event, function() {
          method(window.event);
        });
    }
    addEvent(document, "keydown", function(event) {
      dispatch(event);
    });
    addEvent(document, "keyup", clearModifier);
    addEvent(window, "focus", resetModifiers);
    var previousKey = global.key;
    function noConflict() {
      var k2 = global.key;
      global.key = previousKey;
      return k2;
    }
    global.key = assignKey;
    global.key.setScope = setScope;
    global.key.getScope = getScope;
    global.key.deleteScope = deleteScope;
    global.key.filter = filter;
    global.key.isPressed = isPressed;
    global.key.getPressedKeyCodes = getPressedKeyCodes;
    global.key.noConflict = noConflict;
    global.key.unbind = unbindKey;
    module.exports = assignKey;
  })(commonjsGlobal);
})(keymaster$1);
var keymasterExports = keymaster$1.exports;
const keymaster = /* @__PURE__ */ getDefaultExportFromCjs(keymasterExports);
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 {
  useAddKeyboard as u
};
