// src/utils/paths.ts
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { fileURLToPath } from "url";
var __filename = fileURLToPath(import.meta.url);
var __dirname = path.dirname(__filename);
function findUpFrom(startDir, targetName, marker, maxLevels) {
  let dir = startDir;
  for (let i = 0; i < maxLevels; i++) {
    const candidate = path.join(dir, targetName);
    if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) {
      if (fs.existsSync(path.join(candidate, marker))) {
        return candidate;
      }
    }
    dir = path.dirname(dir);
  }
  return null;
}
function findUpDir(targetName, marker, maxLevels = 8) {
  const fromModule = findUpFrom(__dirname, targetName, marker, maxLevels);
  if (fromModule) return fromModule;
  return findUpFrom(process.cwd(), targetName, marker, maxLevels);
}
function resolveTemplatesDir() {
  return findUpDir("templates", "base") ?? path.join(__dirname, "templates");
}
function resolvePersonasDir() {
  const agentsDir = findUpDir("agents", "personas");
  if (agentsDir) {
    return path.join(agentsDir, "personas");
  }
  return path.join(__dirname, "agents", "personas");
}
function resolveSkillsDir() {
  const agentsDir = findUpDir("agents", "skills");
  if (agentsDir) {
    return path.join(agentsDir, "skills", "claude-code");
  }
  return path.join(__dirname, "agents", "skills", "claude-code");
}
function resolveProjectSkillsDir(cwd) {
  let dir = cwd ?? process.cwd();
  for (let i = 0; i < 8; i++) {
    const candidate = path.join(dir, "agents", "skills", "claude-code");
    if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) {
      const agentsDir = path.join(dir, "agents");
      if (fs.existsSync(path.join(agentsDir, "skills"))) {
        return candidate;
      }
    }
    const parent = path.dirname(dir);
    if (parent === dir) break;
    dir = parent;
  }
  return null;
}
function resolveGlobalSkillsDir() {
  const agentsDir = findUpDir("agents", "skills");
  if (agentsDir) {
    return path.join(agentsDir, "skills", "claude-code");
  }
  return path.join(__dirname, "agents", "skills", "claude-code");
}
function resolveCommunitySkillsDir(platform = "claude-code") {
  const agentsDir = findUpDir("agents", "skills");
  if (agentsDir) {
    return path.join(agentsDir, "skills", "community", platform);
  }
  return path.join(__dirname, "agents", "skills", "community", platform);
}
function resolveGlobalCommunitySkillsDir(platform = "claude-code") {
  return path.join(os.homedir(), ".harness", "skills", "community", platform);
}
function resolveGlobalCommunityBaseDir() {
  return path.join(os.homedir(), ".harness", "skills", "community");
}
function resolveAllSkillsDirs(platform = "claude-code") {
  const dirs = [];
  const projectDir = resolveProjectSkillsDir();
  if (projectDir) {
    const platformDir = path.join(path.dirname(projectDir), platform);
    if (fs.existsSync(platformDir)) {
      dirs.push(platformDir);
    }
  }
  const communityDir = resolveCommunitySkillsDir(platform);
  if (fs.existsSync(communityDir)) {
    dirs.push(communityDir);
  }
  const globalDir = resolveGlobalSkillsDir();
  const globalPlatformDir = path.join(path.dirname(globalDir), platform);
  if (fs.existsSync(globalPlatformDir)) {
    if (!dirs.some((d) => path.resolve(d) === path.resolve(globalPlatformDir))) {
      dirs.push(globalPlatformDir);
    }
  }
  return dirs;
}

export {
  resolveTemplatesDir,
  resolvePersonasDir,
  resolveSkillsDir,
  resolveProjectSkillsDir,
  resolveGlobalSkillsDir,
  resolveCommunitySkillsDir,
  resolveGlobalCommunitySkillsDir,
  resolveGlobalCommunityBaseDir,
  resolveAllSkillsDirs
};
