"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var emoji_suggestions_modal_exports = {};
__export(emoji_suggestions_modal_exports, {
  EmojiSuggestionsModal: () => EmojiSuggestionsModal
});
module.exports = __toCommonJS(emoji_suggestions_modal_exports);
var import_design_system = require("@wix/design-system");
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_ui_components_provider = require("./../../ui-components/ui-components-provider.js");
var import_emoji_suggestions_modal_scss = __toESM(require("./emoji-suggestions-modal-scss.js"));
const EmojiSuggestionsModal = ({
  command,
  items,
  query,
  messages
}) => {
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  const currElement = (0, import_react.useRef)(null);
  const [refElement, setRefElement] = (0, import_react.useState)(null);
  const [selectedId, setSelectedId] = (0, import_react.useState)();
  (0, import_react.useEffect)(() => {
    setSelectedId(items[0]?.id);
  }, [items]);
  (0, import_react.useEffect)(() => {
    const handleMessage = ({ type, payload }) => {
      if (type === "onKeyDown") {
        const { event } = payload;
        if (["ArrowUp", "ArrowDown"].includes(event.key)) {
          const direction = event.key === "ArrowUp" ? -1 : 1;
          if (!items.length) {
            return true;
          }
          setSelectedId((prev) => {
            const currIndex = items.findIndex((option) => option.id === prev);
            const nextIndex = (currIndex + direction + items.length) % items.length;
            return items[nextIndex].id;
          });
          return true;
        }
        if (["Enter", "Tab"].includes(event.key)) {
          const option = items.find((option2) => option2.id === selectedId);
          if (!option) {
            return false;
          }
          command({ emoji: option.native, query });
          return true;
        }
      }
    };
    messages?.subscribe(handleMessage);
    return () => messages?.unsubscribe(handleMessage);
  }, [messages, items, query, command, selectedId]);
  const dropdownOptions = (0, import_react.useMemo)(
    () => items.map((emoji) => ({
      as: "button",
      ellipsis: false,
      id: emoji.id,
      onClick: () => {
        command({ emoji: emoji.native, query });
      },
      optionTitle: `${emoji.native} ${emoji.shortcodes}`,
      shouldFocusWithoutScroll: true,
      title: false,
      value: "action"
    })),
    [items, query, command]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: import_emoji_suggestions_modal_scss.default.emojiSuggestionsModal,
      style: { zIndex: zIndexService.getZIndex("DIALOG") },
      ref: setRefElement
    },
    refElement && /* @__PURE__ */ import_react.default.createElement(import_ui_components_provider.UiComponentsProvider, { containerEl: refElement }, /* @__PURE__ */ import_react.default.createElement(
      import_design_system.DropdownBase,
      {
        ref: currElement,
        placement: "bottom-start",
        minWidth: 280,
        maxHeight: 250,
        focusOnOption: selectedId,
        options: dropdownOptions,
        onMouseDown: (e) => e.preventDefault(),
        open: true,
        listType: "action"
      }
    ))
  );
};
//# sourceMappingURL=emoji-suggestions-modal.js.map
