import { createTypedReducerWithImmer } from "@insite/client-framework/Common/CreateTypedReducer";
import { ProcessFileResult } from "@insite/client-framework/Store/Components/VmiUsersImportModal/Handlers/ProcessFile";
import { BatchCreateVmiUsersResult } from "@insite/client-framework/Store/Components/VmiUsersImportModal/Handlers/ValidateVmiUsers";
import VmiUsersImportModalState from "@insite/client-framework/Store/Components/VmiUsersImportModal/VmiUsersImportModalState";
import { Draft } from "immer";

const initialState: VmiUsersImportModalState = {
    parsedItems: [],
    importedItems: [],
    errorEntries: [],
    isBadFile: false,
    isUploading: false,
    vmiUsersProcessed: false,
    allowCancel: false,
    uploadCancelled: false,
    uploadLimitExceeded: false,
    errorsModalIsOpen: false,
    modalIsOpen: false,
    fullFileError: false,
    continueUpload: false,
};

const reducer = {
    "Components/VmiUsersImportModal/BeginValidateVmiUsers": (draft: Draft<VmiUsersImportModalState>) => {
        draft.vmiUsersProcessed = false;
    },
    "Components/VmiUsersImportModal/CompleteValidateVmiUsers": (
        draft: Draft<VmiUsersImportModalState>,
        action: { result: BatchCreateVmiUsersResult },
    ) => {
        draft.importedItems = action.result.importedItems;
        draft.errorEntries = action.result.errorEntries;
        draft.vmiUsersProcessed = true;
    },
    "Components/VmiUsersImportModal/CleanupUploadData": (draft: Draft<VmiUsersImportModalState>) => {
        draft.importedItems = [];
        draft.errorEntries = [];
        draft.continueUpload = false;
        draft.vmiUsersProcessed = false;
    },
    "Components/VmiUsersImportModal/CompleteProcessFile": (
        draft: Draft<VmiUsersImportModalState>,
        action: { result: ProcessFileResult },
    ) => {
        draft.parsedItems = action.result.parsedItems;
        draft.uploadLimitExceeded = action.result.uploadLimitExceeded;
        draft.isBadFile = action.result.isBadFile;
        draft.isUploading = action.result.isUploading;
        draft.allowCancel = action.result.allowCancel;
    },
    "Components/VmiUsersImportModal/SetAllowCancel": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { allowCancel: boolean } },
    ) => {
        draft.allowCancel = action.parameter.allowCancel;
    },
    "Components/VmiUsersImportModal/SetContinueUpload": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { continueUpload: boolean } },
    ) => {
        draft.continueUpload = action.parameter.continueUpload;
    },
    "Components/VmiUsersImportModal/SetErrorsModalIsOpen": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { errorsModalIsOpen: boolean } },
    ) => {
        draft.errorsModalIsOpen = action.parameter.errorsModalIsOpen;
    },
    "Components/VmiUsersImportModal/SetIsBadFile": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { isBadFile: boolean } },
    ) => {
        draft.isBadFile = action.parameter.isBadFile;
    },
    "Components/VmiUsersImportModal/SetIsUploading": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { isUploading: boolean } },
    ) => {
        draft.isUploading = action.parameter.isUploading;
    },
    "Components/VmiUsersImportModal/SetUploadCancelled": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { uploadCancelled: boolean } },
    ) => {
        draft.uploadCancelled = action.parameter.uploadCancelled;
    },
    "Components/VmiUsersImportModal/SetUploadLimitExceeded": (
        draft: Draft<VmiUsersImportModalState>,
        action: { parameter: { uploadLimitExceeded: boolean } },
    ) => {
        draft.uploadLimitExceeded = action.parameter.uploadLimitExceeded;
    },
    "Components/VmiUsersImportModal/SetModalIsOpen": (
        draft: Draft<VmiUsersImportModalState>,
        action: { isOpen: boolean },
    ) => {
        draft.modalIsOpen = action.isOpen;
    },
    "Components/VmiUsersImportModal/SetFullFileError": (
        draft: Draft<VmiUsersImportModalState>,
        action: { fullFileError: boolean },
    ) => {
        draft.fullFileError = action.fullFileError;
    },
};

export default createTypedReducerWithImmer(initialState, reducer);
