import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useCallback } from "react";
import { Box, Text, useInput, useApp } from "ink";
import { getConfig } from "../../../config.js";
import { semantic } from "../theme/palette.js";
import { activityStore } from "../state/activity-store-instance.js";
function DashboardUninstall({ onBack }) {
  const { exit } = useApp();
  const [phase, setPhase] = useState("confirm");
  const [confirmed, setConfirmed] = useState(false);
  const [resultLines, setResultLines] = useState([]);
  const [resultOk, setResultOk] = useState(false);
  const runUninstall = useCallback(async () => {
    setPhase("running");
    const end = activityStore.begin("uninstall");
    try {
      const config = getConfig();
      const { uninstall } = await import("../actions/index.js");
      const result = await uninstall({ dataDir: config.dataDir, confirmed: true });
      const lines = [];
      if (result.dataDirRemoved) {
        lines.push(`Removed data directory: ${config.dataDir}`);
      }
      for (const ar of result.agentResults) {
        if (ar.error) {
          lines.push(`${ar.displayName}: error \u2014 ${ar.error}`);
        } else if (ar.removed.length > 0) {
          lines.push(`${ar.displayName}: removed ${ar.removed.join(", ")}`);
        } else {
          lines.push(`${ar.displayName}: nothing to remove`);
        }
      }
      if (lines.length === 0) {
        lines.push("Nothing found to remove.");
      }
      if (result.error) {
        lines.push(`Error: ${result.error}`);
      }
      setResultLines(lines);
      setResultOk(result.ok);
      setPhase("done");
    } finally {
      end();
    }
  }, []);
  useInput(useCallback((input, key) => {
    if (phase === "running") return;
    if (phase === "done") {
      if (key.escape || input === "q" || key.return) {
        exit();
      }
      return;
    }
    if (input === "y" || input === "Y") {
      setConfirmed(true);
      void runUninstall();
    } else if (key.escape || input === "q" || input === "n" || input === "N") {
      onBack();
    }
  }, [phase, runUninstall, onBack, exit]));
  if (phase === "running") {
    return /* @__PURE__ */ jsx(Box, { paddingX: 2, children: /* @__PURE__ */ jsx(Text, { color: semantic.warn, children: "Uninstalling wigolo\u2026" }) });
  }
  if (phase === "done") {
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
      /* @__PURE__ */ jsx(Text, { bold: true, color: resultOk ? semantic.ok : semantic.err, children: resultOk ? "Wigolo uninstalled" : "Uninstall encountered errors" }),
      resultLines.map((line, i) => /* @__PURE__ */ jsx(Box, { paddingLeft: 2, children: /* @__PURE__ */ jsx(Text, { color: line.startsWith("Error") ? semantic.err : void 0, children: line }) }, i)),
      /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: "Press enter or q to exit" }) })
    ] });
  }
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
    /* @__PURE__ */ jsx(Text, { bold: true, color: semantic.err, children: "Uninstall wigolo" }),
    /* @__PURE__ */ jsxs(Box, { marginTop: 1, flexDirection: "column", children: [
      /* @__PURE__ */ jsx(Text, { children: "This will permanently remove:" }),
      /* @__PURE__ */ jsxs(Box, { paddingLeft: 2, flexDirection: "column", children: [
        /* @__PURE__ */ jsx(Text, { children: "\u2022 All cached data and embeddings" }),
        /* @__PURE__ */ jsx(Text, { children: "\u2022 ML models and browser data" }),
        /* @__PURE__ */ jsx(Text, { children: "\u2022 MCP configs from all detected AI tools" })
      ] })
    ] }),
    /* @__PURE__ */ jsxs(Box, { marginTop: 1, children: [
      /* @__PURE__ */ jsx(Text, { color: semantic.warn, children: "Type y to confirm or n/esc to cancel: " }),
      confirmed && /* @__PURE__ */ jsx(Text, { color: semantic.ok, children: "y" })
    ] })
  ] });
}
export {
  DashboardUninstall
};
//# sourceMappingURL=DashboardUninstall.js.map