// @bun
import {
  WelcomeV2
} from "./chunk-cpmczx39.js";
import {
  ApproveApiKey
} from "./chunk-3g7bvgp2.js";
import {
  PressEnterToContinue,
  init_PressEnterToContinue
} from "./chunk-qxp0nye6.js";
import {
  ThemePicker,
  init_ThemePicker
} from "./chunk-xfgc43at.js";
import {
  ConsoleOAuthFlow,
  Select,
  Spinner,
  init_ConsoleOAuthFlow,
  init_Spinner,
  init_select
} from "./chunk-421yegj3.js";
import"./chunk-aaxz8a5g.js";
import"./chunk-2kcgxnvp.js";
import"./chunk-14p6wvsq.js";
import"./chunk-6s0q1s5r.js";
import"./chunk-0cpg8qh8.js";
import"./chunk-9phn5khx.js";
import"./chunk-vk9xcryd.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-ptxteaeh.js";
import"./chunk-7fcmqvbz.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-q3b4n194.js";
import"./chunk-zaqfb3tz.js";
import"./chunk-6hzx0kqr.js";
import"./chunk-71sdcaq6.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-dxa3y9vh.js";
import"./chunk-gjvhjywj.js";
import"./chunk-1w20m7gw.js";
import"./chunk-3gam18xn.js";
import"./chunk-rpmntgyh.js";
import"./chunk-24kv69g3.js";
import {
  init_useExitOnCtrlCDWithKeybindings,
  useExitOnCtrlCDWithKeybindings
} from "./chunk-tzcrwnrf.js";
import {
  init_terminalSetup,
  setupTerminal,
  shouldOfferTerminalSetup
} from "./chunk-k7xszak2.js";
import"./chunk-ztmb7geg.js";
import"./chunk-tbdkekz3.js";
import {
  init_useKeybinding
} from "./chunk-65zbgg1n.js";
import"./chunk-4spgkgr3.js";
import"./chunk-60fkafk2.js";
import"./chunk-h5zz0vxy.js";
import"./chunk-6hygvrhn.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-8gv2pjby.js";
import"./chunk-pn23dh3d.js";
import"./chunk-rpdh02rm.js";
import"./chunk-j1wktfe2.js";
import"./chunk-fjtzfz1t.js";
import"./chunk-6hr7742d.js";
import"./chunk-xyg1sk2w.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-hem4f4gz.js";
import"./chunk-hqxp6b72.js";
import"./chunk-ccw8pse0.js";
import"./chunk-wsnxtggw.js";
import"./chunk-k8nwt8yq.js";
import"./chunk-vxbjzggp.js";
import"./chunk-d6h68rz1.js";
import"./chunk-8yvsnrkr.js";
import"./chunk-5mmhfbxb.js";
import {
  getCustomApiKeyStatus,
  init_auth,
  init_authPortable,
  init_config1 as init_config,
  isAnthropicAuthEnabled,
  normalizeApiKeyForConfig
} from "./chunk-s2t5anwv.js";
import"./chunk-ga1jkyqy.js";
import"./chunk-9hbhydvp.js";
import"./chunk-ck9r8hpf.js";
import"./chunk-k7hexw3v.js";
import"./chunk-trqrj23e.js";
import"./chunk-za3k6h2h.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-hjxqmtg5.js";
import"./chunk-bzyzrq0k.js";
import"./chunk-vwenx8ke.js";
import {
  env,
  init_env
} from "./chunk-q2h79ncs.js";
import"./chunk-v4ypszbb.js";
import"./chunk-qfsn720k.js";
import"./chunk-7qx3xhv3.js";
import"./chunk-j9475b46.js";
import"./chunk-fdwabr8p.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-dbts5q5p.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import"./chunk-q1vrhh0q.js";
import"./chunk-hn4w9pkj.js";
import {
  Link,
  Newline,
  ThemedBox_default,
  ThemedText,
  init_src,
  useKeybindings,
  useTheme,
  useTimeout
} from "./chunk-2g0ytd9d.js";
import {
  require_jsx_dev_runtime,
  require_react
} from "./chunk-de0qnnf7.js";
import {
  init_analytics,
  logEvent
} from "./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-92bjkrf2.js";
import"./chunk-10k5jfc5.js";
import"./chunk-n9g24mwe.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-hzmzwte2.js";
import"./chunk-pdvg91cg.js";
import"./chunk-c696122m.js";
import"./chunk-xbj5keyy.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import"./chunk-aeysytks.js";
import {
  init_envUtils,
  isRunningOnHomespace
} from "./chunk-s3hafnk3.js";
import"./chunk-kb3758f7.js";
import"./chunk-9h9k5vz3.js";
import"./chunk-xmpgmeb8.js";
import"./chunk-2kz09j2s.js";
import"./chunk-c0k7b0jw.js";
import"./chunk-pecy49yr.js";
import"./chunk-azbab59e.js";
import"./chunk-3nk9q8dr.js";
import {
  __toESM
} from "./chunk-hhsxm2yr.js";

// src/components/Onboarding.tsx
init_analytics();
init_terminalSetup();
init_useExitOnCtrlCDWithKeybindings();
init_src();
init_useKeybinding();
init_auth();
init_authPortable();
init_config();
init_env();
init_envUtils();
var import_react4 = __toESM(require_react(), 1);

// src/utils/preflightChecks.tsx
var import_react = __toESM(require_react(), 1);

// src/hooks/useTimeout.ts
init_src();

// src/utils/preflightChecks.tsx
init_src();
init_Spinner();
var jsx_dev_runtime = __toESM(require_jsx_dev_runtime(), 1);
async function checkEndpoints() {
  return { success: true };
}
function PreflightStep({ onSuccess }) {
  const [result, setResult] = import_react.useState(null);
  const [isChecking, setIsChecking] = import_react.useState(true);
  const showSpinner = useTimeout(1000) && isChecking;
  import_react.useEffect(() => {
    async function run() {
      const checkResult = await checkEndpoints();
      setResult(checkResult);
      setIsChecking(false);
    }
    run();
  }, []);
  import_react.useEffect(() => {
    if (result?.success) {
      onSuccess();
    }
  }, [result, onSuccess]);
  return /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
    flexDirection: "column",
    gap: 1,
    paddingLeft: 1,
    children: isChecking && showSpinner ? /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
      paddingLeft: 1,
      children: [
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(Spinner, {}, undefined, false, undefined, this),
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
          children: "Checking connectivity..."
        }, undefined, false, undefined, this)
      ]
    }, undefined, true, undefined, this) : !result?.success && !isChecking && /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedBox_default, {
      flexDirection: "column",
      gap: 1,
      children: [
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
          color: "error",
          children: "Unable to connect to Anthropic services"
        }, undefined, false, undefined, this),
        /* @__PURE__ */ jsx_dev_runtime.jsxDEV(ThemedText, {
          color: "error",
          children: result?.error
        }, undefined, false, undefined, this)
      ]
    }, undefined, true, undefined, this)
  }, undefined, false, undefined, this);
}

// src/components/Onboarding.tsx
init_ConsoleOAuthFlow();
init_select();
init_PressEnterToContinue();
init_ThemePicker();

// src/components/ui/OrderedList.tsx
init_src();
var import_react3 = __toESM(require_react(), 1);

// src/components/ui/OrderedListItem.tsx
init_src();
var import_react2 = __toESM(require_react(), 1);
var jsx_dev_runtime2 = __toESM(require_jsx_dev_runtime(), 1);
var OrderedListItemContext = import_react2.createContext({ marker: "" });
function OrderedListItem({ children }) {
  const { marker } = import_react2.useContext(OrderedListItemContext);
  return /* @__PURE__ */ jsx_dev_runtime2.jsxDEV(ThemedBox_default, {
    gap: 1,
    children: [
      /* @__PURE__ */ jsx_dev_runtime2.jsxDEV(ThemedText, {
        dimColor: true,
        children: marker
      }, undefined, false, undefined, this),
      /* @__PURE__ */ jsx_dev_runtime2.jsxDEV(ThemedBox_default, {
        flexDirection: "column",
        children
      }, undefined, false, undefined, this)
    ]
  }, undefined, true, undefined, this);
}

// src/components/ui/OrderedList.tsx
var jsx_dev_runtime3 = __toESM(require_jsx_dev_runtime(), 1);
var OrderedListContext = import_react3.createContext({ marker: "" });
function OrderedListComponent({ children }) {
  const { marker: parentMarker } = import_react3.useContext(OrderedListContext);
  let numberOfItems = 0;
  for (const child of import_react3.default.Children.toArray(children)) {
    if (!import_react3.isValidElement(child) || child.type !== OrderedListItem) {
      continue;
    }
    numberOfItems++;
  }
  const maxMarkerWidth = String(numberOfItems).length;
  return /* @__PURE__ */ jsx_dev_runtime3.jsxDEV(ThemedBox_default, {
    flexDirection: "column",
    children: import_react3.default.Children.map(children, (child, index) => {
      if (!import_react3.isValidElement(child) || child.type !== OrderedListItem) {
        return child;
      }
      const paddedMarker = `${String(index + 1).padStart(maxMarkerWidth)}.`;
      const marker = `${parentMarker}${paddedMarker}`;
      return /* @__PURE__ */ jsx_dev_runtime3.jsxDEV(OrderedListContext.Provider, {
        value: { marker },
        children: /* @__PURE__ */ jsx_dev_runtime3.jsxDEV(OrderedListItemContext.Provider, {
          value: { marker },
          children: child
        }, undefined, false, undefined, this)
      }, undefined, false, undefined, this);
    })
  }, undefined, false, undefined, this);
}
OrderedListComponent.Item = OrderedListItem;
var OrderedList = OrderedListComponent;

// src/components/Onboarding.tsx
var jsx_dev_runtime4 = __toESM(require_jsx_dev_runtime(), 1);
function Onboarding({ onDone }) {
  const [currentStepIndex, setCurrentStepIndex] = import_react4.useState(0);
  const [skipOAuth, setSkipOAuth] = import_react4.useState(false);
  const [oauthEnabled] = import_react4.useState(() => isAnthropicAuthEnabled());
  const [theme, setTheme] = useTheme();
  import_react4.useEffect(() => {
    logEvent("tengu_began_setup", {
      oauthEnabled
    });
  }, [oauthEnabled]);
  function goToNextStep() {
    if (currentStepIndex < steps.length - 1) {
      const nextIndex = currentStepIndex + 1;
      setCurrentStepIndex(nextIndex);
      logEvent("tengu_onboarding_step", {
        oauthEnabled,
        stepId: steps[nextIndex]?.id
      });
    } else {
      onDone();
    }
  }
  function handleThemeSelection(newTheme) {
    setTheme(newTheme);
    goToNextStep();
  }
  const exitState = useExitOnCtrlCDWithKeybindings();
  const themeStep = /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
    marginX: 1,
    children: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemePicker, {
      onThemeSelect: handleThemeSelection,
      showIntroText: true,
      helpText: "To change this later, run /theme",
      hideEscToCancel: true,
      skipExitHandling: true
    }, undefined, false, undefined, this)
  }, undefined, false, undefined, this);
  const securityStep = /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
    flexDirection: "column",
    gap: 1,
    paddingLeft: 1,
    children: [
      /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
        bold: true,
        children: "Security notes:"
      }, undefined, false, undefined, this),
      /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
        flexDirection: "column",
        width: 70,
        children: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(OrderedList, {
          children: [
            /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(OrderedList.Item, {
              children: [
                /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                  children: "RedScope can make mistakes"
                }, undefined, false, undefined, this),
                /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                  dimColor: true,
                  wrap: "wrap",
                  children: [
                    "You should always review RedScope AI's responses, especially when ",
                    /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Newline, {}, undefined, false, undefined, this),
                    "running code.",
                    /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Newline, {}, undefined, false, undefined, this)
                  ]
                }, undefined, true, undefined, this)
              ]
            }, undefined, true, undefined, this),
            /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(OrderedList.Item, {
              children: [
                /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                  children: "Due to prompt injection risks, only use it with code you trust"
                }, undefined, false, undefined, this),
                /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                  dimColor: true,
                  wrap: "wrap",
                  children: [
                    "For more details see:",
                    /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Newline, {}, undefined, false, undefined, this),
                    /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Link, {
                      url: "https://code.claude.com/docs/en/security"
                    }, undefined, false, undefined, this)
                  ]
                }, undefined, true, undefined, this)
              ]
            }, undefined, true, undefined, this)
          ]
        }, undefined, true, undefined, this)
      }, undefined, false, undefined, this),
      /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(PressEnterToContinue, {}, undefined, false, undefined, this)
    ]
  }, undefined, true, undefined, this);
  const preflightStep = /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(PreflightStep, {
    onSuccess: goToNextStep
  }, undefined, false, undefined, this);
  const apiKeyNeedingApproval = import_react4.useMemo(() => {
    if (!process.env.ANTHROPIC_API_KEY || isRunningOnHomespace()) {
      return "";
    }
    const customApiKeyTruncated = normalizeApiKeyForConfig(process.env.ANTHROPIC_API_KEY);
    if (getCustomApiKeyStatus(customApiKeyTruncated) === "new") {
      return customApiKeyTruncated;
    }
  }, []);
  function handleApiKeyDone(approved) {
    if (approved) {
      setSkipOAuth(true);
    }
    goToNextStep();
  }
  const steps = [];
  steps.push({ id: "theme", component: themeStep });
  if (apiKeyNeedingApproval) {
    steps.push({
      id: "api-key",
      component: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ApproveApiKey, {
        customApiKeyTruncated: apiKeyNeedingApproval,
        onDone: handleApiKeyDone
      }, undefined, false, undefined, this)
    });
  }
  if (oauthEnabled) {
    steps.push({
      id: "oauth",
      component: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(SkippableStep, {
        skip: skipOAuth,
        onSkip: goToNextStep,
        children: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ConsoleOAuthFlow, {
          startingMessage: "Set up RedScope API access. Choose a provider and enter your API key, or reuse an existing Claude configuration.",
          onDone: goToNextStep
        }, undefined, false, undefined, this)
      }, undefined, false, undefined, this)
    });
  }
  steps.push({ id: "security", component: securityStep });
  if (shouldOfferTerminalSetup()) {
    steps.push({
      id: "terminal-setup",
      component: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
        flexDirection: "column",
        gap: 1,
        paddingLeft: 1,
        children: [
          /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
            bold: true,
            children: "Use RedScope AI's terminal setup?"
          }, undefined, false, undefined, this),
          /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
            flexDirection: "column",
            width: 70,
            gap: 1,
            children: [
              /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                children: [
                  "For the optimal coding experience, enable the recommended settings",
                  /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Newline, {}, undefined, false, undefined, this),
                  "for your terminal:",
                  " ",
                  env.terminal === "Apple_Terminal" ? "Option+Enter for newlines and visual bell" : "Shift+Enter for newlines"
                ]
              }, undefined, true, undefined, this),
              /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(Select, {
                options: [
                  {
                    label: "Yes, use recommended settings",
                    value: "install"
                  },
                  {
                    label: "No, maybe later with /terminal-setup",
                    value: "no"
                  }
                ],
                onChange: (value) => {
                  if (value === "install") {
                    setupTerminal(theme).catch(() => {}).finally(goToNextStep);
                  } else {
                    goToNextStep();
                  }
                },
                onCancel: () => goToNextStep()
              }, undefined, false, undefined, this),
              /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
                dimColor: true,
                children: exitState.pending ? /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(jsx_dev_runtime4.Fragment, {
                  children: [
                    "Press ",
                    exitState.keyName,
                    " again to exit"
                  ]
                }, undefined, true, undefined, this) : /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(jsx_dev_runtime4.Fragment, {
                  children: "Enter to confirm \xB7 Esc to skip"
                }, undefined, false, undefined, this)
              }, undefined, false, undefined, this)
            ]
          }, undefined, true, undefined, this)
        ]
      }, undefined, true, undefined, this)
    });
  }
  const currentStep = steps[currentStepIndex];
  const handleSecurityContinue = import_react4.useCallback(() => {
    if (currentStepIndex === steps.length - 1) {
      onDone();
    } else {
      goToNextStep();
    }
  }, [currentStepIndex, steps.length, oauthEnabled, onDone]);
  const handleTerminalSetupSkip = import_react4.useCallback(() => {
    goToNextStep();
  }, [currentStepIndex, steps.length, oauthEnabled, onDone]);
  useKeybindings({
    "confirm:yes": handleSecurityContinue
  }, {
    context: "Confirmation",
    isActive: currentStep?.id === "security"
  });
  useKeybindings({
    "confirm:no": handleTerminalSetupSkip
  }, {
    context: "Confirmation",
    isActive: currentStep?.id === "terminal-setup"
  });
  return /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
    flexDirection: "column",
    children: [
      /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(WelcomeV2, {}, undefined, false, undefined, this),
      /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
        flexDirection: "column",
        marginTop: 1,
        children: [
          currentStep?.component,
          exitState.pending && /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedBox_default, {
            padding: 1,
            children: /* @__PURE__ */ jsx_dev_runtime4.jsxDEV(ThemedText, {
              dimColor: true,
              children: [
                "Press ",
                exitState.keyName,
                " again to exit"
              ]
            }, undefined, true, undefined, this)
          }, undefined, false, undefined, this)
        ]
      }, undefined, true, undefined, this)
    ]
  }, undefined, true, undefined, this);
}
function SkippableStep({
  skip,
  onSkip,
  children
}) {
  import_react4.useEffect(() => {
    if (skip) {
      onSkip();
    }
  }, [skip, onSkip]);
  if (skip) {
    return null;
  }
  return children;
}
export {
  SkippableStep,
  Onboarding
};
