"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const antd = require("antd");
const t = require("io-ts");
const React = require("react");
const kit_Theme_UI = require("./Theme/UI.cjs");
require("./Theme/themeUtils.cjs");
require("./Theme/themes.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const t__namespace = /* @__PURE__ */ _interopNamespaceDefault(t);
const KeyboardShortcut = t__namespace.type({
  alt: t__namespace.boolean,
  ctrl: t__namespace.boolean,
  key: t__namespace.string,
  meta: t__namespace.boolean,
  shift: t__namespace.boolean
});
const matchesShortcut = (e, shortcut) => e.ctrlKey === shortcut.ctrl && e.metaKey === shortcut.meta && e.altKey === shortcut.alt && e.shiftKey === shortcut.shift && formatKey(e.code, e.key) === shortcut.key;
const shortcutToString = (shortcut) => {
  const os = window.navigator.userAgent;
  const s = [];
  shortcut.ctrl && s.push("Ctrl");
  shortcut.meta && s.push(os.includes("Mac") ? "Cmd" : os.includes("Win") ? "Win" : "Super");
  shortcut.shift && s.push("Shift");
  shortcut.alt && s.push("Alt");
  shortcut.key && s.push(shortcut.key);
  return s.join(" + ");
};
const formatKey = (code, key) => {
  if (code.startsWith("Digit"))
    return code.replace("Digit", "");
  switch (code) {
    case "BracketLeft":
      return "[";
    case "BracketRight":
      return "]";
    case "Backslash":
      return "\\";
    case "Semicolon":
      return ";";
    case "Quote":
      return "'";
    case "Comma":
      return ",";
    case "Period":
      return ".";
    case "Slash":
      return "/";
    case "Space":
      return "Space";
    default:
      if (["Control", "Meta", "Alt", "Shift"].includes(key))
        return "";
      return key.toUpperCase();
  }
};
const InputShortcut = ({
  placeholder = "Press any key",
  value,
  onChange,
  ...props
}) => {
  const {
    themeSettings: { className: themeClass }
  } = kit_Theme_UI.useTheme();
  const inputRef = React.useRef(null);
  const [inputValue, setInputValue] = React.useState();
  React.useEffect(() => {
    value && setInputValue(shortcutToString(value));
  }, [value]);
  const onKeyDown = React.useCallback(
    (e) => {
      var _a;
      e.stopPropagation();
      e.preventDefault();
      const keys = {
        alt: e.altKey,
        ctrl: e.ctrlKey,
        key: formatKey(e.code, e.key),
        meta: e.metaKey,
        shift: e.shiftKey
      };
      if (shortcutToString(keys) === "TAB") {
        (_a = inputRef == null ? void 0 : inputRef.current) == null ? void 0 : _a.blur();
      } else {
        value ? onChange == null ? void 0 : onChange(keys) : setInputValue(shortcutToString(keys));
      }
    },
    [onChange, value]
  );
  return /* @__PURE__ */ jsxRuntime.jsx(
    antd.Input,
    {
      className: themeClass,
      placeholder,
      ref: inputRef,
      value: inputValue,
      onKeyDown,
      ...props
    }
  );
};
exports.KeyboardShortcut = KeyboardShortcut;
exports.default = InputShortcut;
exports.formatKey = formatKey;
exports.matchesShortcut = matchesShortcut;
exports.shortcutToString = shortcutToString;
