"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 modifier_infra_exports = {};
__export(modifier_infra_exports, {
  getModifier: () => getModifier
});
module.exports = __toCommonJS(modifier_infra_exports);
var A = __toESM(require("fp-ts-esm/Array"));
var import_function = require("fp-ts-esm/function");
var T = __toESM(require("fp-ts-esm/Tree"));
var import_monocle_ts_esm = require("monocle-ts-esm");
var import_generate_random_id = require("./converters/generate-random-id.js");
const getModifier = (nodesAccessor, nodesSetter) => (root) => {
  class TraversalModifier {
    constructor(tree2, traversal) {
      this.tree = tree2;
      this.traversal = traversal;
    }
    filter(predicate) {
      return new TraversalModifier(
        this.tree,
        this.traversal.composePrism(import_monocle_ts_esm.Prism.fromPredicate(predicate))
      );
    }
    set(setter) {
      return (0, import_function.pipe)(
        // @ts-ignore $TsFixMe
        this.traversal.asFold().getAll(this.tree),
        A.map((node) => node._modId),
        foldTree(this.tree, setter)
      );
    }
  }
  const modifyById = (idsToSet, setter) => (modTreeNode) => {
    const { _modId, ...rest } = modTreeNode;
    const node = rest;
    return idsToSet.includes(_modId) ? setter(node) : node;
  };
  const mergeWith = (prefix) => (suffix) => [...prefix, ...suffix];
  const toArray = (node) => Array.isArray(node) ? node : A.of(node);
  const foldTree = (tree2, setter) => (idsToSet) => T.fold((root2, forest) => ({
    ...root2,
    ...nodesSetter(
      forest.reduce(
        (modifiedForest, node) => (
          // @ts-ignore $TsFixMe
          (0, import_function.pipe)(node, modifyById(idsToSet, setter), toArray, mergeWith(modifiedForest))
        ),
        []
      )
    )
  }))(tree2);
  const tree = T.unfoldTree(root, (node) => [
    { ...node, _modId: (0, import_generate_random_id.generateId)() },
    nodesAccessor(node)
  ]);
  return new TraversalModifier(
    tree,
    // @ts-ignore $TsFixMe
    (0, import_monocle_ts_esm.fromTraversable)(T.tree)()
  );
};
//# sourceMappingURL=modifier-infra.js.map
