"use strict";
/* Socket Lib - Built with esbuild */
"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 lockfile_exports = {};
__export(lockfile_exports, {
  DEFAULT_MIN_RELEASE_DAYS: () => DEFAULT_MIN_RELEASE_DAYS,
  DlxLockfileError: () => DlxLockfileError,
  generatePackagePin: () => generatePackagePin,
  specName: () => specName,
  specRange: () => specRange
});
module.exports = __toCommonJS(lockfile_exports);
var import_node_os = __toESM(require("node:os"));
var import_pacote = __toESM(require("../external/pacote"));
var import_safe = require("../fs/safe");
var import_arborist = require("./arborist");
var import_integrity = require("../integrity");
var import_date = require("../primordials/date");
var import_json = require("../primordials/json");
var import_string = require("../primordials/string");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
const DEFAULT_MIN_RELEASE_DAYS = 7;
class DlxLockfileError extends Error {
  constructor(message, options) {
    super(message, options);
    this.name = "DlxLockfileError";
  }
}
async function generatePackagePin(options) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const { minReleaseDays, minReleaseMins, package: spec } = options;
  if (typeof spec !== "string" || spec.length === 0) {
    throw new DlxLockfileError("generatePackagePin requires a package spec");
  }
  if (minReleaseDays !== void 0 && minReleaseMins !== void 0) {
    throw new DlxLockfileError(
      "generatePackagePin: minReleaseDays and minReleaseMins are mutually exclusive"
    );
  }
  const effectiveDays = minReleaseDays !== void 0 ? minReleaseDays : minReleaseMins !== void 0 ? void 0 : DEFAULT_MIN_RELEASE_DAYS;
  const ageMs = effectiveDays !== void 0 ? effectiveDays * 864e5 : minReleaseMins !== void 0 ? minReleaseMins * 6e4 : 0;
  const before = ageMs > 0 ? new import_date.DateCtor((0, import_date.DateNow)() - ageMs) : void 0;
  const scratch = path.join(
    import_node_os.default.tmpdir(),
    `socket-lib-pin-${process.pid}-${Date.now()}`
  );
  await (0, import_safe.safeMkdir)(scratch, { recursive: true });
  try {
    const packageJson = (0, import_json.JSONStringify)(
      {
        name: "socket-lib-pin",
        version: "0.0.0",
        private: true,
        dependencies: { [specName(spec)]: specRange(spec) }
      },
      void 0,
      2
    );
    await fs.promises.writeFile(
      path.join(scratch, "package.json"),
      packageJson + "\n",
      "utf8"
    );
    await (0, import_arborist.writeSafeNpmrc)(scratch, {
      minReleaseDays: effectiveDays,
      minReleaseMins
    });
    const ideal = await (0, import_arborist.safeIdealTree)({ path: scratch, before });
    const tarball = await import_pacote.default.tarball(`${ideal.name}@${ideal.version}`);
    const hash = (0, import_integrity.computeHashes)(tarball);
    return {
      name: ideal.name,
      version: ideal.version,
      hash,
      packageJson,
      lockfile: ideal.lockfile
    };
  } finally {
    try {
      await (0, import_safe.safeDelete)(scratch, { force: true });
    } catch {
    }
  }
}
function specName(spec) {
  const atIdx = (0, import_string.StringPrototypeLastIndexOf)(spec, "@");
  if (atIdx <= 0) {
    return spec;
  }
  return (0, import_string.StringPrototypeSlice)(spec, 0, atIdx);
}
function specRange(spec) {
  const atIdx = (0, import_string.StringPrototypeLastIndexOf)(spec, "@");
  if (atIdx <= 0) {
    return "latest";
  }
  return (0, import_string.StringPrototypeSlice)(spec, atIdx + 1) || "latest";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_MIN_RELEASE_DAYS,
  DlxLockfileError,
  generatePackagePin,
  specName,
  specRange
});
