import React from "react";
import { isOfType } from "../../ts-utils.js";
const createInitialState = (initialPromptOptions) => ({
  type: "INITIAL",
  selectedPromptIndex: 0,
  textualPrompt: "",
  initialPrompt: initialPromptOptions?.prompt
});
const aiModalReducerCreator = (initialPromptOptions) => (state, action) => {
  switch (action.type) {
    case "SET_SELECTED_PROMPT_INDEX": {
      if (isOfType(["INITIAL"], state)) {
        return {
          ...state,
          selectedPromptIndex: action.payload.selectedPromptIndex
        };
      }
      return state;
    }
    case "SET_FREE_PROMPT_VALUE": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          ...state,
          textualPrompt: action.payload.value
        };
      }
      return state;
    }
    case "SUBMIT_TEXTUAL_PROMPT": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          type: "IN_PROGRESS",
          entries: [
            ...state.type === "RESULTS" ? state.entries : [],
            { type: "request", requestPrompt: state.textualPrompt }
          ],
          requestId: action.payload.requestId
        };
      }
      if (isOfType(["ERROR"], state)) {
        return {
          type: "IN_PROGRESS",
          requestId: action.payload.requestId,
          entries: state.entries
        };
      }
      return state;
    }
    case "SUBMIT_PROMPT": {
      if (isOfType(["INITIAL", "RESULTS"], state)) {
        return {
          type: "IN_PROGRESS",
          requestId: action.payload.requestId,
          entries: [
            ...state.type === "RESULTS" ? state.entries : [],
            { type: "request", requestPrompt: action.payload.prompt }
          ]
        };
      }
      if (isOfType(["ERROR"], state)) {
        return {
          type: "IN_PROGRESS",
          requestId: action.payload.requestId,
          entries: state.entries
        };
      }
      return state;
    }
    case "CANCEL_PROMPT": {
      if (state.type === "IN_PROGRESS") {
        const lastEntry = state.entries[state.entries.length - 1];
        return {
          type: "RESULTS",
          entries: state.entries.slice(0, -1),
          textualPrompt: lastEntry.type === "request" && typeof lastEntry.requestPrompt === "string" ? lastEntry.requestPrompt : ""
        };
      }
      if (isOfType(["RESULTS"], state)) {
        return createInitialState(initialPromptOptions);
      }
      return state;
    }
    case "FINISH_LOADING": {
      if (state.type === "IN_PROGRESS" && state.requestId === action.payload.requestId) {
        return {
          type: "RESULTS",
          entries: [
            ...state.entries,
            {
              type: "result",
              response: action.payload.suggestions[0],
              requestPrompt: state.entries[state.entries.length - 1].requestPrompt
            }
          ],
          textualPrompt: ""
        };
      }
      return state;
    }
    case "SET_ERROR": {
      if (isOfType(["IN_PROGRESS"], state)) {
        return {
          type: "ERROR",
          entries: state.entries,
          requestPrompt: action.payload.requestPrompt,
          requestContent: action.payload.requestContent
        };
      }
      return state;
    }
    case "GO_BACK": {
      if (isOfType(["ERROR"], state)) {
        const lastEntry = state.entries[state.entries.length - 1];
        return {
          type: "RESULTS",
          entries: state.entries.slice(0, -1),
          textualPrompt: lastEntry.type === "request" && typeof lastEntry.requestPrompt === "string" ? lastEntry.requestPrompt : ""
        };
      }
      return state;
    }
    default: {
      return state;
    }
  }
};
const useAiModalReducer = (initialPromptOptions) => {
  const initialState = React.useMemo(() => createInitialState(initialPromptOptions), []);
  const [state, dispatch] = React.useReducer(
    aiModalReducerCreator(initialPromptOptions),
    initialState
  );
  return { state, dispatch };
};
export {
  useAiModalReducer
};
//# sourceMappingURL=use-ai-modal-reducer.js.map
