"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 cargo_exports = {};
__export(cargo_exports, {
  cargoParser: () => cargoParser
});
module.exports = __toCommonJS(cargo_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
const cargoParser = {
  name: "cargo",
  manifestFiles: ["Cargo.toml"],
  language: "rust",
  canParse(manifestPath) {
    return path.basename(manifestPath) === "Cargo.toml";
  },
  async parse(manifestPath, projectRoot) {
    let content;
    try {
      content = fs.readFileSync(manifestPath, "utf8");
    } catch {
      return [];
    }
    const nameMatch = content.match(/^\[package\][^[]*name\s*=\s*"([^"]+)"/ms);
    const versionMatch = content.match(/^\[package\][^[]*version\s*=\s*"([^"]+)"/ms);
    const name = nameMatch ? nameMatch[1] : path.basename(path.dirname(manifestPath));
    const version = versionMatch ? versionMatch[1] : void 0;
    const externalDeps = [];
    const depsMatch = content.match(/^\[dependencies\]([\s\S]*?)(?=^\[|\Z)/m);
    if (depsMatch) {
      for (const line of depsMatch[1].split("\n")) {
        const m = line.trim().match(/^([a-zA-Z0-9_-]+)\s*=/);
        if (m) externalDeps.push(m[1]);
      }
    }
    const relDir = path.relative(projectRoot, path.dirname(manifestPath)).replace(/\\/g, "/") || ".";
    const relManifest = path.relative(projectRoot, manifestPath).replace(/\\/g, "/");
    const packages = [{
      id: `pkg:cargo:${relDir}`,
      name,
      path: relDir,
      source: "manifest",
      language: "rust",
      manifestPath: relManifest,
      version,
      externalDeps,
      updatedAt: Date.now()
    }];
    const workspaceMatch = content.match(/^\[workspace\][^[]*members\s*=\s*\[([^\]]+)\]/ms);
    if (workspaceMatch) {
      const members = workspaceMatch[1].match(/"([^"]+)"/g)?.map((s) => s.replace(/"/g, "")) ?? [];
      for (const member of members) {
        const memberDir = path.join(path.dirname(manifestPath), member);
        const memberCargo = path.join(memberDir, "Cargo.toml");
        if (fs.existsSync(memberCargo)) {
          const sub = await this.parse(memberCargo, projectRoot);
          packages.push(...sub);
        }
      }
    }
    return packages;
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  cargoParser
});
//# sourceMappingURL=cargo.js.map
