"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_api_exports = {};
__export(use_ai_modal_api_exports, {
  useAiModalApi: () => useAiModalApi
});
module.exports = __toCommonJS(use_ai_modal_api_exports);
var import_react = __toESM(require("react"));
var import_prompts = require("../prompts.js");
var import_ts_utils = require("../ts-utils.js");
var import_use_session_storage = require("../utils/use-session-storage.js");
var import_use_ai_modal_actions = require("./slice/use-ai-modal-actions.js");
var import_use_ai_modal_reducer = require("./slice/use-ai-modal-reducer.js");
var import_use_ai_modal_selectors = require("./slice/use-ai-modal-selectors.js");
var import_use_ai_modal_shortcuts = require("./use-ai-modal-shortcuts.js");
const DISCARD_MODAL_STORAGE_KEY = "ricos-ai-discard-modal";
const useAiModalApi = ({
  selection,
  configurePrompts,
  getGatewayBaseUrl,
  appDefId,
  closeModal,
  openDiscardModal,
  modalRef,
  inputRef,
  listRef,
  publishModalAction,
  initialPromptOptions
}) => {
  const [dontShowDiscardModal, setDontShowDiscardModal] = (0, import_use_session_storage.useSessionStorage)(
    DISCARD_MODAL_STORAGE_KEY,
    false
  );
  const [isDiscardModalOpen, setIsDiscardModalOpen] = import_react.default.useState(false);
  const [prompts] = import_react.default.useState(
    () => configurePrompts ? configurePrompts(import_prompts.defaultPromptsConfiguration) : import_prompts.defaultPromptsConfiguration
  );
  const { state, dispatch } = (0, import_use_ai_modal_reducer.useAiModalReducer)(initialPromptOptions);
  const actions = (0, import_use_ai_modal_actions.useAiModalActions)({
    state,
    dispatch,
    getGatewayBaseUrl,
    appDefId,
    inputRef,
    listRef,
    selection,
    initialPromptOptions
  });
  const tryCloseModal = import_react.default.useCallback(async () => {
    if ((0, import_ts_utils.isOfType)(["INITIAL"], state) || dontShowDiscardModal) {
      closeModal();
      return;
    }
    setIsDiscardModalOpen(true);
    const discardResult = await openDiscardModal(inputRef.current);
    setIsDiscardModalOpen(false);
    if (discardResult.confirmed) {
      if (discardResult.dontShowAgain) {
        setDontShowDiscardModal(true);
      }
      closeModal();
      return;
    }
  }, [state.type, closeModal, openDiscardModal]);
  (0, import_use_ai_modal_shortcuts.useAiModalShortcuts)({
    state,
    selection,
    modalRef,
    closeModal,
    prompts,
    cancelPrompt: actions.cancelPrompt,
    triggerFreePrompt: actions.triggerFreePrompt,
    submitPrompt: actions.submitPrompt,
    setSelectedPromptIndex: actions.setSelectedPromptIndex,
    publishModalAction
  });
  const { getResultActions, textualPrompt, promptsList } = (0, import_use_ai_modal_selectors.useAiModalSelectors)({
    publishModalAction,
    selection,
    prompts,
    state,
    closeModal,
    cancelPrompt: actions.cancelPrompt,
    submitPrompt: actions.submitPrompt,
    contextualResultActions: initialPromptOptions?.contextualResultActions
  });
  return {
    ...actions,
    tryCloseModal,
    // Derived
    getResultActions,
    textualPrompt,
    promptsList,
    // State
    state,
    isDiscardModalOpen
  };
};
//# sourceMappingURL=use-ai-modal-api.js.map
