// src/actions/deplint/packageJsonEditor.ts
import fs from "node:fs";
import chalk from "chalk";
import semver from "semver";
import sortPackageJson from "sort-package-json";
function readPackageJson(location) {
  return JSON.parse(fs.readFileSync(`${location}/package.json`, "utf8"));
}
function writePackageJson(location, pkg) {
  const sorted = sortPackageJson(JSON.stringify(pkg, null, 2));
  fs.writeFileSync(`${location}/package.json`, `${sorted}
`);
}
function moveDep(pkg, dep, from, to) {
  const fromSection = pkg[from];
  if (!fromSection || !Object.hasOwn(fromSection, dep)) return;
  const version = fromSection[dep];
  delete fromSection[dep];
  if (Object.keys(fromSection).length === 0) {
    delete pkg[from];
  }
  const toSection = pkg[to] ?? {};
  toSection[dep] = version;
  pkg[to] = toSection;
  if (from === "peerDependencies" && to !== "peerDependencies") {
    removePeerMeta(pkg, dep);
  }
}
function removeDep(pkg, dep, section) {
  const sectionObj = pkg[section];
  if (!sectionObj || !Object.hasOwn(sectionObj, dep)) return;
  delete sectionObj[dep];
  if (Object.keys(sectionObj).length === 0) {
    delete pkg[section];
  }
  if (section === "peerDependencies") {
    removePeerMeta(pkg, dep);
  }
}
function addDep(pkg, dep, version, section) {
  const sectionObj = pkg[section] ?? {};
  sectionObj[dep] = version;
  pkg[section] = sectionObj;
}
function setPeerOptional(pkg, dep, optional) {
  if (optional === void 0) return;
  if (optional) {
    const meta2 = pkg.peerDependenciesMeta ?? {};
    const entry = meta2[dep] ?? {};
    entry.optional = true;
    meta2[dep] = entry;
    pkg.peerDependenciesMeta = meta2;
    return;
  }
  const meta = pkg.peerDependenciesMeta;
  if (!meta || !Object.hasOwn(meta, dep)) return;
  delete meta[dep].optional;
  if (Object.keys(meta[dep]).length === 0) {
    delete meta[dep];
  }
  if (Object.keys(meta).length === 0) {
    delete pkg.peerDependenciesMeta;
  }
}
function removePeerMeta(pkg, dep) {
  const meta = pkg.peerDependenciesMeta;
  if (!meta || !Object.hasOwn(meta, dep)) return;
  delete meta[dep];
  if (Object.keys(meta).length === 0) {
    delete pkg.peerDependenciesMeta;
  }
}

// src/actions/deplint/packageEditor.ts
var DeplintPackageEditor = class {
  dirty = false;
  pkg;
  workspace;
  constructor(workspace) {
    this.workspace = workspace;
    this.pkg = readPackageJson(workspace.location);
  }
  applyFix(fix) {
    const before = JSON.stringify(this.pkg);
    switch (fix.action) {
      case "add": {
        addDep(this.pkg, fix.dependency, fix.version, fix.section);
        if (fix.section === "peerDependencies") {
          setPeerOptional(this.pkg, fix.dependency, fix.optional);
        }
        break;
      }
      case "move": {
        moveDep(this.pkg, fix.dependency, fix.fromSection, fix.toSection);
        if (fix.version !== void 0 && this.pkg[fix.toSection]?.[fix.dependency] !== void 0) {
          this.pkg[fix.toSection][fix.dependency] = fix.version;
        }
        if (fix.toSection === "peerDependencies") {
          setPeerOptional(this.pkg, fix.dependency, fix.optional);
        }
        break;
      }
      case "remove": {
        removeDep(this.pkg, fix.dependency, fix.section);
        break;
      }
      case "set-version": {
        const section = this.pkg[fix.section];
        if (section?.[fix.dependency] !== void 0) {
          addDep(this.pkg, fix.dependency, fix.version, fix.section);
        }
        break;
      }
      case "resolve-peer-bump": {
        break;
      }
    }
    if (JSON.stringify(this.pkg) === before) return void 0;
    this.dirty = true;
    return {
      dependency: fix.dependency,
      detail: fix.detail,
      workspace: this.workspace
    };
  }
  save() {
    if (!this.dirty) return;
    writePackageJson(this.workspace.location, this.pkg);
  }
};

// src/actions/deplint/fixer.ts
function applyDeplintFixes(diagnostics) {
  const editors = /* @__PURE__ */ new Map();
  const applied = [];
  for (const diagnostic of diagnostics) {
    const fixes = diagnostic.fixes ?? [];
    for (const fix of fixes) {
      const workspaceKey = diagnostic.workspace.location;
      let editor = editors.get(workspaceKey);
      if (!editor) {
        editor = new DeplintPackageEditor(diagnostic.workspace);
        editors.set(workspaceKey, editor);
      }
      const appliedFix = editor.applyFix(fix);
      if (appliedFix) applied.push(appliedFix);
    }
  }
  for (const editor of editors.values()) {
    editor.save();
  }
  return applied;
}
export {
  applyDeplintFixes
};
//# sourceMappingURL=fixer.mjs.map
