"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 a_to_custom_link_exports = {};
__export(a_to_custom_link_exports, {
  aToCustomLink: () => aToCustomLink
});
module.exports = __toCommonJS(a_to_custom_link_exports);
var E = __toESM(require("fp-ts-esm/Either"));
var import_function = require("fp-ts-esm/function");
var import_Predicate = require("fp-ts-esm/Predicate");
var S = __toESM(require("fp-ts-esm/string"));
var import_v1 = require("./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_fp_utils = require("../../../../fp-utils.js");
var import_node_utils = require("../../../node-utils.js");
var import_parse5_utils = require("../core/parse5-utils.js");
var import_rules = require("../core/rules.js");
const parseNavigationData = (0, import_function.flow)(
  S.replace(/~#~/g, '"'),
  (d) => E.tryCatch(
    () => JSON.parse(d),
    () => ({ type: "pageLink", id: d })
  ),
  E.fold(import_function.identity, import_function.identity)
);
const toCustomData = (0, import_function.flow)(
  (0, import_fp_utils.getMatches)(/Wix\.(.+)\('(.+)'\)/),
  E.fromOption(() => "failed to parse custom link onclick"),
  E.map(([, , data]) => parseNavigationData(data)),
  E.map(JSON.stringify),
  E.fold(import_function.identity, import_function.identity)
);
const getData = (onclick) => onclick?.startsWith("Wix.") ? { customData: toCustomData(onclick) } : {};
const mergeData = (onclick) => (link) => ({ ...link, ...getData(onclick) });
const createCustomLink = ({ url, onclick, ...rest }) => (0, import_function.pipe)({ url, ...rest }, import_node_utils.createLink, mergeData(onclick));
const aToCustomLink = [
  (0, import_fp_utils.and)([
    import_rules.aToLink[0],
    (0, import_Predicate.not)((0, import_parse5_utils.hasParent)(import_parse5_utils.isRoot))
  ]),
  (context) => (node) => {
    const attrs = (0, import_parse5_utils.getAttributes)(node);
    return context.addDecoration(
      import_v1.Decoration_Type.LINK,
      { linkData: { link: createCustomLink({ ...attrs, url: attrs.href }) } },
      node
    );
  }
];
//# sourceMappingURL=a-to-custom-link.js.map
