import React from "react";
import { isOfType } from "../ts-utils.js";
import { useSessionStorage } from "../utils/use-session-storage.js";
import { useAiImageModalActions } from "./slice/use-ai-image-modal-actions.js";
import { useAiImageModalReducer } from "./slice/use-ai-image-modal-reducer.js";
import { useAiImageModalShortcuts } from "./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] = useSessionStorage(
    DISCARD_MODAL_STORAGE_KEY,
    false
  );
  const [savedImageStyle, saveImageStyle] = useSessionStorage(
    IMAGE_STYLE_STORAGE_KEY,
    "NONE"
  );
  const [savedConvertedPromptHelperValue, setSavedConvertedPromptHelperValue] = useSessionStorage(
    CONVERTED_PROMPT_STORAGE_KEY,
    true
  );
  const [isConvertedPromptHelperVisible, setConvertedPromptHelperVisible] = React.useState(false);
  const [isDiscardModalOpen, setIsDiscardModalOpen] = React.useState(false);
  const { state, dispatch } = useAiImageModalReducer({
    imageStyle: savedImageStyle,
    convertSelectionToPrompt
  });
  const {
    tryConvertingSelectionToPrompt,
    setPromptValue,
    setImageStyleValue,
    submitPrompt,
    cancelPrompt,
    insertImage,
    retryPrompt,
    goBack
  } = useAiImageModalActions({
    state,
    dispatch,
    getGatewayBaseUrl,
    appDefId,
    focusInputArea,
    saveImageStyle,
    savedConvertedPromptHelperValue,
    setConvertedPromptHelperVisible,
    publishModalAction
  });
  const tryCloseModal = React.useCallback(async () => {
    publishModalAction("close_on_button_click");
    if (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 = React.useCallback(async () => {
    setConvertedPromptHelperVisible(false);
    setSavedConvertedPromptHelperValue(false);
    publishModalAction("close_on_button_click");
  }, []);
  useAiImageModalShortcuts({
    state,
    modalRef,
    cancelPrompt,
    closeModal
  });
  return {
    tryCloseModal,
    isDiscardModalOpen,
    closeOptimisedPromptHelper,
    // Actions
    tryConvertingSelectionToPrompt,
    setPromptValue,
    setImageStyleValue,
    submitPrompt,
    cancelPrompt,
    retryPrompt,
    goBack,
    insertImage,
    // State
    state,
    isConvertedPromptHelperVisible
  };
};
export {
  useAiImageModalApi
};
//# sourceMappingURL=use-ai-image-modal-api.js.map
