import React, { Fragment } from "react";
import { HASHTAG_TYPE } from "./../../ricos-content/index.js";
import { Decoration_Type, Link_Target } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { hashtagRegex } from "../const.js";
import styles from "../statics/hashtag-scss.js";
const hasLink = (node) => {
  return node.textData?.decorations?.some(
    (decoration) => [Decoration_Type.LINK, Decoration_Type.ANCHOR, Decoration_Type.MENTION].includes(
      decoration.type
    )
  );
};
const decorator = ({
  text,
  contextualData,
  node
}) => {
  const settings = contextualData.config[HASHTAG_TYPE] || {};
  const { createHref, onClick } = settings;
  const { anchorTarget = Link_Target.SELF } = contextualData;
  const target = `_${anchorTarget?.toLowerCase?.()}`;
  const href = createHref ? createHref(text) : null;
  const decoratedOnClick = (e) => onClick(e, text);
  const onKeyDown = (e) => (e.key === "Enter" || e.key === " ") && onClick(e, text);
  const key = `hashtag-${text}`;
  const colorDecoration = node.textData?.decorations?.find(
    ({ type }) => type === Decoration_Type.COLOR
  );
  const foregroundColor = colorDecoration?.colorData?.foreground;
  return href ? /* @__PURE__ */ React.createElement(
    "a",
    {
      key,
      className: styles.hashtag,
      href,
      target,
      onClick: decoratedOnClick,
      onKeyDown
    },
    /* @__PURE__ */ React.createElement(
      "span",
      {
        style: foregroundColor ? { color: foregroundColor, textDecoration: "inherit" } : void 0
      },
      text
    )
  ) : /* @__PURE__ */ React.createElement("span", { key, className: styles.hashtag }, text);
};
const nodeViewDecorator = {
  rule: (node) => !!(node.textData?.text.match(hashtagRegex) && !hasLink(node)),
  decorate: ({ node, context }) => {
    const hashtags = [];
    const spans = [];
    const { text = "" } = node.textData || {};
    Array.from(text.matchAll(hashtagRegex)).forEach((match) => {
      const hash = match[0];
      let hashLength = hash.length;
      let from = match.index || 0;
      if (!hash.startsWith("#")) {
        from++;
        hashLength--;
      }
      const to = from + hashLength;
      hashtags.push([from, to]);
    });
    let start = 0;
    const end = text.length;
    hashtags.forEach(([from, to]) => {
      if (from > start) {
        spans.push(/* @__PURE__ */ React.createElement("span", { key: `${text}-${start}` }, text.substring(start, from)));
      }
      spans.push(
        /* @__PURE__ */ React.createElement(Fragment, { key: `${text}-${from}` }, decorator({ text: text.substring(from, to), contextualData: context, node }))
      );
      start = to;
    });
    const lastHashTag = hashtags[hashtags.length - 1][1];
    if (lastHashTag < end) {
      spans.push(/* @__PURE__ */ React.createElement("span", { key: `${text}-${end}` }, text.substring(lastHashTag, end)));
    }
    return /* @__PURE__ */ React.createElement(Fragment, { key: "hastagContainer" }, spans);
  },
  isText: true
};
export {
  nodeViewDecorator
};
//# sourceMappingURL=hashtag-decorator.js.map
