"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, {
  addToYarnIndex: () => addToYarnIndex,
  consumeDependenciesMeta: () => consumeDependenciesMeta,
  consumeDependencyList: () => consumeDependencyList,
  consumeEntryProperties: () => consumeEntryProperties,
  jsParseYarnLock: () => jsParseYarnLock,
  newEntry: () => newEntry,
  parseYarnLock: () => parseYarnLock,
  skipIndentedBlock: () => skipIndentedBlock,
  stripQuotes: () => stripQuotes,
  valueAfterKey: () => valueAfterKey
});
module.exports = __toCommonJS(parse_lockfile_exports);
var import_array = require("../../../../primordials/array");
var import_math = require("../../../../primordials/math");
var import_object = require("../../../../primordials/object");
var import_string = require("../../../../primordials/string");
var import_manifest = require("../../../../smol/manifest");
var import_parse_yarn_descriptor = require("./parse-yarn-descriptor");
function addToYarnIndex(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 consumeDependenciesMeta(content, startPos, _entry) {
  let pos = startPos;
  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);
    if (line.length < 4 || line[0] !== " " || line[1] !== " " || line[2] !== " " || line[3] !== " ") {
      break;
    }
    pos = end + 1;
  }
  return pos;
}
function consumeDependencyList(content, startPos, deps) {
  let pos = startPos;
  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);
    if (line.length < 4 || line[0] !== " " || line[1] !== " " || line[2] !== " " || line[3] !== " ") {
      break;
    }
    const depLine = (0, import_string.StringPrototypeTrim)(line);
    const colonIdx = (0, import_string.StringPrototypeIndexOf)(depLine, ":");
    const spaceIdx = (0, import_string.StringPrototypeIndexOf)(depLine, " ");
    const sepIdx = colonIdx === -1 ? spaceIdx : spaceIdx === -1 ? colonIdx : (0, import_math.MathMin)(colonIdx, spaceIdx);
    if (sepIdx > 0) {
      (0, import_array.ArrayPrototypePush)(
        deps,
        stripQuotes((0, import_string.StringPrototypeSlice)(depLine, 0, sepIdx))
      );
    }
    pos = end + 1;
  }
  return pos;
}
function consumeEntryProperties(content, startPos, entry) {
  let pos = startPos;
  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);
    if (line.length === 0 || line[0] !== " " && line[0] !== "	") {
      break;
    }
    const propLine = (0, import_string.StringPrototypeTrim)(line);
    pos = end + 1;
    if ((0, import_string.StringPrototypeIndexOf)(propLine, "version ") === 0 || (0, import_string.StringPrototypeIndexOf)(propLine, "version:") === 0) {
      entry.version = stripQuotes(valueAfterKey(propLine, "version ".length));
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "resolved ") === 0 || (0, import_string.StringPrototypeIndexOf)(propLine, "resolved:") === 0) {
      entry.resolved = stripQuotes(valueAfterKey(propLine, "resolved ".length));
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "integrity ") === 0 || (0, import_string.StringPrototypeIndexOf)(propLine, "integrity:") === 0) {
      entry.integrity = valueAfterKey(propLine, "integrity ".length);
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "checksum ") === 0 || (0, import_string.StringPrototypeIndexOf)(propLine, "checksum:") === 0) {
      entry.checksum = valueAfterKey(propLine, "checksum ".length);
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "linkType") === 0) {
      const colonIdx = (0, import_string.StringPrototypeIndexOf)(propLine, ":");
      if (colonIdx > 0) {
        entry.linkType = (0, import_string.StringPrototypeTrim)(
          (0, import_string.StringPrototypeSlice)(propLine, colonIdx + 1)
        );
      }
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "resolution") === 0) {
      const colonIdx = (0, import_string.StringPrototypeIndexOf)(propLine, ":");
      if (colonIdx > 0) {
        const resValue = stripQuotes(
          (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(propLine, colonIdx + 1))
        );
        if ((0, import_string.StringPrototypeIndexOf)(resValue, "http://") === 0 || (0, import_string.StringPrototypeIndexOf)(resValue, "https://") === 0) {
          entry.resolved = resValue;
        }
      }
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "dependencies:") === 0) {
      pos = consumeDependencyList(content, pos, entry.dependencies);
    } else if ((0, import_string.StringPrototypeIndexOf)(propLine, "dependenciesMeta:") === 0) {
      pos = consumeDependenciesMeta(content, pos, entry);
    }
  }
  return pos;
}
function jsParseYarnLock(content) {
  const packages = [];
  const packageIndex = {
    __proto__: null
  };
  const isBerry = (0, import_string.StringPrototypeIndexOf)(content, "__metadata:") !== -1;
  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;
    if (!line || (0, import_string.StringPrototypeTrim)(line) === "" || line[0] === "#") {
      continue;
    }
    if ((0, import_string.StringPrototypeTrim)(line) === "__metadata:") {
      pos = skipIndentedBlock(content, pos);
      continue;
    }
    if (line[0] === " " || line[0] === "	") {
      continue;
    }
    const trimmed = (0, import_string.StringPrototypeTrim)(line);
    if (!(0, import_string.StringPrototypeEndsWith)(trimmed, ":")) {
      continue;
    }
    const spec = (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(trimmed, 0, -1));
    let header = stripQuotes(spec);
    const commaIdx = (0, import_string.StringPrototypeIndexOf)(header, ",");
    if (commaIdx !== -1) {
      header = (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(header, 0, commaIdx));
    }
    if ((0, import_string.StringPrototypeIndexOf)(header, "@workspace:") !== -1) {
      pos = skipIndentedBlock(content, pos);
      continue;
    }
    const parsed = (0, import_parse_yarn_descriptor.parseYarnDescriptor)(header);
    const entry = newEntry(parsed.name);
    pos = consumeEntryProperties(content, pos, entry);
    if (isBerry && entry.linkType === "soft") {
      continue;
    }
    if (entry.name && entry.version) {
      const ref = (0, import_object.ObjectFreeze)({
        __proto__: null,
        name: entry.name,
        version: entry.version,
        resolved: entry.resolved,
        integrity: entry.integrity || entry.checksum || void 0,
        ecosystem: "npm",
        depType: "prod",
        isDev: false,
        isOptional: entry.isOptional,
        isPeer: false,
        isBundled: false,
        vcsUrl: void 0,
        vcsCommit: void 0,
        dependencies: entry.dependencies
      });
      (0, import_array.ArrayPrototypePush)(packages, ref);
      addToYarnIndex(packageIndex, entry.name, packages.length - 1);
    }
  }
  return (0, import_object.ObjectFreeze)({
    __proto__: null,
    type: "lockfile",
    lockVersion: isBerry ? "berry" : "1",
    ecosystem: "npm",
    packages: (0, import_object.ObjectFreeze)(packages),
    _index: packageIndex
  });
}
function newEntry(name) {
  return {
    name,
    version: void 0,
    resolved: void 0,
    integrity: void 0,
    checksum: void 0,
    dependencies: [],
    isOptional: false,
    linkType: void 0
  };
}
function skipIndentedBlock(content, startPos) {
  let pos = startPos;
  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);
    if (line.length === 0 || line[0] !== " " && line[0] !== "	") {
      return pos;
    }
    pos = end + 1;
  }
  return pos;
}
function stripQuotes(s) {
  if (s.length === 0) {
    return s;
  }
  let out = s;
  if ((0, import_string.StringPrototypeCharCodeAt)(out, 0) === 34) {
    out = (0, import_string.StringPrototypeSlice)(out, 1);
  }
  if (out.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(out, out.length - 1) === 34) {
    out = (0, import_string.StringPrototypeSlice)(out, 0, -1);
  }
  return out;
}
function valueAfterKey(line, keyLen) {
  return (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(line, keyLen));
}
const _smol = (0, import_manifest.getSmolManifest)();
const parseYarnLock = _smol ? (
  /* c8 ignore next 2 - smol Node binary only. */
  (content) => _smol.parseLockfile(content, "npm", "yarn")
) : jsParseYarnLock;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addToYarnIndex,
  consumeDependenciesMeta,
  consumeDependencyList,
  consumeEntryProperties,
  jsParseYarnLock,
  newEntry,
  parseYarnLock,
  skipIndentedBlock,
  stripQuotes,
  valueAfterKey
});
