import * as A from "fp-ts-esm/Array";
import { MonoidAny } from "fp-ts-esm/boolean";
import { constFalse, flow, pipe } from "fp-ts-esm/function";
import { concatAll } from "fp-ts-esm/Monoid";
import * as O from "fp-ts-esm/Option";
import * as RONEA from "fp-ts-esm/ReadonlyNonEmptyArray";
import * as R from "fp-ts-esm/Record";
import * as S from "fp-ts-esm/string";
import { parseFragment } from "parse5";
import { equals } from "../../../../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 = flow(
  (n) => O.fromNullable(isText(n) ? n.value : null),
  O.map(S.trim),
  O.fold(() => false, equals(S.Eq)(""))
);
const hasDescendant = (predicate) => (node) => predicate(node) || !isLeaf(node) && pipe(node.childNodes, A.map(hasDescendant(predicate)), concatAll(MonoidAny));
const toRecord = A.reduce(
  {},
  (rec, { name, value }) => ({
    ...rec,
    [name]: value
  })
);
const getAttributes = (el) => pipe(
  el,
  (el2) => el2.attrs ? O.some(el2.attrs) : O.none,
  O.fold(() => ({}), toRecord)
);
const hasAttribute = (key, valuePredicate) => flow(getAttributes, R.lookup(key), O.fold(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) => flow(toName, equals(S.Eq)(tag));
const toStyle = flow(
  S.split(";"),
  RONEA.map(flow(S.split(":"), RONEA.map(S.trim))),
  RONEA.reduce(
    {},
    (style, rule) => ({
      ...style,
      [rule[0]]: rule[1]
    })
  )
);
const getStyle = flow(getAttributes, R.lookup("style"), O.map(toStyle));
const hasStyleFor = (styleRule) => flow(
  getStyle,
  O.fold(
    () => false,
    (s) => R.has(styleRule, s)
  )
);
const hasStyleRule = (style) => flow(
  getStyle,
  O.fold(
    () => false,
    (s) => R.isSubrecord(S.Eq)(style, s)
  )
);
const getClassNames = flow(
  getAttributes,
  R.lookup("class"),
  O.fold(() => RONEA.of(""), S.split(" "))
);
const hasClass = (predicate) => flow(getClassNames, RONEA.map(predicate), concatAll(MonoidAny));
const hasChild = (predicate) => flow(getChildNodes, A.map(predicate), concatAll(MonoidAny));
const oneOf = (tags) => (node) => pipe(tags, A.map(equals(S.Eq)(node.nodeName)), concatAll(MonoidAny));
const toAst = (html) => parseFragment(html);
export {
  appendChild,
  getAttributes,
  getChildNodes,
  getClassNames,
  getStyle,
  hasAttribute,
  hasChild,
  hasClass,
  hasDescendant,
  hasParent,
  hasStyleFor,
  hasStyleRule,
  hasTag,
  isRoot,
  isText,
  isWhitespace,
  oneOf,
  toAst,
  toDocumentFragment,
  toName
};
//# sourceMappingURL=parse5-utils.js.map
