import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import process__default from 'node:process';
import { i as installSuperpowers } from '../shared/ccjk.Bq8TqZG_.mjs';
import { j as join } from '../shared/ccjk.bQ7Dh1g4.mjs';
import 'node:child_process';
import 'node:fs/promises';
import 'node:util';
import './index2.mjs';
import 'node:url';

const CORE_SKILLS = [
  "brainstorming",
  "writing-plans",
  "executing-plans",
  "systematic-debugging",
  "test-driven-development",
  "using-git-worktrees"
];

function getSuperpowersDir$1() {
  return join(homedir(), ".claude", "plugins", "superpowers");
}
function isSuperpowersInstalled() {
  const superpowersDir = getSuperpowersDir$1();
  return existsSync(superpowersDir) && existsSync(join(superpowersDir, "skills"));
}
function areCoreSkillsInstalled() {
  const skillsDir = join(getSuperpowersDir$1(), "skills");
  if (!existsSync(skillsDir)) {
    return false;
  }
  for (const skill of CORE_SKILLS) {
    const skillPath = join(skillsDir, skill);
    if (!existsSync(skillPath) || !existsSync(join(skillPath, "skill.json"))) {
      return false;
    }
  }
  return true;
}
async function autoInstallSuperpowers(lang = "zh-CN") {
  try {
    if (isSuperpowersInstalled() && areCoreSkillsInstalled()) {
      return true;
    }
    const result = await installSuperpowers({
      lang,
      skipPrompt: true
      // Skip user prompts for silent installation
    });
    if (!result.success) {
      if (process__default.env.DEBUG) {
        console.error("[CCJK Zero-Config] Installation failed:", result.error || result.message);
      }
      return false;
    }
    return isSuperpowersInstalled() && areCoreSkillsInstalled();
  } catch (error) {
    if (process__default.env.DEBUG) {
      console.error("[CCJK Zero-Config] Auto-install failed:", error);
    }
    return false;
  }
}

function getSkillsDir() {
  return join(homedir(), ".claude", "plugins", "superpowers", "skills");
}
function isSkillInstalled(skillName) {
  const skillPath = join(getSkillsDir(), skillName);
  return existsSync(skillPath) && existsSync(join(skillPath, "skill.json"));
}
async function loadSkill(skillName) {
  try {
    if (!isSkillInstalled(skillName)) {
      return {
        skill: skillName,
        success: false,
        error: "Skill not installed"
      };
    }
    const skillJsonPath = join(getSkillsDir(), skillName, "skill.json");
    const skillJson = JSON.parse(readFileSync(skillJsonPath, "utf-8"));
    if (!skillJson.name || !skillJson.version) {
      return {
        skill: skillName,
        success: false,
        error: "Invalid skill.json format"
      };
    }
    return {
      skill: skillName,
      success: true
    };
  } catch (error) {
    return {
      skill: skillName,
      success: false,
      error: error instanceof Error ? error.message : "Unknown error"
    };
  }
}
async function loadCoreSkills(_lang = "zh-CN") {
  const results = await Promise.all(
    CORE_SKILLS.map((skill) => loadSkill(skill))
  );
  if (process__default.env.DEBUG) {
    const successful = results.filter((r) => r.success);
    const failed = results.filter((r) => !r.success);
    console.log(`[Zero-Config] Loaded ${successful.length}/${CORE_SKILLS.length} core skills`);
    if (failed.length > 0) {
      console.log(`[Zero-Config] Failed skills: ${failed.map((r) => r.skill).join(", ")}`);
    }
  }
  return results;
}

function getActivationStatePath() {
  return join(homedir(), ".claude", "plugins", "superpowers", ".activation-state.json");
}
function getSuperpowersDir() {
  return join(homedir(), ".claude", "plugins", "superpowers");
}
function loadActivationState() {
  try {
    const statePath = getActivationStatePath();
    if (!existsSync(statePath)) {
      return null;
    }
    const stateJson = readFileSync(statePath, "utf-8");
    return JSON.parse(stateJson);
  } catch (error) {
    if (process__default.env.DEBUG) {
      console.error("[Zero-Config] Failed to load activation state:", error);
    }
    return null;
  }
}
function saveActivationState(status) {
  try {
    const statePath = getActivationStatePath();
    writeFileSync(statePath, JSON.stringify(status, null, 2), "utf-8");
  } catch (error) {
    if (process__default.env.DEBUG) {
      console.error("[Zero-Config] Failed to save activation state:", error);
    }
  }
}
function checkActivationStatus() {
  const superpowersInstalled = existsSync(getSuperpowersDir());
  const savedState = loadActivationState();
  if (savedState) {
    return savedState;
  }
  return {
    isInstalled: superpowersInstalled,
    coreSkillsLoaded: false,
    loadedSkills: [],
    needsActivation: true,
    lastActivation: void 0
  };
}
async function activateSuperpowers(lang = "zh-CN") {
  const currentStatus = checkActivationStatus();
  if (!currentStatus.needsActivation) {
    return currentStatus;
  }
  if (!currentStatus.isInstalled) {
    const installSuccess = await autoInstallSuperpowers(lang);
    if (!installSuccess) {
      return {
        isInstalled: false,
        coreSkillsLoaded: false,
        loadedSkills: [],
        needsActivation: true,
        lastActivation: void 0
      };
    }
  }
  const loadResults = await loadCoreSkills(lang);
  const successfulLoads = loadResults.filter((r) => r.success);
  const allCoreSkillsLoaded = successfulLoads.length === loadResults.length;
  const newStatus = {
    isInstalled: true,
    coreSkillsLoaded: allCoreSkillsLoaded,
    loadedSkills: successfulLoads.map((r) => r.skill),
    needsActivation: false,
    lastActivation: (/* @__PURE__ */ new Date()).toISOString()
  };
  saveActivationState(newStatus);
  return newStatus;
}

export { activateSuperpowers, autoInstallSuperpowers, checkActivationStatus, loadCoreSkills, loadSkill };
