"use strict";
/* Socket Lib - Built with esbuild */
"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 parse_lockfile_exports = {};
__export(parse_lockfile_exports, {
  addToPnpmIndex: () => addToPnpmIndex,
  freezeEntry: () => freezeEntry,
  indentOf: () => indentOf,
  jsParsePnpmLock: () => jsParsePnpmLock,
  newPnpmEntry: () => newPnpmEntry,
  parsePnpmLock: () => parsePnpmLock,
  stripPeerSuffix: () => stripPeerSuffix
});
module.exports = __toCommonJS(parse_lockfile_exports);
var import_array = require("../../../primordials/array");
var import_object = require("../../../primordials/object");
var import_regexp = require("../../../primordials/regexp");
var import_string = require("../../../primordials/string");
var import_manifest = require("../../../smol/manifest");
var import_detect_pnpm_version = require("./detect-pnpm-version");
var import_parse_pnpm_package_id_v5 = require("./parse-pnpm-package-id-v5");
var import_parse_pnpm_package_id_v6_v9 = require("./parse-pnpm-package-id-v6-v9");
const RE_INTEGRITY = /integrity:\s*([a-zA-Z0-9+/=-]+)/;
const RE_TARBALL = /tarball:\s*['"]?([^'"}\s]+)['"]?/;
function addToPnpmIndex(packageIndex, name, idx) {
  const existing = packageIndex[name];
  if (existing === void 0) {
    packageIndex[name] = idx;
  } else if (typeof existing === "number") {
    packageIndex[name] = [existing, idx];
  } else {
    (0, import_array.ArrayPrototypePush)(existing, idx);
  }
}
function freezeEntry(entry) {
  const { _inDeps, ...rest } = entry;
  void _inDeps;
  return (0, import_object.ObjectFreeze)({
    __proto__: null,
    ...rest
  });
}
function indentOf(line) {
  let indent = 0;
  while (indent < line.length && (line[indent] === " " || line[indent] === "	")) {
    indent++;
  }
  return indent;
}
function jsParsePnpmLock(content) {
  const packages = [];
  const packageIndex = {
    __proto__: null
  };
  const lockVersion = (0, import_detect_pnpm_version.detectPnpmVersion)(content);
  let inPackages = false;
  let inSnapshots = false;
  let inImporters = false;
  let currentPkg;
  let currentIndent = 0;
  let currentImporter;
  let importerIndent = 0;
  let pos = 0;
  while (pos < content.length) {
    const eol = (0, import_string.StringPrototypeIndexOf)(content, "\n", pos);
    const end = eol === -1 ? content.length : eol;
    const line = (0, import_string.StringPrototypeSlice)(content, pos, end);
    pos = end + 1;
    const trimmed = (0, import_string.StringPrototypeTrim)(line);
    if (trimmed === "packages:") {
      inPackages = true;
      inSnapshots = false;
      inImporters = false;
      continue;
    }
    if (trimmed === "snapshots:") {
      inSnapshots = true;
      inPackages = false;
      inImporters = false;
      continue;
    }
    if (trimmed === "importers:") {
      inImporters = true;
      inPackages = false;
      inSnapshots = false;
      continue;
    }
    if (line[0] !== " " && line[0] !== "	" && trimmed.length > 0 && trimmed !== "packages:" && trimmed !== "snapshots:" && trimmed !== "importers:") {
      inPackages = false;
      inSnapshots = false;
      inImporters = false;
      continue;
    }
    if (inImporters) {
      const indent2 = indentOf(line);
      if (indent2 === 2 && (0, import_string.StringPrototypeEndsWith)(trimmed, ":")) {
        currentImporter = { section: void 0 };
        importerIndent = indent2;
        continue;
      }
      if (currentImporter && indent2 > importerIndent) {
        if ((0, import_string.StringPrototypeIndexOf)(trimmed, "devDependencies:") === 0) {
          currentImporter.section = "dev";
        } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "optionalDependencies:") === 0) {
          currentImporter.section = "optional";
        } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "dependencies:") === 0) {
          currentImporter.section = "prod";
        } else if (indent2 > importerIndent + 2) {
          if ((0, import_string.StringPrototypeIndexOf)(trimmed, "specifier:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "version:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "resolution:") === 0) {
            continue;
          }
          const colonIdx = (0, import_string.StringPrototypeIndexOf)(trimmed, ":");
          if (colonIdx > 0) {
            const depName = (0, import_string.StringPrototypeSlice)(trimmed, 0, colonIdx);
            const depVersion = (0, import_string.StringPrototypeTrim)(
              (0, import_string.StringPrototypeSlice)(trimmed, colonIdx + 1)
            );
            if (depVersion.length === 0 || (0, import_string.StringPrototypeIndexOf)(depVersion, "link:") === 0 || (0, import_string.StringPrototypeIndexOf)(depVersion, "workspace:") === 0 || (0, import_string.StringPrototypeIndexOf)(depVersion, "file:") === 0) {
              continue;
            }
            const versionWithoutPeer = stripPeerSuffix(depVersion);
            if (packageIndex[depName] === void 0) {
              const section = currentImporter.section;
              const ref = (0, import_object.ObjectFreeze)({
                __proto__: null,
                name: depName,
                version: versionWithoutPeer,
                resolved: void 0,
                integrity: void 0,
                ecosystem: "npm",
                depType: section === "dev" ? "dev" : section === "optional" ? "optional" : "prod",
                isDev: section === "dev",
                isOptional: section === "optional",
                isPeer: false,
                isBundled: false,
                vcsUrl: void 0,
                vcsCommit: void 0,
                dependencies: []
              });
              (0, import_array.ArrayPrototypePush)(packages, ref);
              packageIndex[depName] = packages.length - 1;
            }
          }
        }
      }
      continue;
    }
    if (!inPackages && !inSnapshots) {
      continue;
    }
    const indent = indentOf(line);
    const isPackageEntry = indent >= 2 && indent <= 4 && (0, import_string.StringPrototypeEndsWith)(trimmed, ":") && trimmed.length > 1 && (currentPkg === void 0 || indent <= currentIndent);
    if (isPackageEntry) {
      if (currentPkg && currentPkg.name) {
        const ref = freezeEntry(currentPkg);
        (0, import_array.ArrayPrototypePush)(packages, ref);
        addToPnpmIndex(packageIndex, currentPkg.name, packages.length - 1);
      }
      const key = (0, import_string.StringPrototypeSlice)(trimmed, 0, -1);
      const parsed = key[0] === "/" ? (0, import_parse_pnpm_package_id_v5.parsePnpmPackageIdV5)(key) : (0, import_parse_pnpm_package_id_v6_v9.parsePnpmPackageIdV6V9)(key);
      currentPkg = newPnpmEntry(parsed.name, parsed.version);
      currentIndent = indent;
      continue;
    }
    if (currentPkg && indent > currentIndent) {
      if ((0, import_string.StringPrototypeIndexOf)(trimmed, "dev:") === 0) {
        if ((0, import_string.StringPrototypeIndexOf)(trimmed, "true") !== -1) {
          currentPkg.depType = "dev";
          currentPkg.isDev = true;
        }
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "optional:") === 0) {
        if ((0, import_string.StringPrototypeIndexOf)(trimmed, "true") !== -1) {
          currentPkg.depType = "optional";
          currentPkg.isOptional = true;
        }
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "integrity:") === 0) {
        currentPkg.integrity = (0, import_string.StringPrototypeTrim)(
          (0, import_string.StringPrototypeSlice)(trimmed, 10)
        );
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "resolution:") === 0) {
        const intMatch = (0, import_regexp.RegExpPrototypeExec)(RE_INTEGRITY, trimmed);
        if (intMatch) {
          currentPkg.integrity = intMatch[1];
        }
        const tarballMatch = (0, import_regexp.RegExpPrototypeExec)(RE_TARBALL, trimmed);
        if (tarballMatch) {
          currentPkg.resolved = tarballMatch[1];
        }
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "dependencies:") === 0) {
        currentPkg.dependencies = [];
        currentPkg._inDeps = true;
      } else if (currentPkg._inDeps && ((0, import_string.StringPrototypeIndexOf)(trimmed, "peerDependencies:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "optionalDependencies:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "engines:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "os:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "cpu:") === 0 || (0, import_string.StringPrototypeIndexOf)(trimmed, "bin:") === 0)) {
        currentPkg._inDeps = false;
      } else if (currentPkg._inDeps && indent > currentIndent + 2) {
        const colonIdx = (0, import_string.StringPrototypeIndexOf)(trimmed, ":");
        if (colonIdx > 0) {
          (0, import_array.ArrayPrototypePush)(
            currentPkg.dependencies,
            (0, import_string.StringPrototypeSlice)(trimmed, 0, colonIdx)
          );
        }
      }
    }
  }
  if (currentPkg && currentPkg.name) {
    const ref = freezeEntry(currentPkg);
    (0, import_array.ArrayPrototypePush)(packages, ref);
    addToPnpmIndex(packageIndex, currentPkg.name, packages.length - 1);
  }
  return (0, import_object.ObjectFreeze)({
    __proto__: null,
    type: "lockfile",
    lockVersion: String(lockVersion),
    ecosystem: "npm",
    packages: (0, import_object.ObjectFreeze)(packages),
    _index: packageIndex
  });
}
function newPnpmEntry(name, version) {
  return {
    name,
    version,
    resolved: void 0,
    integrity: void 0,
    ecosystem: "npm",
    depType: "prod",
    isDev: false,
    isOptional: false,
    isPeer: false,
    isBundled: false,
    vcsUrl: void 0,
    vcsCommit: void 0,
    dependencies: [],
    _inDeps: false
  };
}
function stripPeerSuffix(version) {
  const underIdx = (0, import_string.StringPrototypeIndexOf)(version, "_");
  const noUnderscore = underIdx !== -1 ? (0, import_string.StringPrototypeSlice)(version, 0, underIdx) : version;
  const parenIdx = (0, import_string.StringPrototypeIndexOf)(noUnderscore, "(");
  return parenIdx !== -1 ? (0, import_string.StringPrototypeSlice)(noUnderscore, 0, parenIdx) : noUnderscore;
}
const _smol = (0, import_manifest.getSmolManifest)();
const parsePnpmLock = _smol ? (
  /* c8 ignore next 2 - smol Node binary only. */
  (content) => _smol.parseLockfile(content, "npm", "pnpm")
) : jsParsePnpmLock;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addToPnpmIndex,
  freezeEntry,
  indentOf,
  jsParsePnpmLock,
  newPnpmEntry,
  parsePnpmLock,
  stripPeerSuffix
});
