"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_ai_modal_shortcuts_exports = {};
__export(use_ai_modal_shortcuts_exports, {
  useAiModalShortcuts: () => useAiModalShortcuts
});
module.exports = __toCommonJS(use_ai_modal_shortcuts_exports);
var import_react = __toESM(require("react"));
var import_ts_utils = require("../ts-utils.js");
const useAiModalShortcuts = ({
  state,
  selection,
  modalRef,
  cancelPrompt,
  closeModal,
  triggerFreePrompt,
  submitPrompt,
  prompts,
  setSelectedPromptIndex,
  publishModalAction
}) => {
  import_react.default.useEffect(() => {
    if (typeof window === "undefined") {
      return;
    }
    const handleKeyDown = (e) => {
      if (!(modalRef.current.contains(document.activeElement) || modalRef.current === document.activeElement)) {
        return;
      }
      switch (state.type) {
        case "INITIAL": {
          const promptsList = prompts.selectionPrompts;
          if (e.key === "Enter" && e.target instanceof HTMLElement && e.target.getAttribute("data-hook") !== "sidePanel-header-close-button") {
            e.preventDefault();
            if (state.textualPrompt.length) {
              triggerFreePrompt();
              publishModalAction("submit_free_prompt", state.textualPrompt);
              return;
            } else {
              const prompt = promptsList[state.selectedPromptIndex];
              if (prompt.type === "prompt") {
                submitPrompt(prompt, selection);
                publishModalAction(
                  "promptSelected",
                  "promptSlug" in prompt ? prompt.promptSlug : prompt.promptId
                );
                return;
              }
            }
          }
          if (e.key === "ArrowDown") {
            e.preventDefault();
            const currentItem = promptsList[state.selectedPromptIndex];
            const promptsOnly = promptsList.filter((p) => (0, import_ts_utils.isOfType)(["prompt"], p));
            const nextItemIndex = promptsOnly.findIndex((p) => p === currentItem) + 1;
            const nextItem = promptsOnly[nextItemIndex];
            if (nextItem && (0, import_ts_utils.isOfType)(["prompt"], nextItem)) {
              setSelectedPromptIndex(
                Math.min(promptsList.indexOf(nextItem), promptsList.length - 1),
                true
              );
            }
            return;
          }
          if (e.key === "ArrowUp") {
            e.preventDefault();
            const currentItem = promptsList[state.selectedPromptIndex];
            const promptsOnly = promptsList.filter((p) => (0, import_ts_utils.isOfType)(["prompt"], p));
            const nextItemIndex = Math.max(promptsOnly.findIndex((p) => p === currentItem) - 1, 0);
            const prevItem = promptsOnly[nextItemIndex];
            if (prevItem && (0, import_ts_utils.isOfType)(["prompt"], prevItem)) {
              setSelectedPromptIndex(Math.max(promptsList.indexOf(prevItem), 0), true);
            }
            return;
          }
          break;
        }
        case "IN_PROGRESS": {
          if (e.key === "Escape") {
            cancelPrompt();
            publishModalAction("cancel_prompt");
            return;
          }
          break;
        }
        default: {
        }
      }
      if (e.key === "Escape") {
        closeModal();
        publishModalAction("close_with_esc");
        return;
      }
    };
    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
    };
  }, [modalRef.current, state, selection, closeModal]);
};
//# sourceMappingURL=use-ai-modal-shortcuts.js.map
