// src/lib/dependencies/DuplicateDetector.ts
import { EOL } from "node:os";
import chalk from "chalk";

// src/lib/jsonFormatters.ts
var multiLineToJSONArray = (output) => {
  const withCommas = output.replaceAll("\r\n", "").replaceAll("\n", ",");
  const cleanCollection = withCommas.slice(0, Math.max(0, withCommas.length - 1));
  const collection = `[${cleanCollection}]`;
  return JSON.parse(collection);
};

// src/lib/dependencies/DuplicateDetector.ts
var trimVirtualMeta = (value) => {
  const virtualParts = value.split("virtual:");
  if (virtualParts.length > 1) {
    const hashParts = virtualParts[1].split("#");
    return virtualParts[0] + hashParts[1];
  } else {
    return value;
  }
};
var trimObjectDependencyVirtualMeta = (obj) => {
  const resultObj = {};
  for (const [key, value] of Object.entries(obj)) {
    resultObj[trimVirtualMeta(key)] = {
      descriptor: trimVirtualMeta(value.descriptor),
      locator: trimVirtualMeta(value.locator)
    };
  }
  return resultObj;
};
var trimDependencyVirtualMeta = (dependencies) => {
  return dependencies.map((dependency) => {
    return { children: trimObjectDependencyVirtualMeta(dependency.children), value: trimVirtualMeta(dependency.value) };
  });
};
function extractPnpmVersions(data, depName) {
  const versions = /* @__PURE__ */ new Set();
  function walk(obj) {
    if (Array.isArray(obj)) {
      for (const item of obj) walk(item);
    } else if (obj && typeof obj === "object") {
      const record = obj;
      if (typeof record.version === "string" && typeof record.from === "string" && record.from === depName) {
        versions.add(record.version);
      }
      for (const val of Object.values(record)) walk(val);
    }
  }
  walk(data);
  return [...versions];
}
var DuplicateDetector = class {
  dependency;
  duplicateVersions;
  constructor(output, dependency) {
    this.dependency = dependency;
    this.duplicateVersions = this.parseVersions(output, dependency);
  }
  detect() {
    if (this.duplicateVersions.length > 1) {
      console.log(chalk.yellow(`${EOL}Duplicates found for: ${this.dependency}`));
      const duplicateVersions = this.duplicateVersions.join(`${EOL}  `);
      console.log(chalk.grey(`  ${duplicateVersions}`, EOL));
      return 1;
    } else {
      console.log(`${this.dependency} - OK`);
      return 0;
    }
  }
  parseVersions(output, dependency) {
    try {
      const parsed = JSON.parse(output);
      if (Array.isArray(parsed)) {
        return extractPnpmVersions(parsed, dependency);
      }
    } catch {
    }
    try {
      const entries = trimDependencyVirtualMeta(multiLineToJSONArray(output));
      const versions = /* @__PURE__ */ new Set();
      for (const entry of entries) {
        for (const key of Object.keys(entry.children)) {
          const version = key.includes("@virtual:") ? void 0 : key;
          if (version) versions.add(version);
        }
      }
      return [...versions];
    } catch {
      console.warn(chalk.yellow(`Could not parse dependency output for ${dependency}`));
      return [];
    }
  }
};
export {
  DuplicateDetector
};
//# sourceMappingURL=DuplicateDetector.mjs.map
