import path from "node:path";
import fs from "node:fs";
const nextConfigNames = [
  "next.config.ts",
  "next.config.js",
  "next.config.mjs"
];
function hasNextConfigFile(appDir) {
  return nextConfigNames.some((configName) => {
    const configPath = path.join(appDir, configName);
    if (!fs.existsSync(configPath)) {
      return false;
    }
    try {
      return fs.statSync(configPath).isFile();
    } catch {
      return false;
    }
  });
}
function findAppRoot(startDir = process.cwd()) {
  let current = startDir;
  while (true) {
    if (hasNextConfigFile(current)) {
      const mercatoDir = path.join(current, ".mercato");
      const generatedDir = path.join(mercatoDir, "generated");
      if (fs.existsSync(generatedDir)) {
        return { appDir: current, mercatoDir, generatedDir };
      }
      return { appDir: current, mercatoDir, generatedDir };
    }
    const parent = path.dirname(current);
    if (parent === current) {
      break;
    }
    current = parent;
  }
  return null;
}
function findAllApps(rootDir) {
  const appsDir = path.join(rootDir, "apps");
  if (!fs.existsSync(appsDir)) return [];
  const apps = [];
  const entries = fs.readdirSync(appsDir, { withFileTypes: true });
  for (const entry of entries) {
    if (!entry.isDirectory()) continue;
    const appDir = path.join(appsDir, entry.name);
    if (!hasNextConfigFile(appDir)) continue;
    const mercatoDir = path.join(appDir, ".mercato");
    const generatedDir = path.join(mercatoDir, "generated");
    if (fs.existsSync(generatedDir)) {
      apps.push({ appDir, mercatoDir, generatedDir });
    }
  }
  return apps;
}
export {
  findAllApps,
  findAppRoot
};
//# sourceMappingURL=appResolver.js.map
