import classNames from "classnames";
import React, { useContext } from "react";
import { RicosViewerContext } from "./../../ricos-context/viewer/index.js";
import { Decoration_Type, Node_Type } from "./../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { getLinkHrefUrl } from "./../../_shared/link/get-link-href-url.js";
import { WEB_LINK } from "../data-hooks.js";
import styles from "../statics/link-viewer-scss.js";
import { LINK_TYPE } from "../types.js";
const decorator = (element, linkDecoration, contextualData, node) => {
  const { baseUrl } = useContext(RicosViewerContext);
  const link = linkDecoration.linkData?.link;
  const target = link?.target || contextualData.anchorTarget;
  const rel = link?.rel || {};
  const relValue = [
    "noopener",
    rel.nofollow ? "nofollow" : null,
    rel.sponsored ? "sponsored" : null,
    rel.ugc ? "ugc" : null,
    rel.noreferrer ? "noreferrer" : null
  ].filter(Boolean).join(" ");
  const onClick = () => contextualData.helpers.onViewerAction?.(LINK_TYPE, "Click", "", node.id, {
    type: Node_Type.TEXT,
    textData: node.textData
  });
  return /* @__PURE__ */ React.createElement(
    "a",
    {
      key: `link=${element.key}`,
      target: `_${target?.toLowerCase?.()}`,
      href: link ? getLinkHrefUrl(link, baseUrl) : void 0,
      rel: relValue,
      className: classNames(styles.link, styles.linkInViewer),
      "data-hook": WEB_LINK,
      onClick
    },
    element
  );
};
const LinkDecorator = {
  rule: (node) => !!node.textData?.decorations?.some(({ type }) => type === Decoration_Type.LINK),
  decorate: ({ element, node, context }) => {
    const linkDecoration = node.textData?.decorations?.find(
      ({ type }) => type === Decoration_Type.LINK
    );
    return decorator(element, linkDecoration, context, node);
  },
  priority: 1e3
};
var link_default = LinkDecorator;
export {
  link_default as default
};
//# sourceMappingURL=link.js.map
