"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_image_modal_api_exports = {};
__export(use_ai_image_modal_api_exports, {
  useAiImageModalApi: () => useAiImageModalApi
});
module.exports = __toCommonJS(use_ai_image_modal_api_exports);
var import_react = __toESM(require("react"));
var import_ts_utils = require("../ts-utils.js");
var import_use_session_storage = require("../utils/use-session-storage.js");
var import_use_ai_image_modal_actions = require("./slice/use-ai-image-modal-actions.js");
var import_use_ai_image_modal_reducer = require("./slice/use-ai-image-modal-reducer.js");
var import_use_ai_image_modal_shortcuts = require("./use-ai-image-modal-shortcuts.js");
const DISCARD_MODAL_STORAGE_KEY = "ricos-ai-discard-modal";
const IMAGE_STYLE_STORAGE_KEY = "ricos-ai-image-style";
const CONVERTED_PROMPT_STORAGE_KEY = "ricos-ai-image-converted-prompt";
const useAiImageModalApi = ({
  closeModal,
  openDiscardModal,
  focusInputArea,
  modalRef,
  getGatewayBaseUrl,
  appDefId,
  convertSelectionToPrompt,
  publishModalAction
}) => {
  const [dontShowDiscardModal, setDontShowDiscardModal] = (0, import_use_session_storage.useSessionStorage)(
    DISCARD_MODAL_STORAGE_KEY,
    false
  );
  const [savedImageStyle, saveImageStyle] = (0, import_use_session_storage.useSessionStorage)(
    IMAGE_STYLE_STORAGE_KEY,
    "NONE"
  );
  const [savedConvertedPromptHelperValue, setSavedConvertedPromptHelperValue] = (0, import_use_session_storage.useSessionStorage)(
    CONVERTED_PROMPT_STORAGE_KEY,
    true
  );
  const [isConvertedPromptHelperVisible, setConvertedPromptHelperVisible] = import_react.default.useState(false);
  const [isDiscardModalOpen, setIsDiscardModalOpen] = import_react.default.useState(false);
  const { state, dispatch } = (0, import_use_ai_image_modal_reducer.useAiImageModalReducer)({
    imageStyle: savedImageStyle,
    convertSelectionToPrompt
  });
  const {
    tryConvertingSelectionToPrompt,
    setPromptValue,
    setImageStyleValue,
    submitPrompt,
    cancelPrompt,
    insertImage,
    retryPrompt,
    goBack
  } = (0, import_use_ai_image_modal_actions.useAiImageModalActions)({
    state,
    dispatch,
    getGatewayBaseUrl,
    appDefId,
    focusInputArea,
    saveImageStyle,
    savedConvertedPromptHelperValue,
    setConvertedPromptHelperVisible,
    publishModalAction
  });
  const tryCloseModal = import_react.default.useCallback(async () => {
    publishModalAction("close_on_button_click");
    if ((0, import_ts_utils.isOfType)(["INITIAL"], state) || dontShowDiscardModal) {
      closeModal();
      return false;
    }
    setIsDiscardModalOpen(true);
    const discardResult = await openDiscardModal(modalRef);
    setIsDiscardModalOpen(false);
    if (discardResult.confirmed) {
      if (discardResult.dontShowAgain) {
        setDontShowDiscardModal(true);
      }
      closeModal();
      return true;
    }
  }, [state.type, closeModal, openDiscardModal]);
  const closeOptimisedPromptHelper = import_react.default.useCallback(async () => {
    setConvertedPromptHelperVisible(false);
    setSavedConvertedPromptHelperValue(false);
    publishModalAction("close_on_button_click");
  }, []);
  (0, import_use_ai_image_modal_shortcuts.useAiImageModalShortcuts)({
    state,
    modalRef,
    cancelPrompt,
    closeModal
  });
  return {
    tryCloseModal,
    isDiscardModalOpen,
    closeOptimisedPromptHelper,
    // Actions
    tryConvertingSelectionToPrompt,
    setPromptValue,
    setImageStyleValue,
    submitPrompt,
    cancelPrompt,
    retryPrompt,
    goBack,
    insertImage,
    // State
    state,
    isConvertedPromptHelperVisible
  };
};
//# sourceMappingURL=use-ai-image-modal-api.js.map
