"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useKeyboardShortcuts_exports = {};
__export(useKeyboardShortcuts_exports, {
  useKeyboardShortcuts: () => useKeyboardShortcuts
});
module.exports = __toCommonJS(useKeyboardShortcuts_exports);
var import_react = require("react");
var import_ink = require("ink");
const EXIT_WARNING_TIMEOUT = 3e3;
function useKeyboardShortcuts({ state, dispatch, agent }) {
  const { exit } = (0, import_ink.useApp)();
  const sessionIdRef = (0, import_react.useRef)(state.session.id);
  (0, import_react.useEffect)(() => {
    sessionIdRef.current = state.session.id;
  }, [state.session.id]);
  (0, import_react.useEffect)(() => {
    if (!state.ui.exitWarningShown || !state.ui.exitWarningTimestamp) return;
    const elapsed = Date.now() - state.ui.exitWarningTimestamp;
    const remaining = EXIT_WARNING_TIMEOUT - elapsed;
    if (remaining <= 0) {
      dispatch({ type: "EXIT_WARNING_CLEAR" });
      return;
    }
    const timer = setTimeout(() => {
      dispatch({ type: "EXIT_WARNING_CLEAR" });
    }, remaining);
    return () => clearTimeout(timer);
  }, [state.ui.exitWarningShown, state.ui.exitWarningTimestamp, dispatch]);
  (0, import_ink.useInput)(
    (inputChar, key) => {
      if (state.approval) {
        return;
      }
      if (state.ui.activeOverlay !== "none" && state.ui.activeOverlay !== "approval") {
        return;
      }
      if (key.ctrl && inputChar === "c") {
        if (state.ui.exitWarningShown) {
          exit();
        } else {
          dispatch({ type: "EXIT_WARNING_SHOW" });
        }
        return;
      }
      if (key.escape) {
        if (state.ui.exitWarningShown) {
          dispatch({ type: "EXIT_WARNING_CLEAR" });
          return;
        }
        if (state.ui.isProcessing) {
          const currentSessionId = sessionIdRef.current;
          if (!currentSessionId) {
            return;
          }
          void agent.cancel(currentSessionId).catch(() => {
          });
          dispatch({ type: "CANCEL_START" });
        } else if (state.ui.activeOverlay !== "none") {
          dispatch({ type: "CLOSE_OVERLAY" });
        }
      }
    },
    // Always active - we handle guards internally for more reliable behavior
    { isActive: true }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useKeyboardShortcuts
});
