import { u as useKeyboardShortcutsPaneState } from './useKeyboardShortcutsPaneState-C_eLceJQ.js';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import Close from '@bigbinary/neeto-icons/Close';
import Typography from '@bigbinary/neetoui/Typography';
import Button from '@bigbinary/neetoui/Button';
import { manager } from '@bigbinary/neetoui/managers';
import classnames from 'classnames';
import useHotkeys from '@bigbinary/neeto-hotkeys';
import { useTranslation } from 'react-i18next';
import { memo, Fragment, createElement } from 'react';
import { withT } from '@bigbinary/neeto-commons-frontend/react-utils';
import Kbd from '@bigbinary/neetoui/Kbd';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import i18next from 'i18next';
import { toPairs } from 'ramda';
import { U as UAParser } from './ua-parser-rRwDJPFB.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { n } from './inject-css-C2dztUxs.js';
import 'zustand/shallow';
import '@bigbinary/neeto-commons-frontend/react-utils/withImmutableActions';
import 'zustand';

var KEY_SHORTFORM_MAP = {
  command: "⌘",
  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 = {
  "⌘": "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 HotKey = function HotKey(_ref) {
  var description = _ref.description,
    sequence = _ref.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(Typography, {
        className: "neeto-ui-text-gray-800 min-w-0 flex-grow break-words",
        lineHeight: "normal",
        style: "body2",
        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(Kbd, {
              keyName: keyName,
              tooltipProps: getTooltipProps(keyName)
            }), isSequentialHotkey && idx + 1 !== sequences.length && /*#__PURE__*/jsx(Kbd, {
              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(Typography, {
        style: "h6",
        textTransform: "uppercase",
        weight: "bold",
        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, {
        className: "mb-3",
        href: fullShortcutsLink,
        style: "link",
        target: "_blank",
        label: t("neetoMolecules.keyboardShortcuts.viewFullListOfShortcuts")
      })]
    }, categoryName);
  });
});
var HotKeyList$1 = /*#__PURE__*/memo(HotKeyList);

var css = ".neeto-molecules-keyboard-shortcuts-pane{flex-grow:0;flex-shrink:0;z-index:calc(var(--neeto-ui-modal-z-index) + 1)}";
n(css,{});

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",
    className: classnames("neeto-molecules-keyboard-shortcuts-pane neeto-ui-border-gray-300 transition-width neeto-ui-bg-white ms-auto h-dvh overflow-hidden border-l duration-300 ease-in-out rtl:border-l-0 rtl:border-r", {
      "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: "neeto-ui-border-gray-300 my-2 flex items-center justify-between border-b px-4 pb-2",
        children: [/*#__PURE__*/jsx(Typography, {
          "data-testid": "keyboard-shortcuts-pane-title",
          style: "h4",
          children: t("neetoMolecules.keyboardShortcuts.title")
        }), /*#__PURE__*/jsx(Button, {
          "data-testid": "keyboard-shortcuts-pane-close-button",
          icon: Close,
          style: "text",
          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
