import React from "react";
import { Decoration_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import styles from "../statics/mentions-scss.js";
import { MENTION_TYPE } from "../types.js";
const decorator = (element, mentionDecoration, contextualData) => {
  const { mentionData } = mentionDecoration;
  const settings = contextualData.config[MENTION_TYPE] || {};
  const { onMentionClick, onMentionHover, getMentionLink } = settings;
  const ref = React.useRef(null);
  const onMouseOver = () => onMentionHover?.(mentionData, ref.current);
  const onClick = () => onMentionClick?.(mentionData);
  const onKeyDown = (e) => (e.key === "Enter" || e.key === " ") && onClick();
  const cursorStyle = onMentionClick === void 0 ? styles.cursorDefault : styles.cursorPointer;
  const commonProps = {
    onMouseOver,
    ref,
    className: `${styles.mention} ${styles.mentionViewer} ${cursorStyle}`
  };
  const key = `mention-${mentionData.id}`;
  if (onMentionClick) {
    return /* @__PURE__ */ React.createElement(
      "span",
      {
        key,
        ...commonProps,
        role: "link",
        tabIndex: 0,
        onKeyDown,
        onClick
      },
      element
    );
  } else if (getMentionLink) {
    return /* @__PURE__ */ React.createElement(
      "a",
      {
        key,
        ...commonProps,
        href: getMentionLink(mentionData),
        rel: "noopener noreferrer",
        tabIndex: 0
      },
      element
    );
  } else {
    return /* @__PURE__ */ React.createElement("span", { key, ...commonProps }, element);
  }
};
const MentionDecorator = {
  rule: (node) => !!node.textData?.decorations?.some(({ type }) => type === Decoration_Type.MENTION),
  decorate: ({ element, node, context }) => {
    const mentionDecoration = node.textData?.decorations?.find(
      ({ type }) => type === Decoration_Type.MENTION
    );
    return decorator(element, mentionDecoration, context);
  }
};
var mention_default = MentionDecorator;
export {
  mention_default as default
};
//# sourceMappingURL=mention.js.map
