"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 parse5_utils_exports = {};
__export(parse5_utils_exports, {
  appendChild: () => appendChild,
  getAttributes: () => getAttributes,
  getChildNodes: () => getChildNodes,
  getClassNames: () => getClassNames,
  getStyle: () => getStyle,
  hasAttribute: () => hasAttribute,
  hasChild: () => hasChild,
  hasClass: () => hasClass,
  hasDescendant: () => hasDescendant,
  hasParent: () => hasParent,
  hasStyleFor: () => hasStyleFor,
  hasStyleRule: () => hasStyleRule,
  hasTag: () => hasTag,
  isRoot: () => isRoot,
  isText: () => isText,
  isWhitespace: () => isWhitespace,
  oneOf: () => oneOf,
  toAst: () => toAst,
  toDocumentFragment: () => toDocumentFragment,
  toName: () => toName
});
module.exports = __toCommonJS(parse5_utils_exports);
var A = __toESM(require("fp-ts-esm/Array"));
var import_boolean = require("fp-ts-esm/boolean");
var import_function = require("fp-ts-esm/function");
var import_Monoid = require("fp-ts-esm/Monoid");
var O = __toESM(require("fp-ts-esm/Option"));
var RONEA = __toESM(require("fp-ts-esm/ReadonlyNonEmptyArray"));
var R = __toESM(require("fp-ts-esm/Record"));
var S = __toESM(require("fp-ts-esm/string"));
var import_parse5 = require("parse5");
var import_fp_utils = require("../../../../fp-utils.js");
const isText = (node) => node.nodeName === "#text";
const isComment = (node) => node.nodeName === "#comment";
const isLeaf = (node) => isText(node) || isComment(node) || node.childNodes.length === 0;
const isWhitespace = (0, import_function.flow)(
  (n) => O.fromNullable(isText(n) ? n.value : null),
  O.map(S.trim),
  O.fold(() => false, (0, import_fp_utils.equals)(S.Eq)(""))
);
const hasDescendant = (predicate) => (node) => predicate(node) || !isLeaf(node) && (0, import_function.pipe)(node.childNodes, A.map(hasDescendant(predicate)), (0, import_Monoid.concatAll)(import_boolean.MonoidAny));
const toRecord = A.reduce(
  {},
  (rec, { name, value }) => ({
    ...rec,
    [name]: value
  })
);
const getAttributes = (el) => (0, import_function.pipe)(
  el,
  (el2) => el2.attrs ? O.some(el2.attrs) : O.none,
  O.fold(() => ({}), toRecord)
);
const hasAttribute = (key, valuePredicate) => (0, import_function.flow)(getAttributes, R.lookup(key), O.fold(import_function.constFalse, valuePredicate));
const getChildNodes = (element) => isLeaf(element) ? [] : element.childNodes;
const appendChild = (element, node) => {
  node.parentNode = element;
  element.childNodes.push(node);
};
const isRoot = (node) => node.nodeName === "#document-fragment";
const toDocumentFragment = (nodes) => {
  const fragment = {
    nodeName: "#document-fragment",
    childNodes: []
  };
  fragment.childNodes = nodes.map((n) => ({ ...n, parentNode: fragment }));
  return fragment;
};
const toName = (node) => node.nodeName;
const hasParent = (predicate) => (node) => predicate(node.parentNode);
const hasTag = (tag) => (0, import_function.flow)(toName, (0, import_fp_utils.equals)(S.Eq)(tag));
const toStyle = (0, import_function.flow)(
  S.split(";"),
  RONEA.map((0, import_function.flow)(S.split(":"), RONEA.map(S.trim))),
  RONEA.reduce(
    {},
    (style, rule) => ({
      ...style,
      [rule[0]]: rule[1]
    })
  )
);
const getStyle = (0, import_function.flow)(getAttributes, R.lookup("style"), O.map(toStyle));
const hasStyleFor = (styleRule) => (0, import_function.flow)(
  getStyle,
  O.fold(
    () => false,
    (s) => R.has(styleRule, s)
  )
);
const hasStyleRule = (style) => (0, import_function.flow)(
  getStyle,
  O.fold(
    () => false,
    (s) => R.isSubrecord(S.Eq)(style, s)
  )
);
const getClassNames = (0, import_function.flow)(
  getAttributes,
  R.lookup("class"),
  O.fold(() => RONEA.of(""), S.split(" "))
);
const hasClass = (predicate) => (0, import_function.flow)(getClassNames, RONEA.map(predicate), (0, import_Monoid.concatAll)(import_boolean.MonoidAny));
const hasChild = (predicate) => (0, import_function.flow)(getChildNodes, A.map(predicate), (0, import_Monoid.concatAll)(import_boolean.MonoidAny));
const oneOf = (tags) => (node) => (0, import_function.pipe)(tags, A.map((0, import_fp_utils.equals)(S.Eq)(node.nodeName)), (0, import_Monoid.concatAll)(import_boolean.MonoidAny));
const toAst = (html) => (0, import_parse5.parseFragment)(html);
//# sourceMappingURL=parse5-utils.js.map
