#!/usr/bin/env node
import {
  useModalState
} from "./chunk-7SOPVGDV.js";
import {
  useKeyboardNavigation
} from "./chunk-KUV24B5M.js";
import {
  useRowScroll
} from "./chunk-WYVDNGJB.js";
import {
  ViewTitle
} from "./chunk-MGNYPVOJ.js";
import {
  useTextInput
} from "./chunk-U3IGFMCY.js";
import {
  DEFAULT_SOURCE,
  addSource,
  getSourceSummary,
  removeSource
} from "./chunk-TZXYBG3R.js";
import {
  getErrorMessage,
  verbose
} from "./chunk-LWXRUR6B.js";
import {
  CLI_COLORS
} from "./chunk-6OWHQ7HM.js";
import {
  init_esm_shims
} from "./chunk-DHET7RCE.js";

// src/cli/components/wizard/step-settings.tsx
init_esm_shims();
import { useState as useState2, useEffect, useCallback as useCallback2 } from "react";
import { Box, Text, useInput } from "ink";

// src/cli/components/hooks/use-source-operations.ts
init_esm_shims();
import { useState, useCallback } from "react";
function useSourceOperations(projectDir, onReload) {
  const [statusMessage, setStatusMessage] = useState(null);
  const handleAdd = useCallback(
    async (url) => {
      try {
        const result = await addSource(projectDir, url);
        setStatusMessage({
          text: `Added "${result.name}" (${result.skillCount} skills)`,
          color: CLI_COLORS.SUCCESS
        });
        await onReload();
      } catch (error) {
        const message = getErrorMessage(error);
        setStatusMessage({ text: `Failed to add source: ${message}`, color: CLI_COLORS.ERROR });
      }
    },
    [projectDir, onReload]
  );
  const handleRemove = useCallback(
    async (name) => {
      try {
        await removeSource(projectDir, name);
        setStatusMessage({ text: `Removed "${name}"`, color: CLI_COLORS.SUCCESS });
        await onReload();
        return true;
      } catch (error) {
        const message = getErrorMessage(error);
        setStatusMessage({ text: `Failed to remove: ${message}`, color: CLI_COLORS.ERROR });
        return false;
      }
    },
    [projectDir, onReload]
  );
  const clearStatus = useCallback(() => {
    setStatusMessage(null);
  }, []);
  return { handleAdd, handleRemove, statusMessage, clearStatus };
}

// src/cli/components/wizard/step-settings.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var DEFAULT_SOURCE_NAME = "public";
var SOURCE_LIST_BORDER_LINES = 2;
var StepSettings = ({
  projectDir,
  availableHeight = 0,
  onClose
}) => {
  const [summary, setSummary] = useState2(null);
  const addModal = useModalState();
  const {
    value: addSourceInput,
    setValue: setAddSourceInput,
    handleInput: handleTextInput
  } = useTextInput("");
  const [isLoading, setIsLoading] = useState2(true);
  const loadSummary = useCallback2(async () => {
    try {
      const result = await getSourceSummary(projectDir);
      setSummary(result);
    } catch (error) {
      verbose(`Failed to load source summary: ${getErrorMessage(error)}`);
      setSummary({
        sources: [{ name: DEFAULT_SOURCE_NAME, url: DEFAULT_SOURCE, enabled: true }],
        localSkillCount: 0,
        pluginSkillCount: 0
      });
    }
    setIsLoading(false);
  }, [projectDir]);
  useEffect(() => {
    void loadSummary();
  }, [loadSummary]);
  const { handleAdd, handleRemove, statusMessage, clearStatus } = useSourceOperations(
    projectDir,
    loadSummary
  );
  const sourceCount = summary?.sources.length ?? 0;
  const { focusedIndex, setFocusedIndex } = useKeyboardNavigation(
    sourceCount,
    { onEscape: onClose },
    { wrap: false, vimKeys: false, active: !addModal.isOpen }
  );
  const sourceViewportHeight = availableHeight > SOURCE_LIST_BORDER_LINES ? availableHeight - SOURCE_LIST_BORDER_LINES : 0;
  const { scrollEnabled, scrollTop } = useRowScroll({
    focusedIndex,
    itemCount: sourceCount,
    availableHeight: sourceViewportHeight
  });
  useInput((input, key) => {
    if (statusMessage) {
      clearStatus();
    }
    if (addModal.isOpen) {
      if (key.escape) {
        addModal.close();
        setAddSourceInput("");
        return;
      }
      if (key.return) {
        if (addSourceInput.trim()) {
          addModal.close();
          setAddSourceInput("");
          void handleAdd(addSourceInput.trim());
        }
        return;
      }
      handleTextInput(input, key);
      return;
    }
    if (key.return) {
      return;
    }
    if (key.backspace || key.delete) {
      if (summary?.sources[focusedIndex]) {
        const source = summary.sources[focusedIndex];
        if (source.name !== DEFAULT_SOURCE_NAME) {
          void handleRemove(source.name).then((success) => {
            if (success) {
              setFocusedIndex((prev) => Math.max(0, prev - 1));
            }
          });
        }
      }
      return;
    }
    if (input === "a" || input === "A") {
      addModal.open(true);
      setAddSourceInput("");
    }
  });
  if (isLoading) {
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
      /* @__PURE__ */ jsx(ViewTitle, { children: "Skill Sources" }),
      /* @__PURE__ */ jsx(Text, { dimColor: true, children: "Loading sources..." })
    ] });
  }
  const sourceElements = summary?.sources.map((source, index) => {
    const isFocused = index === focusedIndex && !addModal.isOpen;
    const isDefault = source.name === DEFAULT_SOURCE_NAME;
    const checkmark = source.enabled ? "\u2713" : " ";
    const displayName = isDefault ? "Public" : source.name;
    const suffix = isDefault ? " (default)" : "";
    return /* @__PURE__ */ jsxs(Box, { flexShrink: 0, children: [
      /* @__PURE__ */ jsxs(Text, { color: isFocused ? CLI_COLORS.PRIMARY : void 0, bold: isFocused, children: [
        isFocused ? ">" : " ",
        " ",
        checkmark,
        " ",
        displayName
      ] }),
      /* @__PURE__ */ jsxs(Text, { dimColor: true, children: [
        "  ",
        source.url,
        suffix
      ] })
    ] }, source.name);
  });
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", paddingX: 2, children: [
    /* @__PURE__ */ jsx(ViewTitle, { children: "Skill Sources" }),
    /* @__PURE__ */ jsx(Box, { marginTop: 1 }),
    /* @__PURE__ */ jsx(Text, { bold: true, children: "Configured marketplaces:" }),
    /* @__PURE__ */ jsx(
      Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: CLI_COLORS.NEUTRAL,
        paddingX: 1,
        marginTop: 1,
        children: /* @__PURE__ */ jsx(
          Box,
          {
            flexDirection: "column",
            ...scrollEnabled && { height: sourceViewportHeight, overflow: "hidden" },
            children: /* @__PURE__ */ jsx(
              Box,
              {
                flexDirection: "column",
                marginTop: scrollTop > 0 ? -scrollTop : 0,
                ...scrollEnabled && { flexShrink: 0 },
                children: sourceElements
              }
            )
          }
        )
      }
    ),
    /* @__PURE__ */ jsx(
      Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: addModal.isOpen ? CLI_COLORS.PRIMARY : CLI_COLORS.NEUTRAL,
        paddingX: 1,
        marginTop: 1,
        children: /* @__PURE__ */ jsxs(Text, { color: addModal.isOpen ? CLI_COLORS.PRIMARY : void 0, children: [
          "+ Add source: ",
          addModal.isOpen ? addSourceInput : "",
          addModal.isOpen ? "\u2588" : ""
        ] })
      }
    ),
    statusMessage && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: statusMessage.color, children: statusMessage.text }) }),
    /* @__PURE__ */ jsxs(Box, { marginTop: 1, flexDirection: "column", children: [
      /* @__PURE__ */ jsxs(Text, { dimColor: true, children: [
        "Local skills: ",
        summary?.localSkillCount ?? 0,
        " in .claude/skills/"
      ] }),
      /* @__PURE__ */ jsxs(Text, { dimColor: true, children: [
        "Plugins: ",
        summary?.pluginSkillCount ?? 0,
        " from installed plugins"
      ] })
    ] }),
    /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { dimColor: true, children: addModal.isOpen ? "ENTER submit  ESC cancel" : "A add  DEL remove  ESC or G to close" }) })
  ] });
};

export {
  StepSettings
};
//# sourceMappingURL=chunk-52M2XF3W.js.map