"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 parser_exports = {};
__export(parser_exports, {
  parser: () => parser
});
module.exports = __toCommonJS(parser_exports);
var A = __toESM(require("fp-ts-esm/Array"));
var import_function = require("fp-ts-esm/function");
var import_v1 = require("./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_node_utils = require("../../../node-utils.js");
var import_parse5_utils = require("./parse5-utils.js");
const contextOf = (rules, decorations, interceptorFn) => ({
  visit: visit(rules, decorations, interceptorFn),
  decorations: (0, import_node_utils.reduceDecorations)(decorations),
  addDecoration: addDecoration(rules, decorations, interceptorFn)
});
const htmlToNodes = (rules, decorations, interceptorFn) => (contentNode) => {
  const filteredRules = rules.filter(([_if]) => {
    try {
      return !!contentNode && _if(contentNode);
    } catch (e) {
      console.error(e, contentNode);
      return false;
    }
  });
  if (filteredRules.length === 0) {
    const context = contextOf(rules, decorations, interceptorFn);
    return interceptorFn({ contentNode, context, result: [] });
  }
  return filteredRules.flatMap(([, then]) => {
    const context = contextOf(rules, decorations, interceptorFn);
    const result = then(context)(contentNode);
    return interceptorFn({ contentNode, context, result });
  });
};
const addDecoration = (rules, decorations, interceptorFn) => (type, data = {}, element) => {
  const decoration = (0, import_node_utils.createDecoration)(type, data);
  return visit(rules, [decoration, ...decorations], interceptorFn)(element);
};
const visit = (rules, decorations, interceptorFn) => (element) => (0, import_function.pipe)(element, import_parse5_utils.getChildNodes, A.chain(htmlToNodes(rules, decorations, interceptorFn)));
const traverse = (rules, interceptorFn) => visit(rules, [], interceptorFn);
const toRichContent = (nodes) => ({ nodes, metadata: (0, import_node_utils.initializeMetadata)() });
const parser = (rules, interceptorFn) => (0, import_function.flow)(import_parse5_utils.toAst, traverse(rules, interceptorFn), toRichContent, import_v1.RichContent.fromJSON);
//# sourceMappingURL=parser.js.map
