import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { existsSync } from "node:fs";
async function hasPackageJsonDependency(projectRoot, files, dependencyNames) {
  const packageJsonFiles = files.filter((f) => f === "package.json" || f.endsWith("/package.json"));
  if (packageJsonFiles.length === 0) {
    packageJsonFiles.push("package.json");
  }
  for (const file of packageJsonFiles) {
    const packageJsonPath = join(projectRoot, file);
    if (existsSync(packageJsonPath)) {
      try {
        const content = await readFile(packageJsonPath, "utf-8");
        const pkg = JSON.parse(content);
        const deps = { ...pkg.dependencies, ...pkg.devDependencies };
        if (deps) {
          for (const depName of dependencyNames) {
            if (deps[depName]) {
              return true;
            }
          }
        }
      } catch {
      }
    }
  }
  return false;
}
async function hasComposerDependency(projectRoot, files, dependencyNames) {
  const composerFiles = files.filter((f) => f === "composer.json" || f.endsWith("/composer.json"));
  if (composerFiles.length === 0) {
    composerFiles.push("composer.json");
  }
  for (const file of composerFiles) {
    const composerPath = join(projectRoot, file);
    if (existsSync(composerPath)) {
      try {
        const composer = JSON.parse(await readFile(composerPath, "utf-8"));
        const deps = { ...composer.require, ...composer["require-dev"] };
        if (deps) {
          for (const depName of dependencyNames) {
            if (deps[depName]) {
              return true;
            }
          }
        }
      } catch {
      }
    }
  }
  return false;
}
export {
  hasComposerDependency,
  hasPackageJsonDependency
};
