import React from "react";
import { isOfType } from "../../ts-utils.js";
const createInitialState = ({
  imageStyle,
  convertSelectionToPrompt
}) => convertSelectionToPrompt ? { type: "INITIALIZING", imageStyle, requestId: "" } : {
  type: "INITIAL",
  textualPrompt: "",
  imageStyle,
  isDirty: false,
  validationError: void 0,
  rateLimitError: void 0
};
const aiModalStateReducer = (state, action) => {
  switch (action.type) {
    case "INITIALIZE": {
      if (isOfType(["INITIALIZING"], state)) {
        return {
          ...state,
          requestId: action.payload.requestId
        };
      }
      return state;
    }
    case "DONE_INITIALIZE": {
      if (isOfType(["INITIALIZING"], state) && state.requestId === action.payload.requestId) {
        return {
          type: "INITIAL",
          imageStyle: state.imageStyle,
          textualPrompt: action.payload.value,
          isDirty: false,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      return state;
    }
    case "SET_PROMPT_VALUE": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          ...state,
          textualPrompt: action.payload.value,
          isDirty: true,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      if (isOfType(["INITIALIZING"], state)) {
        return {
          type: "INITIAL",
          imageStyle: state.imageStyle,
          textualPrompt: action.payload.value,
          isDirty: true,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      return state;
    }
    case "SET_IMAGE_STYLE_VALUE": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          ...state,
          imageStyle: action.payload.value
        };
      }
      return state;
    }
    case "SUBMIT_PROMPT": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          type: "IN_PROGRESS",
          entries: [...state.type === "RESULTS" ? state.entries : [], { type: "GENERATING" }],
          requestId: action.payload.requestId,
          textualPrompt: state.textualPrompt,
          imageStyle: state.imageStyle,
          isDirty: state.isDirty
        };
      }
      if (isOfType(["ERROR"], state)) {
        return {
          type: "IN_PROGRESS",
          requestId: action.payload.requestId,
          entries: [...state.entries, { type: "GENERATING" }],
          textualPrompt: state.requestPrompt,
          imageStyle: state.requestImageStyle,
          isDirty: state.isDirty
        };
      }
      return state;
    }
    case "CANCEL_PROMPT": {
      if (state.type === "IN_PROGRESS") {
        if (state.entries.length === 1) {
          return {
            type: "INITIAL",
            textualPrompt: state.textualPrompt,
            imageStyle: state.imageStyle,
            isDirty: state.isDirty,
            validationError: void 0,
            rateLimitError: void 0
          };
        }
        return {
          type: "RESULTS",
          entries: state.entries.slice(0, -1),
          textualPrompt: state.textualPrompt,
          imageStyle: state.imageStyle,
          isDirty: state.isDirty,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      if (isOfType(["RESULTS"], state)) {
        return createInitialState({ imageStyle: state.imageStyle });
      }
      return state;
    }
    case "FINISH_LOADING": {
      if (state.type === "IN_PROGRESS" && state.requestId === action.payload.requestId) {
        return {
          type: "RESULTS",
          entries: [
            ...state.entries.slice(0, -1),
            { type: "GENERATED", url: action.payload.result, file: action.payload.file }
          ],
          textualPrompt: state.textualPrompt,
          imageStyle: state.imageStyle,
          isDirty: state.isDirty,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      return state;
    }
    case "SET_ERROR": {
      if (isOfType(["IN_PROGRESS"], state)) {
        return {
          type: "ERROR",
          entries: state.entries.slice(0, -1),
          requestPrompt: action.payload.requestPrompt,
          requestImageStyle: action.payload.requestImageStyle,
          isDirty: state.isDirty
        };
      }
      return state;
    }
    case "GO_BACK": {
      if (isOfType(["ERROR"], state)) {
        return state.entries.length === 0 ? {
          type: "INITIAL",
          textualPrompt: state.requestPrompt,
          imageStyle: state.requestImageStyle,
          isDirty: state.isDirty,
          validationError: void 0,
          rateLimitError: void 0
        } : {
          type: "RESULTS",
          entries: state.entries.slice(0, -1),
          textualPrompt: state.requestPrompt,
          imageStyle: state.requestImageStyle,
          isDirty: state.isDirty,
          validationError: void 0,
          rateLimitError: void 0
        };
      }
      return state;
    }
    case "SET_VALIDATION_ERROR": {
      if (isOfType(["IN_PROGRESS"], state)) {
        return {
          type: "INITIAL",
          textualPrompt: state.textualPrompt,
          imageStyle: state.imageStyle,
          isDirty: state.isDirty,
          validationError: action.payload,
          rateLimitError: void 0
        };
      }
      return state;
    }
    case "SET_RATE_LIMIT_ERROR": {
      if (isOfType(["IN_PROGRESS"], state)) {
        return {
          type: "INITIAL",
          textualPrompt: state.textualPrompt,
          imageStyle: state.imageStyle,
          isDirty: state.isDirty,
          validationError: void 0,
          rateLimitError: action.payload
        };
      }
      return state;
    }
    default: {
      return state;
    }
  }
};
const useAiImageModalReducer = (options) => {
  const initialState = React.useMemo(() => createInitialState(options), []);
  const [state, dispatch] = React.useReducer(aiModalStateReducer, initialState);
  return { state, dispatch };
};
export {
  useAiImageModalReducer
};
//# sourceMappingURL=use-ai-image-modal-reducer.js.map
