import React from "react";
import { isOfType } from "../ts-utils.js";
const useAiModalShortcuts = ({
  state,
  selection,
  modalRef,
  cancelPrompt,
  closeModal,
  triggerFreePrompt,
  submitPrompt,
  prompts,
  setSelectedPromptIndex,
  publishModalAction
}) => {
  React.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) => isOfType(["prompt"], p));
            const nextItemIndex = promptsOnly.findIndex((p) => p === currentItem) + 1;
            const nextItem = promptsOnly[nextItemIndex];
            if (nextItem && 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) => isOfType(["prompt"], p));
            const nextItemIndex = Math.max(promptsOnly.findIndex((p) => p === currentItem) - 1, 0);
            const prevItem = promptsOnly[nextItemIndex];
            if (prevItem && 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]);
};
export {
  useAiModalShortcuts
};
//# sourceMappingURL=use-ai-modal-shortcuts.js.map
