"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 hashtag_decorator_exports = {};
__export(hashtag_decorator_exports, {
  nodeViewDecorator: () => nodeViewDecorator
});
module.exports = __toCommonJS(hashtag_decorator_exports);
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../../ricos-content/index.js");
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_const = require("../const.js");
var import_hashtag_scss = __toESM(require("../statics/hashtag-scss.js"));
const hasLink = (node) => {
  return node.textData?.decorations?.some(
    (decoration) => [import_v1.Decoration_Type.LINK, import_v1.Decoration_Type.ANCHOR, import_v1.Decoration_Type.MENTION].includes(
      decoration.type
    )
  );
};
const decorator = ({
  text,
  contextualData,
  node
}) => {
  const settings = contextualData.config[import_ricos_content.HASHTAG_TYPE] || {};
  const { createHref, onClick } = settings;
  const { anchorTarget = import_v1.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 === import_v1.Decoration_Type.COLOR
  );
  const foregroundColor = colorDecoration?.colorData?.foreground;
  return href ? /* @__PURE__ */ import_react.default.createElement(
    "a",
    {
      key,
      className: import_hashtag_scss.default.hashtag,
      href,
      target,
      onClick: decoratedOnClick,
      onKeyDown
    },
    /* @__PURE__ */ import_react.default.createElement(
      "span",
      {
        style: foregroundColor ? { color: foregroundColor, textDecoration: "inherit" } : void 0
      },
      text
    )
  ) : /* @__PURE__ */ import_react.default.createElement("span", { key, className: import_hashtag_scss.default.hashtag }, text);
};
const nodeViewDecorator = {
  rule: (node) => !!(node.textData?.text.match(import_const.hashtagRegex) && !hasLink(node)),
  decorate: ({ node, context }) => {
    const hashtags = [];
    const spans = [];
    const { text = "" } = node.textData || {};
    Array.from(text.matchAll(import_const.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__ */ import_react.default.createElement("span", { key: `${text}-${start}` }, text.substring(start, from)));
      }
      spans.push(
        /* @__PURE__ */ import_react.default.createElement(import_react.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__ */ import_react.default.createElement("span", { key: `${text}-${end}` }, text.substring(lastHashTag, end)));
    }
    return /* @__PURE__ */ import_react.default.createElement(import_react.Fragment, { key: "hastagContainer" }, spans);
  },
  isText: true
};
//# sourceMappingURL=hashtag-decorator.js.map
