import { createTypedReducerWithImmer } from "@insite/client-framework/Common/CreateTypedReducer";
import { GetVmiBinsApiParameter } from "@insite/client-framework/Services/VmiBinsService";
import VmiBinsState from "@insite/client-framework/Store/Pages/VmiBins/VmiBinsState";
import { Draft } from "immer";

const initialState: VmiBinsState = {
    getVmiBinsParameter: {
        expand: ["product"],
    },
    filtersOpen: false,
    selectedVmiBins: {},
    selectVmiBinsChangedQty: {},
    isExportingVmiProducts: false,
};

export enum Actions {
    Add = "Add",
    Remove = "Remove",
}

const reducer = {
    "Pages/VmiBins/UpdateSearchFields": (draft: Draft<VmiBinsState>, action: { parameter: GetVmiBinsApiParameter }) => {
        draft.getVmiBinsParameter = { ...draft.getVmiBinsParameter, ...action.parameter };
    },
    "Pages/VmiBins/ClearParameter": (draft: Draft<VmiBinsState>) => {
        draft.getVmiBinsParameter = {
            ...initialState.getVmiBinsParameter,
            pageSize: draft.getVmiBinsParameter.pageSize,
            vmiLocationId: draft.getVmiBinsParameter.vmiLocationId,
        };
    },
    "Pages/VmiBins/ToggleFiltersOpen": (draft: Draft<VmiBinsState>) => {
        draft.filtersOpen = !draft.filtersOpen;
    },
    "Pages/VmiBins/ToggleBinCheck": (draft: Draft<VmiBinsState>, action: { ids: string[] }) => {
        for (const id of action.ids) {
            if (draft.selectedVmiBins[id]) {
                delete draft.selectedVmiBins[id];
            } else {
                draft.selectedVmiBins[id] = true;
            }
        }
    },
    "Pages/VmiBins/ClearVmiItemChecks": (draft: Draft<VmiBinsState>) => {
        draft.selectedVmiBins = {};
    },
    "Pages/VmiBins/HandleVmiBinsChangedQty": (
        draft: Draft<VmiBinsState>,
        action: { ids: string[]; action: string },
    ) => {
        if (!draft.selectVmiBinsChangedQty) {
            return;
        }
        for (const id of action.ids) {
            if (action.action === Actions[Actions.Add] && !draft.selectVmiBinsChangedQty[id]) {
                draft.selectVmiBinsChangedQty[id] = true;
            } else if (action.action === Actions[Actions.Remove]) {
                delete draft.selectVmiBinsChangedQty[id];
            }
        }
    },
    "Pages/VmiBins/ClearSelectedVmiBinsChangedQty": (draft: Draft<VmiBinsState>) => {
        draft.selectVmiBinsChangedQty = {};
    },
    "Pages/VmiBins/SetIsReorderConfirmationModalOpen": (
        draft: Draft<VmiBinsState>,
        action: { isReorderConfirmationModalOpen: boolean },
    ) => {
        draft.isReorderConfirmationModalOpen = action.isReorderConfirmationModalOpen;
    },
    "Pages/VmiBins/SetNextPageUrl": (draft: Draft<VmiBinsState>, action: { nextPageUrl?: string }) => {
        draft.nextPageUrl = action.nextPageUrl;
    },
    "Pages/VmiBins/SetNextAction": (draft: Draft<VmiBinsState>, action: { nextAction?: Function }) => {
        draft.nextAction = action.nextAction;
    },
    "Pages/VmiBins/BeginExportVmiProducts": (draft: Draft<VmiBinsState>) => {
        draft.isExportingVmiProducts = true;
    },
    "Pages/VmiBins/CompleteExportVmiProducts": (draft: Draft<VmiBinsState>) => {
        draft.isExportingVmiProducts = false;
    },
    "Pages/VmiBins/SetCartRequestInProcess": (
        draft: Draft<VmiBinsState>,
        action: { cartRequestInProcess: boolean },
    ) => {
        draft.cartRequestInProcess = action.cartRequestInProcess;
    },
};

export default createTypedReducerWithImmer(initialState, reducer);
