// src/actions/deplint/checkPackage/getUnlistedDependencies.ts
import { builtinModules } from "module";
import chalk2 from "chalk";

// src/actions/deplint/packageJsonEditor.ts
import fs2 from "fs";
import chalk from "chalk";
import sortPackageJson from "sort-package-json";

// src/actions/deplint/getRequiredPeerDependencies.ts
import fs from "fs";
import path from "path";
function findDepPackageJson(location, dep) {
  let dir = location;
  while (true) {
    const candidate = path.join(dir, "node_modules", dep, "package.json");
    if (fs.existsSync(candidate)) return candidate;
    const parent = path.dirname(dir);
    if (parent === dir) return void 0;
    dir = parent;
  }
}

// src/actions/deplint/packageJsonEditor.ts
function readPackageJson(location) {
  return JSON.parse(fs2.readFileSync(`${location}/package.json`, "utf8"));
}
function writePackageJson(location, pkg) {
  const sorted = sortPackageJson(JSON.stringify(pkg, null, 2));
  fs2.writeFileSync(`${location}/package.json`, `${sorted}
`);
}
function addDep(pkg, dep, version, section) {
  const sectionObj = pkg[section] ?? {};
  sectionObj[dep] = version;
  pkg[section] = sectionObj;
}
function resolveDepVersion(location, dep) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (depPkgPath) {
    try {
      const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
      const version = depPkg.version;
      if (version) return `~${version}`;
    } catch {
    }
  }
  return "*";
}
function logFix(workspace, action, dep, detail) {
  const detailStr = detail ? ` (${detail})` : "";
  console.log(`[${chalk.blue(workspace.name)}] Fixed: ${action} ${chalk.green(dep)}${detailStr}`);
}

// src/actions/deplint/tsScriptsAliases.ts
var VARIANT_MAP = {
  "@xylabs/ts-scripts-yarn3": "@xylabs/ts-scripts-pnpm",
  "@xylabs/ts-scripts-pnpm": "@xylabs/ts-scripts-yarn3",
  "@xylabs/ts-scripts-react-yarn3": "@xylabs/ts-scripts-react-pnpm",
  "@xylabs/ts-scripts-react-pnpm": "@xylabs/ts-scripts-react-yarn3"
};
function isSatisfiedByTsScriptsVariant(imp, allDeps) {
  const variant = VARIANT_MAP[imp];
  return variant !== void 0 && allDeps.includes(variant);
}

// src/actions/deplint/checkPackage/getUnlistedDependencies.ts
function isRuntimeImportListed(imp, name, dependencies, peerDependencies) {
  return dependencies.includes(imp) || imp === name || peerDependencies.includes(imp) || builtinModules.includes(imp);
}
function isDistTypeImportListed(imp, name, dependencies, devDependencies, peerDependencies) {
  return dependencies.includes(imp) || imp === name || dependencies.includes(`@types/${imp}`) || devDependencies.includes(`@types/${imp}`) || peerDependencies.includes(imp) || peerDependencies.includes(`@types/${imp}`) || builtinModules.includes(imp);
}
function logMissing(name, imp, importPaths) {
  console.log(`[${chalk2.blue(name)}] Missing dependency in package.json: ${chalk2.red(imp)}`);
  if (importPaths[imp]) {
    console.log(`  ${importPaths[imp].join("\n ")}`);
  }
}
function shouldBePeerDependency(imp, _location, _currentPeers, _distImports, workspaceNames) {
  if (workspaceNames?.has(imp)) return false;
  if (imp.startsWith("@types/")) return false;
  return true;
}
function findUnlistedDependencies(name, dependencies, devDependencies, peerDependencies, externalDistImports, externalDistTypeImports) {
  const allDeps = [...dependencies, ...devDependencies, ...peerDependencies];
  const results = [];
  for (const imp of externalDistImports) {
    if (!isRuntimeImportListed(imp, name, dependencies, peerDependencies) && !isSatisfiedByTsScriptsVariant(imp, allDeps)) {
      results.push({ imp, section: "dependencies" });
    }
  }
  for (const imp of externalDistTypeImports) {
    if (!isDistTypeImportListed(imp, name, dependencies, devDependencies, peerDependencies) && !isSatisfiedByTsScriptsVariant(imp, allDeps)) {
      results.push({ imp, section: "dependencies" });
    }
  }
  return results;
}
function getUnlistedDependencies({ name, location }, {
  dependencies,
  devDependencies,
  peerDependencies
}, {
  externalDistImports,
  externalDistTypeImports,
  distImportPaths
}) {
  const unlisted = findUnlistedDependencies(name, dependencies, devDependencies, peerDependencies, externalDistImports, externalDistTypeImports);
  for (const { imp } of unlisted) {
    logMissing(name, imp, distImportPaths);
  }
  if (unlisted.length > 0) {
    console.log(`  ${chalk2.yellow(`${location}/package.json`)}
`);
  }
  return unlisted.length;
}
function fixUnlistedDependencies(workspace, {
  dependencies,
  devDependencies,
  peerDependencies
}, { externalDistImports, externalDistTypeImports }, workspaceNames) {
  const unlisted = findUnlistedDependencies(workspace.name, dependencies, devDependencies, peerDependencies, externalDistImports, externalDistTypeImports);
  if (unlisted.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  const isTerminal = pkg.private === true;
  for (const { imp, section } of unlisted) {
    const version = resolveDepVersion(workspace.location, imp);
    const isPeer = !isTerminal && section === "dependencies" && shouldBePeerDependency(imp, workspace.location, peerDependencies, externalDistImports, workspaceNames);
    if (isPeer) {
      addDep(pkg, imp, version, "peerDependencies");
      logFix(workspace, "added to peerDependencies", imp, version);
      addDep(pkg, imp, version, "devDependencies");
      logFix(workspace, "added to devDependencies", imp, version);
    } else {
      addDep(pkg, imp, version, section);
      logFix(workspace, `added to ${section}`, imp, version);
    }
  }
  writePackageJson(workspace.location, pkg);
  return unlisted.length;
}
export {
  findUnlistedDependencies,
  fixUnlistedDependencies,
  getUnlistedDependencies,
  shouldBePeerDependency
};
//# sourceMappingURL=getUnlistedDependencies.mjs.map