"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var edit_exports = {};
__export(edit_exports, {
  edit: () => edit
});
module.exports = __toCommonJS(edit_exports);
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var edit;
((edit2) => {
  function sed(...scripts) {
    const script = scripts.join(";").replaceAll("\n", "\\n");
    return (container, paths) => container.exec(["sed", "-iz", script, ...paths]);
  }
  edit2.sed = sed;
  function insert(...edits) {
    return async (Container, paths) => {
      for (const path of paths) {
        await insertLinesInto(Container, path, edits);
      }
    };
  }
  edit2.insert = insert;
  function region(...edits) {
    return async (container, paths) => {
      for (const path of paths) {
        await editRegionsOf(container, path, edits);
      }
    };
  }
  edit2.region = region;
  function js(edit3) {
    return async (container, paths) => {
      for (const path of paths) {
        await container.write(path, edit3(await container.read(path)));
      }
    };
  }
  edit2.js = js;
})(edit || (edit = {}));
async function insertLinesInto(container, path, edits) {
  const editors = insertionEditorsOf(edits);
  const input = (await container.read(path)).split("\n");
  const output = Array();
  for (const line of input) {
    for (const editor of editors) {
      if (editor.before?.(line)) {
        output.push(...editor.lines(line));
      }
      output.push(line);
      if (editor.after?.(line)) {
        output.push(...editor.lines(line));
      }
    }
  }
  await container.write(path, output.join("\n"));
}
function insertionEditorsOf(edits) {
  return edits.map(({ before, after, lines }) => ({
    before: detectorOf(before),
    after: detectorOf(after),
    lines: replacementOf(lines)
  }));
}
async function editRegionsOf(container, path, edits) {
  const editors = regionEditorsOf(edits);
  let editor;
  const input = (await container.read(path)).split("\n");
  const output = Array();
  for (const line of input) {
    if (editor) {
      if (editor.collect(line, output)) {
        continue;
      }
      editor = void 0;
    }
    for (const editorCandidate of editors) {
      if (editorCandidate.detect(line, output)) {
        editor = editorCandidate;
      }
    }
    if (!editor) {
      output.push(line);
    }
  }
  editor?.finish(output);
  await container.write(path, output.join("\n"));
}
function regionEditorsOf(edits) {
  return edits.map(({ from, to, after, before, replacement }) => {
    let collecting = from === void 0 && after === void 0;
    from = detectorOf(from);
    to = detectorOf(to);
    after = detectorOf(after);
    before = detectorOf(before);
    replacement = replacementOf(replacement);
    const collected = Array();
    return {
      detect(line, output) {
        if (from?.(line)) {
          collecting = true;
          return true;
        }
        if (after?.(line)) {
          collecting = true;
          output.push(line);
          return true;
        }
        return false;
      },
      collect(line, output) {
        if (collecting) {
          if (to?.(line)) {
            output.push(...replacement(...collected));
            collected.length = 0;
            collecting = false;
            return true;
          }
          if (before?.(line)) {
            output.push(...replacement(...collected));
            collected.length = 0;
            collecting = false;
            return false;
          }
          return true;
        }
        return false;
      },
      finish(output) {
        if (collecting && to === void 0 && before === void 0) {
          output.push(...replacement(...collected));
        }
      }
    };
  });
}
function detectorOf(detector) {
  if (detector === void 0) {
    return () => false;
  }
  if (typeof detector === "string") {
    return (line) => line === detector;
  }
  if (detector instanceof RegExp) {
    return (line) => !!line.match(detector);
  }
  return detector;
}
function replacementOf(replacement) {
  if (replacement === void 0) {
    return () => [];
  }
  if (typeof replacement === "string") {
    if (replacement[0] === "\n") {
      replacement = replacement.slice(1);
    }
    return () => [replacement];
  }
  if (Array.isArray(replacement)) {
    return () => replacement;
  }
  return replacement;
}
//# sourceMappingURL=edit.js.map
