"use strict";
/* Socket Lib - Built with esbuild */
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 editable_exports = {};
__export(editable_exports, {
  getEditablePackageJsonClass: () => getEditablePackageJsonClass,
  pkgJsonToEditable: () => pkgJsonToEditable,
  toEditablePackageJson: () => toEditablePackageJson,
  toEditablePackageJsonSync: () => toEditablePackageJsonSync
});
module.exports = __toCommonJS(editable_exports);
var import_package_json = __toESM(require("../external/@npmcli/package-json"));
var import_read_package = require("../external/@npmcli/package-json/lib/read-package");
var import_sort = require("../external/@npmcli/package-json/lib/sort");
var import_normalize = require("../paths/normalize");
var import_normalize2 = require("./normalize");
var import_packages = require("../paths/packages");
const identSymbol = Symbol.for("indent");
const newlineSymbol = Symbol.for("newline");
let _EditablePackageJsonClass;
let _fs;
// @__NO_SIDE_EFFECTS__
function getFs() {
  if (_fs === void 0) {
    _fs = require("node:fs");
  }
  return _fs;
}
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
  if (_path === void 0) {
    _path = require("node:path");
  }
  return _path;
}
let _util;
// @__NO_SIDE_EFFECTS__
function getUtil() {
  if (_util === void 0) {
    _util = require("node:util");
  }
  return _util;
}
// @__NO_SIDE_EFFECTS__
function getEditablePackageJsonClass() {
  if (_EditablePackageJsonClass === void 0) {
    _EditablePackageJsonClass = class EditablePackageJson extends import_package_json.default {
      static fixSteps = import_package_json.default.fixSteps;
      static normalizeSteps = import_package_json.default.normalizeSteps;
      static prepareSteps = import_package_json.default.prepareSteps;
      _canSave = true;
      _path = void 0;
      _readFileContent = "";
      _readFileJson = void 0;
      get content() {
        return super.content;
      }
      get filename() {
        const path = this._path;
        if (!path) {
          return "";
        }
        if (path.endsWith("package.json")) {
          return path;
        }
        const nodePath = /* @__PURE__ */ getPath();
        return nodePath.join(path, "package.json");
      }
      static async create(path, opts = {}) {
        const p = new _EditablePackageJsonClass();
        await p.create(path);
        return opts.data ? p.update(opts.data) : p;
      }
      static async fix(path, opts) {
        const p = new _EditablePackageJsonClass();
        await p.load(path, true);
        return await p.fix(opts);
      }
      static async load(path, opts = {}) {
        const p = new _EditablePackageJsonClass();
        if (!opts.create) {
          return await p.load(path);
        }
        try {
          return await p.load(path);
        } catch (err) {
          if (!err.message.startsWith("Could not read package.json")) {
            throw err;
          }
          return p.create(path);
        }
      }
      static async normalize(path, opts) {
        const p = new _EditablePackageJsonClass();
        await p.load(path);
        return await p.normalize(opts);
      }
      static async prepare(path, opts) {
        const p = new _EditablePackageJsonClass();
        await p.load(path, true);
        return await p.prepare(opts);
      }
      create(path) {
        super.create(path);
        this._path = path;
        return this;
      }
      async fix(opts = {}) {
        await super.fix(opts);
        return this;
      }
      fromContent(data) {
        super.fromContent(data);
        this._canSave = false;
        return this;
      }
      fromJSON(data) {
        super.fromJSON(data);
        return this;
      }
      async load(path, create) {
        this._path = path;
        const { promises: fsPromises } = /* @__PURE__ */ getFs();
        let parseErr;
        try {
          this._readFileContent = await (0, import_read_package.read)(this.filename);
        } catch (err) {
          if (!create) {
            throw err;
          }
          parseErr = err;
        }
        if (parseErr) {
          const nodePath = /* @__PURE__ */ getPath();
          const indexFile = nodePath.resolve(this.path || "", "index.js");
          let indexFileContent;
          try {
            indexFileContent = await fsPromises.readFile(indexFile, "utf8");
          } catch {
            throw parseErr;
          }
          try {
            this.fromContent(indexFileContent);
          } catch {
            throw parseErr;
          }
          this._canSave = false;
          return this;
        }
        this.fromJSON(this._readFileContent);
        this._readFileJson = (0, import_read_package.parse)(this._readFileContent);
        return this;
      }
      async normalize(opts = {}) {
        await super.normalize(opts);
        return this;
      }
      get path() {
        return this._path;
      }
      async prepare(opts = {}) {
        await super.prepare(opts);
        return this;
      }
      async save(options) {
        if (!this._canSave || this.content === void 0) {
          throw new Error("No package.json to save to");
        }
        const { ignoreWhitespace = false, sort = false } = {
          __proto__: null,
          ...options
        };
        const {
          [identSymbol]: indent,
          [newlineSymbol]: newline,
          ...rest
        } = this.content;
        const content = sort ? (0, import_sort.packageSort)(rest) : rest;
        const {
          [identSymbol]: _indent,
          [newlineSymbol]: _newline,
          ...origContent
        } = this._readFileJson || {};
        if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, origContent)) {
          return false;
        }
        const format = indent === void 0 || indent === null ? "  " : indent;
        const eol = newline === void 0 || newline === null ? "\n" : newline;
        const fileContent = `${JSON.stringify(
          content,
          void 0,
          format
        )}
`.replace(/\n/g, eol);
        if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
          return false;
        }
        const { promises: fsPromises } = /* @__PURE__ */ getFs();
        await fsPromises.writeFile(this.filename, fileContent);
        this._readFileContent = fileContent;
        this._readFileJson = (0, import_read_package.parse)(fileContent);
        return true;
      }
      saveSync(options) {
        if (!this._canSave || this.content === void 0) {
          throw new Error("No package.json to save to");
        }
        const { ignoreWhitespace = false, sort = false } = {
          __proto__: null,
          ...options
        };
        const {
          [Symbol.for("indent")]: indent,
          [Symbol.for("newline")]: newline,
          ...rest
        } = this.content;
        const content = sort ? (0, import_sort.packageSort)(rest) : rest;
        if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, this._readFileJson)) {
          return false;
        }
        const format = indent === void 0 || indent === null ? "  " : indent;
        const eol = newline === void 0 || newline === null ? "\n" : newline;
        const fileContent = `${JSON.stringify(
          content,
          void 0,
          format
        )}
`.replace(/\n/g, eol);
        if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
          return false;
        }
        const fs = /* @__PURE__ */ getFs();
        fs.writeFileSync(this.filename, fileContent);
        this._readFileContent = fileContent;
        this._readFileJson = (0, import_read_package.parse)(fileContent);
        return true;
      }
      update(content) {
        super.update(content);
        return this;
      }
      willSave(options) {
        const { ignoreWhitespace = false, sort = false } = {
          __proto__: null,
          ...options
        };
        if (!this._canSave || this.content === void 0) {
          return false;
        }
        const {
          [Symbol.for("indent")]: indent,
          [Symbol.for("newline")]: newline,
          ...rest
        } = this.content;
        const content = sort ? (0, import_sort.packageSort)(rest) : rest;
        if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, this._readFileJson)) {
          return false;
        }
        const format = indent === void 0 || indent === null ? "  " : indent;
        const eol = newline === void 0 || newline === null ? "\n" : newline;
        const fileContent = `${JSON.stringify(
          content,
          void 0,
          format
        )}
`.replace(/\n/g, eol);
        if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
          return false;
        }
        return true;
      }
    };
  }
  return _EditablePackageJsonClass;
}
// @__NO_SIDE_EFFECTS__
function pkgJsonToEditable(pkgJson, options) {
  const { normalize, ...normalizeOptions } = {
    __proto__: null,
    ...options
  };
  const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
  return new EditablePackageJson().fromContent(
    normalize ? (0, import_normalize2.normalizePackageJson)(pkgJson, normalizeOptions) : pkgJson
  );
}
// @__NO_SIDE_EFFECTS__
async function toEditablePackageJson(pkgJson, options) {
  const { path: filepath, ...pkgJsonToEditableOptions } = {
    __proto__: null,
    ...options
  };
  const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions;
  if (typeof filepath !== "string") {
    return /* @__PURE__ */ pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions);
  }
  const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
  const pkgJsonPath = (0, import_packages.resolvePackageJsonDirname)(filepath);
  return (await EditablePackageJson.load(pkgJsonPath, { create: true })).fromJSON(
    `${JSON.stringify(
      normalize ? (0, import_normalize2.normalizePackageJson)(pkgJson, {
        ...(0, import_normalize.isNodeModules)(pkgJsonPath) ? {} : { preserve: ["repository"] },
        ...normalizeOptions
      }) : pkgJson,
      null,
      2
    )}
`
  );
}
// @__NO_SIDE_EFFECTS__
function toEditablePackageJsonSync(pkgJson, options) {
  const { path: filepath, ...pkgJsonToEditableOptions } = {
    __proto__: null,
    ...options
  };
  const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions;
  if (typeof filepath !== "string") {
    return /* @__PURE__ */ pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions);
  }
  const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
  const pkgJsonPath = (0, import_packages.resolvePackageJsonDirname)(filepath);
  return new EditablePackageJson().create(pkgJsonPath).fromJSON(
    `${JSON.stringify(
      normalize ? (0, import_normalize2.normalizePackageJson)(pkgJson, {
        ...(0, import_normalize.isNodeModules)(pkgJsonPath) ? {} : { preserve: ["repository"] },
        ...normalizeOptions
      }) : pkgJson,
      null,
      2
    )}
`
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getEditablePackageJsonClass,
  pkgJsonToEditable,
  toEditablePackageJson,
  toEditablePackageJsonSync
});
