"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, {
  addToCargoIndex: () => addToCargoIndex,
  extractCargoDepName: () => extractCargoDepName,
  freezeCargoEntry: () => freezeCargoEntry,
  jsParseCargoLock: () => jsParseCargoLock,
  newCargoEntry: () => newCargoEntry,
  parseCargoGitSource: () => parseCargoGitSource,
  parseCargoLock: () => parseCargoLock,
  parseInlineArray: () => parseInlineArray,
  stripTomlString: () => stripTomlString,
  valueAfterEquals: () => valueAfterEquals
});
module.exports = __toCommonJS(parse_lockfile_exports);
var import_array = require("../../primordials/array");
var import_object = require("../../primordials/object");
var import_string = require("../../primordials/string");
function addToCargoIndex(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 extractCargoDepName(entry) {
  let s = entry;
  if (s.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(s, 0) === 34) {
    s = (0, import_string.StringPrototypeSlice)(s, 1);
  }
  if (s.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(s, s.length - 1) === 34) {
    s = (0, import_string.StringPrototypeSlice)(s, 0, -1);
  }
  const spaceIdx = (0, import_string.StringPrototypeIndexOf)(s, " ");
  if (spaceIdx === -1) {
    return s;
  }
  return (0, import_string.StringPrototypeSlice)(s, 0, spaceIdx);
}
function freezeCargoEntry(entry) {
  const git = entry.source !== void 0 ? parseCargoGitSource(entry.source) : void 0;
  return (0, import_object.ObjectFreeze)({
    __proto__: null,
    name: entry.name,
    version: entry.version,
    resolved: entry.source,
    integrity: entry.checksum,
    ecosystem: "cargo",
    depType: "prod",
    isDev: false,
    isOptional: false,
    isPeer: false,
    isBundled: false,
    vcsUrl: git?.url,
    vcsCommit: git?.commit,
    dependencies: entry.dependencies
  });
}
function jsParseCargoLock(content) {
  const packages = [];
  const packageIndex = {
    __proto__: null
  };
  let lockVersion = "1";
  let currentEntry;
  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.length === 0 || trimmed[0] === "#") {
      continue;
    }
    if (trimmed[0] === "[") {
      if (currentEntry && currentEntry.name) {
        const ref = freezeCargoEntry(currentEntry);
        (0, import_array.ArrayPrototypePush)(packages, ref);
        addToCargoIndex(packageIndex, currentEntry.name, packages.length - 1);
      }
      if (trimmed === "[[package]]") {
        currentEntry = newCargoEntry();
      } else {
        currentEntry = void 0;
      }
      continue;
    }
    if (currentEntry) {
      if (currentEntry.inDependencies) {
        if ((0, import_string.StringPrototypeIndexOf)(trimmed, "]") !== -1) {
          currentEntry.inDependencies = false;
          continue;
        }
        const noComma = trimmed[trimmed.length - 1] === "," ? (0, import_string.StringPrototypeSlice)(trimmed, 0, -1) : trimmed;
        const cleaned = extractCargoDepName(noComma);
        if (cleaned.length > 0) {
          (0, import_array.ArrayPrototypePush)(currentEntry.dependencies, cleaned);
        }
        continue;
      }
      if ((0, import_string.StringPrototypeIndexOf)(trimmed, "name") === 0) {
        currentEntry.name = stripTomlString(valueAfterEquals(trimmed));
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "version") === 0) {
        currentEntry.version = stripTomlString(valueAfterEquals(trimmed));
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "source") === 0) {
        currentEntry.source = stripTomlString(valueAfterEquals(trimmed));
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "checksum") === 0) {
        currentEntry.checksum = stripTomlString(valueAfterEquals(trimmed));
      } else if ((0, import_string.StringPrototypeIndexOf)(trimmed, "dependencies") === 0) {
        const value = valueAfterEquals(trimmed);
        if ((0, import_string.StringPrototypeIndexOf)(value, "[") !== -1 && (0, import_string.StringPrototypeIndexOf)(value, "]") !== -1) {
          const raw = parseInlineArray(value);
          for (let i = 0, { length } = raw; i < length; i++) {
            (0, import_array.ArrayPrototypePush)(
              currentEntry.dependencies,
              extractCargoDepName(raw[i])
            );
          }
        } else {
          currentEntry.inDependencies = true;
        }
      }
    } else {
      if ((0, import_string.StringPrototypeIndexOf)(trimmed, "version") === 0) {
        lockVersion = stripTomlString(valueAfterEquals(trimmed));
      }
    }
  }
  if (currentEntry && currentEntry.name) {
    const ref = freezeCargoEntry(currentEntry);
    (0, import_array.ArrayPrototypePush)(packages, ref);
    addToCargoIndex(packageIndex, currentEntry.name, packages.length - 1);
  }
  return (0, import_object.ObjectFreeze)({
    __proto__: null,
    type: "lockfile",
    lockVersion,
    ecosystem: "cargo",
    packages: (0, import_object.ObjectFreeze)(packages),
    _index: packageIndex
  });
}
function newCargoEntry() {
  return {
    name: "",
    version: "",
    source: void 0,
    checksum: void 0,
    dependencies: [],
    inDependencies: false
  };
}
function parseCargoGitSource(source) {
  if ((0, import_string.StringPrototypeIndexOf)(source, "git+") !== 0) {
    return void 0;
  }
  const hashIdx = (0, import_string.StringPrototypeIndexOf)(source, "#");
  if (hashIdx === -1) {
    return { url: source, commit: void 0 };
  }
  return {
    url: (0, import_string.StringPrototypeSlice)(source, 0, hashIdx),
    commit: (0, import_string.StringPrototypeSlice)(source, hashIdx + 1)
  };
}
function parseInlineArray(value) {
  const start = (0, import_string.StringPrototypeIndexOf)(value, "[");
  const end = (0, import_string.StringPrototypeIndexOf)(value, "]");
  if (start === -1 || end === -1 || end <= start) {
    return [];
  }
  const inner = (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(value, start + 1, end));
  if (inner.length === 0) {
    return [];
  }
  const result = [];
  let i = 0;
  while (i < inner.length) {
    while (i < inner.length && (inner[i] === " " || inner[i] === "	" || inner[i] === ",")) {
      i++;
    }
    if (i >= inner.length) {
      break;
    }
    if ((0, import_string.StringPrototypeCharCodeAt)(inner, i) === 34) {
      const closeIdx = (0, import_string.StringPrototypeIndexOf)(inner, '"', i + 1);
      if (closeIdx === -1) {
        break;
      }
      (0, import_array.ArrayPrototypePush)(result, (0, import_string.StringPrototypeSlice)(inner, i + 1, closeIdx));
      i = closeIdx + 1;
    } else {
      const commaIdx = (0, import_string.StringPrototypeIndexOf)(inner, ",", i);
      const next = commaIdx === -1 ? inner.length : commaIdx;
      (0, import_array.ArrayPrototypePush)(
        result,
        (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(inner, i, next))
      );
      i = next;
    }
  }
  return result;
}
function stripTomlString(value) {
  let s = (0, import_string.StringPrototypeTrim)(value);
  if (s.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(s, 0) === 34) {
    s = (0, import_string.StringPrototypeSlice)(s, 1);
  }
  if (s.length > 0 && (0, import_string.StringPrototypeCharCodeAt)(s, s.length - 1) === 34) {
    s = (0, import_string.StringPrototypeSlice)(s, 0, -1);
  }
  return s;
}
function valueAfterEquals(line) {
  const eq = (0, import_string.StringPrototypeIndexOf)(line, "=");
  if (eq === -1) {
    return "";
  }
  return (0, import_string.StringPrototypeTrim)((0, import_string.StringPrototypeSlice)(line, eq + 1));
}
const parseCargoLock = jsParseCargoLock;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addToCargoIndex,
  extractCargoDepName,
  freezeCargoEntry,
  jsParseCargoLock,
  newCargoEntry,
  parseCargoGitSource,
  parseCargoLock,
  parseInlineArray,
  stripTomlString,
  valueAfterEquals
});
