"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 mention_list_exports = {};
__export(mention_list_exports, {
  MentionList: () => MentionList
});
module.exports = __toCommonJS(mention_list_exports);
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_tooltip = require("./../../wix-rich-content-common/lib/tooltip.js");
var import_mention_list_scss = __toESM(require("../statics/mention-list-scss.js"));
var import_mention_avatar = require("./mention-avatar.js");
const MentionList = (0, import_react.forwardRef)((props, ref) => {
  const { languageDir } = (0, import_react.useContext)(import_ricos_context.RicosContext) || {};
  const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
  const selectItem = (index) => {
    if (props.items[index]) {
      props.command(props.items[index]);
    }
  };
  const upHandler = () => {
    setSelectedIndex((selectedIndex + props.items.length - 1) % props.items.length);
  };
  const downHandler = () => {
    setSelectedIndex((selectedIndex + 1) % props.items.length);
  };
  const enterHandler = () => {
    selectItem(selectedIndex);
  };
  (0, import_react.useEffect)(() => setSelectedIndex(0), [props.items]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    onKeyDown: ({ event }) => {
      if (event.key === "ArrowUp") {
        upHandler();
        return true;
      }
      if (event.key === "ArrowDown") {
        downHandler();
        return true;
      }
      if (["Enter", "Tab"].includes(event.key)) {
        enterHandler();
        return true;
      }
      return false;
    }
  }));
  const hasEllipsis = (name) => {
    const charactersLimit = 23;
    return name.length > charactersLimit;
  };
  return props.items.length ? /* @__PURE__ */ import_react.default.createElement("div", { dir: languageDir, className: import_mention_list_scss.default.mention_list }, props.items.map((item, i) => {
    const tooltipContent = hasEllipsis(item.name) ? item.name : null;
    return /* @__PURE__ */ import_react.default.createElement(import_tooltip.Tooltip, { key: i, content: tooltipContent, place: "top" }, /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: i,
        className: (0, import_classnames.default)(import_mention_list_scss.default.mention_item, {
          [import_mention_list_scss.default.mention_item_selected]: i === selectedIndex
        }),
        onMouseOver: () => setSelectedIndex(i),
        onClick: () => selectItem(i)
      },
      /* @__PURE__ */ import_react.default.createElement(import_mention_avatar.MentionAvatar, { src: item?.avatar, alt: item.name }),
      /* @__PURE__ */ import_react.default.createElement("span", null, item.name)
    ));
  })) : null;
});
//# sourceMappingURL=mention-list.js.map
