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

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

// src/actions/deplint/getRequiredPeerDependencies.ts
import fs from "node:fs";
import path from "node: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 readInstalledVersion(location, dep) {
  const depPkgPath = findDepPackageJson(location, dep);
  if (!depPkgPath) return void 0;
  try {
    const depPkg = JSON.parse(fs2.readFileSync(depPkgPath, "utf8"));
    return depPkg.version;
  } catch {
    return void 0;
  }
}
function resolveDepVersion(location, dep) {
  const version = readInstalledVersion(location, dep);
  return version ? `~${version}` : "*";
}
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/toolchain": "@xylabs/toolchain",
  "@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/getUnlistedDevDependencies.ts
function findUnlistedDevDeps(name, dependencies, devDependencies, peerDependencies, externalAllImports, distImports, externalDistTypeImports) {
  const results = [];
  for (const imp of externalAllImports) {
    if (!distImports.includes(imp) && !externalDistTypeImports.includes(imp) && imp !== name && !dependencies.includes(imp) && !dependencies.includes(`@types/${imp}`) && !peerDependencies.includes(imp) && !peerDependencies.includes(`@types/${imp}`) && !devDependencies.includes(imp) && !devDependencies.includes(`@types/${imp}`) && !builtinModules.includes(imp) && !isSatisfiedByTsScriptsVariant(imp, [...dependencies, ...devDependencies, ...peerDependencies])) {
      results.push(imp);
    }
  }
  return results;
}
function getUnlistedDevDependencies({ name, location }, {
  devDependencies,
  dependencies,
  peerDependencies
}, {
  allImportPaths,
  externalAllImports,
  distImports,
  externalDistTypeImports
}) {
  const unlisted = findUnlistedDevDeps(name, dependencies, devDependencies, peerDependencies, externalAllImports, distImports, externalDistTypeImports);
  for (const imp of unlisted) {
    console.log(`[${chalk2.blue(name)}] Missing devDependency in package.json: ${chalk2.red(imp)}`);
    if (allImportPaths[imp]) {
      console.log(`  ${allImportPaths[imp].join("\n ")}`);
    }
  }
  if (unlisted.length > 0) {
    console.log(`  ${chalk2.yellow(`${location}/package.json`)}
`);
  }
  return unlisted.length;
}
function fixUnlistedDevDependencies(workspace, {
  devDependencies,
  dependencies,
  peerDependencies
}, {
  externalAllImports,
  distImports,
  externalDistTypeImports
}) {
  const unlisted = findUnlistedDevDeps(workspace.name, dependencies, devDependencies, peerDependencies, externalAllImports, distImports, externalDistTypeImports);
  if (unlisted.length === 0) return 0;
  const pkg = readPackageJson(workspace.location);
  for (const imp of unlisted) {
    const version = resolveDepVersion(workspace.location, imp);
    addDep(pkg, imp, version, "devDependencies");
    logFix(workspace, "added to devDependencies", imp, version);
  }
  writePackageJson(workspace.location, pkg);
  return unlisted.length;
}
export {
  findUnlistedDevDeps,
  fixUnlistedDevDependencies,
  getUnlistedDevDependencies
};
//# sourceMappingURL=getUnlistedDevDependencies.mjs.map
