import { exec } from 'node:child_process';
import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { promisify } from 'node:util';
import { i18n } from '../chunks/index2.mjs';
import { j as join } from './ccjk.bQ7Dh1g4.mjs';

const execAsync = promisify(exec);
function getClaudePluginDir() {
  return join(homedir(), ".claude", "plugins");
}
function getSuperpowersPath() {
  return join(getClaudePluginDir(), "superpowers");
}
async function checkSuperpowersInstalled() {
  const superpowersPath = getSuperpowersPath();
  if (!existsSync(superpowersPath)) {
    return { installed: false };
  }
  try {
    const packageJsonPath = join(superpowersPath, "package.json");
    if (existsSync(packageJsonPath)) {
      const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8"));
      const skillsDir = join(superpowersPath, "skills");
      let skillCount = 0;
      if (existsSync(skillsDir)) {
        const { readdir } = await import('node:fs/promises');
        const entries = await readdir(skillsDir, { withFileTypes: true });
        skillCount = entries.filter((e) => e.isDirectory()).length;
      }
      return {
        installed: true,
        version: packageJson.version,
        skillCount,
        path: superpowersPath
      };
    }
    return { installed: true, path: superpowersPath };
  } catch {
    return { installed: true, path: superpowersPath };
  }
}
async function installSuperpowers(options) {
  try {
    const status = await checkSuperpowersInstalled();
    if (status.installed) {
      return {
        success: true,
        message: i18n.t("superpowers:alreadyInstalled")
      };
    }
    const result = await installSuperpowersViaGit(options.skipPrompt);
    if (result.success && options.enableCloudSync && options.cloudProvider && options.cloudCredentials) {
      try {
        const { configureCloudSync } = await import('../chunks/cloud-sync.mjs');
        await configureCloudSync(options.cloudProvider, options.cloudCredentials);
        if (!options.skipPrompt) {
          console.log(i18n.t("superpowers:cloudSync.configured"));
        }
      } catch (error) {
        if (!options.skipPrompt) {
          console.warn(i18n.t("superpowers:cloudSync.configFailed"), error);
        }
      }
    }
    return result;
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return {
      success: false,
      message: i18n.t("superpowers:installFailed"),
      error: errorMessage
    };
  }
}
async function installSuperpowersViaGit(silent = false) {
  try {
    const pluginDir = getClaudePluginDir();
    const superpowersPath = getSuperpowersPath();
    const { mkdir } = await import('node:fs/promises');
    await mkdir(pluginDir, { recursive: true });
    if (!silent) {
      console.log(i18n.t("superpowers:cloning"));
    }
    await execAsync(
      `git clone https://github.com/obra/superpowers.git "${superpowersPath}"`,
      { timeout: 12e4 }
    );
    const status = await checkSuperpowersInstalled();
    if (status.installed) {
      return {
        success: true,
        message: i18n.t("superpowers:installSuccess")
      };
    }
    return {
      success: false,
      message: i18n.t("superpowers:installFailed")
    };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return {
      success: false,
      message: i18n.t("superpowers:installFailed"),
      error: errorMessage
    };
  }
}
async function uninstallSuperpowers() {
  try {
    const status = await checkSuperpowersInstalled();
    if (!status.installed) {
      return {
        success: true,
        message: i18n.t("superpowers:notInstalled")
      };
    }
    const superpowersPath = getSuperpowersPath();
    if (existsSync(superpowersPath)) {
      const { rm } = await import('node:fs/promises');
      await rm(superpowersPath, { recursive: true, force: true });
    }
    const newStatus = await checkSuperpowersInstalled();
    if (!newStatus.installed) {
      return {
        success: true,
        message: i18n.t("superpowers:uninstallSuccess")
      };
    }
    return {
      success: false,
      message: i18n.t("superpowers:uninstallFailed")
    };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return {
      success: false,
      message: i18n.t("superpowers:uninstallFailed"),
      error: errorMessage
    };
  }
}
async function updateSuperpowers() {
  try {
    const status = await checkSuperpowersInstalled();
    if (!status.installed) {
      return {
        success: false,
        message: i18n.t("superpowers:notInstalled")
      };
    }
    const superpowersPath = getSuperpowersPath();
    await execAsync("git pull", {
      cwd: superpowersPath,
      timeout: 6e4
    });
    return {
      success: true,
      message: i18n.t("superpowers:updateSuccess")
    };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return {
      success: false,
      message: i18n.t("superpowers:updateFailed"),
      error: errorMessage
    };
  }
}
async function getSuperpowersSkills() {
  try {
    const status = await checkSuperpowersInstalled();
    if (!status.installed || !status.path) {
      return [];
    }
    const skillsDir = join(status.path, "skills");
    if (!existsSync(skillsDir)) {
      return [];
    }
    const { readdir } = await import('node:fs/promises');
    const entries = await readdir(skillsDir, { withFileTypes: true });
    return entries.filter((e) => e.isDirectory()).map((e) => e.name);
  } catch {
    return [];
  }
}

export { uninstallSuperpowers as a, installSuperpowersViaGit as b, checkSuperpowersInstalled as c, getSuperpowersSkills as g, installSuperpowers as i, updateSuperpowers as u };
