import { u as useKeyboardShortcutsPaneState } from '../useKeyboardShortcutsPaneState-dNIbb_PI.js';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { Tooltip, Kbd, Button } from '@bigbinary/neeto-atoms';
import classnames from 'classnames';
import useHotkeys from '@bigbinary/neeto-hotkeys';
import { manager } from '@bigbinary/neetoui/managers';
import { useTranslation } from 'react-i18next';
import { forwardRef, memo, Fragment, createElement } from 'react';
import { withT } from '@bigbinary/neeto-commons-frontend/react-utils';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import i18next from 'i18next';
import { toPairs } from 'ramda';
import { U as UAParser } from '../ua-parser-OzUiuIwt.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { X } from '../x-B1Er0oV4.js';
import 'zustand/shallow';
import '@bigbinary/neeto-commons-frontend/react-utils/withImmutableActions';
import 'zustand';
import '../createLucideIcon-mNMhCCpf.js';

var KEY_SHORTFORM_MAP = {
  command: "\u2318",
  escape: "esc",
  control: "ctrl",
  option: "opt",
  "return": "enter"
};
var MAC_TO_WINDOWS_KEYS_MAP = {
  option: "alt",
  command: "ctrl",
  "return": "enter",
  "delete": "backspace"
};
var OS = {
  mac: "macOS"};
var FULL_SHORTCUTS_LINK_PROP_NAME = "fullShortcutsLink";
var TOOLTIP_CONTENT = {
  "\u2318": "command",
  esc: "escape",
  ctrl: "control",
  opt: "option",
  backspace: "delete"
};

var isMultipleHotkey = function isMultipleHotkey(hotkey) {
  return Array.isArray(hotkey);
};
var replaceKeys = function replaceKeys(hotkey, keyName, replaceWith) {
  return isMultipleHotkey(hotkey) ? hotkey.map(function (item) {
    return item.replaceAll(keyName, replaceWith);
  }) : hotkey.replaceAll(keyName, replaceWith);
};
var convertHotKeyToWindows = function convertHotKeyToWindows(hotkey) {
  toPairs(MAC_TO_WINDOWS_KEYS_MAP).forEach(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
      macKey = _ref2[0],
      windowsKey = _ref2[1];
    hotkey = replaceKeys(hotkey, macKey, windowsKey);
  });
  return hotkey;
};
var convertHotkeyToUsersPlatform = function convertHotkeyToUsersPlatform(hotkey) {
  var _result$os$name;
  var parser = new UAParser(navigator.userAgent);
  var result = parser.getResult();
  var isOSX = (_result$os$name = result.os.name) === null || _result$os$name === void 0 ? void 0 : _result$os$name.includes(OS.mac);
  if (isOSX) return hotkey;
  return convertHotKeyToWindows(hotkey);
};
var shortenHotKey = function shortenHotKey(hotkey) {
  var result = hotkey;
  Object.entries(KEY_SHORTFORM_MAP).forEach(function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 2),
      longform = _ref4[0],
      shortform = _ref4[1];
    result = result.replaceAll(longform, shortform);
  });
  return result;
};
var getGlobalShortcuts = function getGlobalShortcuts() {
  return _defineProperty({}, i18next.t("neetoMolecules.keyboardShortcuts.global.categoryName"), {
    openKeyboardShortcutsPane: {
      sequence: "command+/",
      description: i18next.t("neetoMolecules.keyboardShortcuts.global.openKeyboardShortcutsPane")
    },
    close: {
      sequence: "esc",
      description: i18next.t("neetoMolecules.keyboardShortcuts.global.close")
    },
    submitForm: {
      sequence: "command+return",
      description: i18next.t("neetoMolecules.keyboardShortcuts.global.submitForm")
    },
    openProductSwitcher: {
      sequence: "command+u",
      description: i18next.t("neetoMolecules.keyboardShortcuts.global.openProductSwitcher")
    }
  });
};
var getTooltipProps = function getTooltipProps(key) {
  if (TOOLTIP_CONTENT[key]) {
    return {
      position: "top",
      content: TOOLTIP_CONTENT[key],
      followCursor: "horizontal"
    };
  }
  return null;
};

var _excluded = ["children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var KbdWithRef = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/jsx("span", _objectSpread(_objectSpread({}, _objectSpread({
    ref: ref
  }, props)), {}, {
    children: /*#__PURE__*/jsx(Kbd, {
      className: "nv-kbd",
      children: children
    })
  }));
});
KbdWithRef.displayName = "KbdWithRef";
var KbdKey = function KbdKey(_ref2) {
  var keyName = _ref2.keyName,
    tooltipProps = _ref2.tooltipProps;
  if (tooltipProps) {
    return /*#__PURE__*/jsx(Tooltip, {
      content: tooltipProps.content,
      position: "top",
      children: /*#__PURE__*/jsx(KbdWithRef, {
        children: keyName
      })
    });
  }
  return /*#__PURE__*/jsx(Kbd, {
    className: "nv-kbd",
    children: keyName
  });
};
var HotKey = function HotKey(_ref3) {
  var description = _ref3.description,
    sequence = _ref3.sequence;
  var hotkey = shortenHotKey(convertHotkeyToUsersPlatform(sequence));
  var isSequentialHotkey = hotkey.includes(" ");
  var isSimultaneousHotkey = hotkey.includes("+");
  var isSingleKeyHotkey = !isSequentialHotkey && !isSimultaneousHotkey;
  var sequences = [];
  if (isSingleKeyHotkey) {
    sequences = [hotkey];
  } else if (isSequentialHotkey) {
    sequences = hotkey.split(" ");
  } else if (isSimultaneousHotkey) {
    sequences = hotkey.split("+");
  }
  return /*#__PURE__*/jsx("div", {
    className: "my-3 w-full",
    children: /*#__PURE__*/jsxs("div", {
      className: "flex items-center justify-between gap-3",
      "data-testid": "hotkey-item",
      children: [/*#__PURE__*/jsx("span", {
        className: "text-foreground min-w-0 flex-grow text-sm leading-normal break-words",
        children: description
      }), /*#__PURE__*/jsx("div", {
        className: "flex flex-shrink-0 items-center gap-1",
        children: sequences.map(function (keyName, idx) {
          return /*#__PURE__*/jsxs(Fragment, {
            children: [/*#__PURE__*/jsx(KbdKey, {
              keyName: keyName,
              tooltipProps: getTooltipProps(keyName)
            }), isSequentialHotkey && idx + 1 !== sequences.length && /*#__PURE__*/jsx(KbdKey, {
              keyName: "then"
            })]
          }, idx);
        })
      })]
    })
  });
};
var HotKey$1 = /*#__PURE__*/memo(HotKey);

var HotKeyList = withT(function (_ref) {
  var t = _ref.t,
    hotkeys = _ref.hotkeys;
  return Object.keys(hotkeys).map(function (categoryName) {
    var categoryValues = hotkeys[categoryName];
    var fullShortcutsLink = categoryValues[FULL_SHORTCUTS_LINK_PROP_NAME];
    return /*#__PURE__*/jsxs("div", {
      className: "mt-4 px-4",
      children: [/*#__PURE__*/jsx("span", {
        className: "text-foreground text-xs font-bold uppercase",
        children: categoryName
      }), Object.entries(categoryValues).map(function (_ref2) {
        var _ref3 = _slicedToArray(_ref2, 2),
          _ref3$ = _ref3[1],
          sequence = _ref3$.sequence,
          description = _ref3$.description;
        return sequence && /*#__PURE__*/createElement(HotKey$1, {
          description: description,
          sequence: sequence,
          key: sequence
        });
      }), fullShortcutsLink && /*#__PURE__*/jsx(Button, {
        asChild: true,
        className: "mb-3",
        variant: "link",
        children: /*#__PURE__*/jsx("a", {
          href: fullShortcutsLink,
          rel: "noreferrer",
          target: "_blank",
          children: t("neetoMolecules.keyboardShortcuts.viewFullListOfShortcuts")
        })
      })]
    }, categoryName);
  });
});
var HotKeyList$1 = /*#__PURE__*/memo(HotKeyList);

var KeyboardShortcutsPane = function KeyboardShortcutsPane(_ref) {
  var _ref$productShortcuts = _ref.productShortcuts,
    productShortcuts = _ref$productShortcuts === void 0 ? {} : _ref$productShortcuts;
  var _useKeyboardShortcuts = useKeyboardShortcutsPaneState(),
    _useKeyboardShortcuts2 = _slicedToArray(_useKeyboardShortcuts, 2),
    isOpen = _useKeyboardShortcuts2[0],
    setIsOpen = _useKeyboardShortcuts2[1];
  var hasOverlays = manager.hasOverlays();
  // eslint-disable-next-line @bigbinary/neeto/no-dangling-constants
  var GLOBAL_SHORTCUTS = getGlobalShortcuts();
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var shortcuts = GLOBAL_SHORTCUTS[t("neetoMolecules.keyboardShortcuts.global.categoryName")];
  useHotkeys(shortcuts.openKeyboardShortcutsPane.sequence, function () {
    return setIsOpen(function (prevIsOpen) {
      return !prevIsOpen;
    });
  }, {
    mode: "global"
  });
  useHotkeys(shortcuts.close.sequence, function () {
    return setIsOpen(false);
  }, {
    mode: "global",
    enabled: isOpen
  });
  return /*#__PURE__*/jsx("div", {
    "data-testid": "keyboard-shortcuts-pane",
    style: {
      zIndex: "var(--nv-pane-z-index, 100000)"
    },
    className: classnames("border-border bg-background transition-width ms-auto h-dvh shrink-0 grow-0 overflow-hidden border-l duration-300 ease-in-out rtl:border-r rtl:border-l-0", {
      "w-80": isOpen,
      "w-0": !isOpen,
      absolute: hasOverlays,
      "end-0": hasOverlays
    }),
    children: /*#__PURE__*/jsxs("div", {
      className: "h-full w-80 overflow-y-auto",
      children: [/*#__PURE__*/jsxs("div", {
        className: "border-border my-2 flex items-center justify-between border-b px-4 pb-2",
        children: [/*#__PURE__*/jsx("h4", {
          className: "text-foreground text-base font-semibold",
          "data-testid": "keyboard-shortcuts-pane-title",
          children: t("neetoMolecules.keyboardShortcuts.title")
        }), /*#__PURE__*/jsx(Button, {
          "data-testid": "keyboard-shortcuts-pane-close-button",
          icon: X,
          size: "icon",
          variant: "ghost",
          onClick: function onClick() {
            return setIsOpen(false);
          }
        })]
      }), /*#__PURE__*/jsx(HotKeyList$1, {
        hotkeys: GLOBAL_SHORTCUTS
      }), /*#__PURE__*/jsx(HotKeyList$1, {
        hotkeys: productShortcuts
      })]
    })
  });
};

var KeyboardShortcuts = {
  Pane: KeyboardShortcutsPane,
  usePaneState: useKeyboardShortcutsPaneState,
  GLOBAL_SHORTCUTS: getGlobalShortcuts()
};

export { KeyboardShortcuts as default };
//# sourceMappingURL=KeyboardShortcuts.js.map
