import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useCallback } from "react";
import { Box, Text, useInput } from "ink";
import { join } from "node:path";
import { homedir } from "node:os";
import { semantic } from "../theme/palette.js";
import { activityStore } from "../state/activity-store-instance.js";
const MENU_ITEMS = [
  {
    id: "export",
    label: "Export config",
    description: "Save settings to ~/wigolo-config-export.json (secrets excluded)"
  },
  {
    id: "import",
    label: "Import config",
    description: "Load settings from ~/wigolo-config-export.json"
  }
];
function DashboardExport({ onBack }) {
  const [cursor, setCursor] = useState(0);
  const [phase, setPhase] = useState("menu");
  const [resultMsg, setResultMsg] = useState("");
  const [resultOk, setResultOk] = useState(false);
  const defaultExportPath = join(homedir(), "wigolo-config-export.json");
  const runAction = useCallback(async (action) => {
    setPhase("running");
    const end = activityStore.begin(action);
    try {
      const { exportConfig, importConfig } = await import("../actions/index.js");
      const configPath = process.env.WIGOLO_CONFIG_PATH ?? join(homedir(), ".wigolo", "config.json");
      if (action === "export") {
        const result = await exportConfig(defaultExportPath, configPath);
        setResultOk(result.ok);
        setResultMsg(
          result.ok ? `Exported to ${defaultExportPath} (secrets excluded)` : `Export failed: ${result.error}`
        );
      } else {
        const result = await importConfig(defaultExportPath, configPath);
        setResultOk(result.ok);
        setResultMsg(
          result.ok ? `Imported from ${defaultExportPath}` : `Import failed: ${result.error}`
        );
      }
    } catch (err) {
      setResultOk(false);
      setResultMsg(err instanceof Error ? err.message : String(err));
    } finally {
      end();
    }
    setPhase("done");
  }, [defaultExportPath]);
  useInput(useCallback((input, key) => {
    if (phase === "running") return;
    if (phase === "done") {
      if (key.escape || input === "q" || key.return) onBack();
      return;
    }
    if (key.upArrow) setCursor((c) => c > 0 ? c - 1 : MENU_ITEMS.length - 1);
    else if (key.downArrow) setCursor((c) => c < MENU_ITEMS.length - 1 ? c + 1 : 0);
    else if (key.return) {
      const item = MENU_ITEMS[cursor];
      if (item) void runAction(item.id);
    } else if (key.escape || input === "q") onBack();
  }, [phase, cursor, runAction, onBack]));
  if (phase === "running") {
    return /* @__PURE__ */ jsx(Box, { paddingX: 2, children: /* @__PURE__ */ jsx(Text, { color: semantic.warn, children: "Running\u2026" }) });
  }
  if (phase === "done") {
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
      /* @__PURE__ */ jsx(Text, { color: resultOk ? semantic.ok : semantic.err, children: resultMsg }),
      /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: "Press enter or q/esc to return" }) })
    ] });
  }
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
    /* @__PURE__ */ jsx(Text, { bold: true, children: "Wigolo \u2014 export / import config" }),
    /* @__PURE__ */ jsxs(Text, { dimColor: true, children: [
      "Export path: ",
      defaultExportPath
    ] }),
    /* @__PURE__ */ jsx(Box, { flexDirection: "column", marginTop: 1, children: MENU_ITEMS.map((item, i) => {
      const isFocused = i === cursor;
      return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
        /* @__PURE__ */ jsxs(Text, { children: [
          isFocused ? /* @__PURE__ */ jsx(Text, { color: semantic.accent, children: "\u276F " }) : "  ",
          /* @__PURE__ */ jsx(Text, { bold: isFocused, children: item.label })
        ] }),
        isFocused && /* @__PURE__ */ jsx(Box, { paddingLeft: 4, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: item.description }) })
      ] }, item.id);
    }) }),
    /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\u2191/\u2193 navigate \xB7 enter select \xB7 q/esc back" }) })
  ] });
}
export {
  DashboardExport
};
//# sourceMappingURL=DashboardExport.js.map