import { jsx } from "react/jsx-runtime";
import { Input } from "antd";
import * as t from "io-ts";
import { useRef, useState, useEffect, useCallback } from "react";
import { useTheme } from "./Theme/UI.js";
import "./Theme/themeUtils.js";
import "./Theme/themes.js";
const KeyboardShortcut = t.type({
  alt: t.boolean,
  ctrl: t.boolean,
  key: t.string,
  meta: t.boolean,
  shift: t.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 }
  } = useTheme();
  const inputRef = useRef(null);
  const [inputValue, setInputValue] = useState();
  useEffect(() => {
    value && setInputValue(shortcutToString(value));
  }, [value]);
  const onKeyDown = 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__ */ jsx(
    Input,
    {
      className: themeClass,
      placeholder,
      ref: inputRef,
      value: inputValue,
      onKeyDown,
      ...props
    }
  );
};
export {
  KeyboardShortcut,
  InputShortcut as default,
  formatKey,
  matchesShortcut,
  shortcutToString
};
