import { createTypedReducerWithImmer } from "@insite/client-framework/Common/CreateTypedReducer";
import CheckoutReviewAndSubmitState from "@insite/client-framework/Store/Pages/CheckoutReviewAndSubmit/CheckoutReviewAndSubmitState";
import { Draft } from "immer";

const initialState: CheckoutReviewAndSubmitState = {
    isPlacingOrder: false,
    isApplyingPromotion: false,
    isRemovingPromotion: false,
    isRemovingPromotions: false,
    isCheckingOutWithPayPay: false,
    isPreloadingData: false,
    requestedDeliveryDate: null,
    isPromotionAppliedSuccess: false,
    isPromotionRemovedSuccess: false,
    isPromotionsRemovedSuccess: false,
    paymetricIFrameIsSet: false,
    validateForm: () => true,
};

const reducer = {
    "Pages/CheckoutReviewAndSubmit/SetCartId": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { cartId?: string },
    ) => {
        draft.cartId = action.cartId;
    },
    "Pages/CheckoutReviewAndSubmit/BeginApplyPromotion": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isApplyingPromotion = true;
    },
    "Pages/CheckoutReviewAndSubmit/CompleteApplyPromotion": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { successMessage?: string; errorMessage?: string; isPromotionAppliedSuccess: boolean },
    ) => {
        draft.isApplyingPromotion = false;
        draft.promotionErrorMessage = action.errorMessage;
        draft.promotionSuccessMessage = action.successMessage;
        draft.isPromotionAppliedSuccess = action.isPromotionAppliedSuccess;

        draft.isPromotionRemovedSuccess = false;
        draft.isPromotionsRemovedSuccess = false;
        draft.removePromotionSuccessMessage = undefined;
        draft.removePromotionsSuccessMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/BeginRemovePromotion": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isRemovingPromotion = true;
    },
    "Pages/CheckoutReviewAndSubmit/CompleteRemovePromotion": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { successMessage?: string; errorMessage?: string; isPromotionRemovedSuccess: boolean },
    ) => {
        draft.isRemovingPromotion = false;
        draft.removePromotionSuccessMessage = action.successMessage;
        draft.removePromotionErrorMessage = action.errorMessage;
        draft.isPromotionRemovedSuccess = action.isPromotionRemovedSuccess;

        draft.isPromotionAppliedSuccess = false;
        draft.isPromotionsRemovedSuccess = false;
        draft.promotionSuccessMessage = undefined;
        draft.promotionErrorMessage = undefined;
        draft.removePromotionsSuccessMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/BeginRemovePromotions": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isRemovingPromotion = true;
    },
    "Pages/CheckoutReviewAndSubmit/CompleteRemovePromotions": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { successMessage?: string; errorMessage?: string; isPromotionsRemovedSuccess: boolean },
    ) => {
        draft.isRemovingPromotions = false;
        draft.removePromotionsSuccessMessage = action.successMessage;
        draft.removePromotionsErrorMessage = action.errorMessage;
        draft.isPromotionsRemovedSuccess = action.isPromotionsRemovedSuccess;

        draft.isPromotionAppliedSuccess = false;
        draft.isPromotionRemovedSuccess = false;
        draft.promotionSuccessMessage = undefined;
        draft.promotionErrorMessage = undefined;
        draft.removePromotionSuccessMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/BeginPlaceOrder": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isPlacingOrder = true;
        draft.placeOrderErrorMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/SetPlaceOrderErrorMessage": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { errorMessage?: string },
    ) => {
        draft.placeOrderErrorMessage = action.errorMessage;
    },
    "Pages/CheckoutReviewAndSubmit/CompletePlaceOrder": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isPlacingOrder = false;
        draft.cartId = undefined;
        draft.requestedDeliveryDate = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/FailedToPlaceOrder": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { errorMessage?: string },
    ) => {
        draft.isPlacingOrder = false;
        draft.placeOrderErrorMessage = action.errorMessage;
    },
    "Pages/CheckoutReviewAndSubmit/BeginCheckoutWithPayPal": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isCheckingOutWithPayPay = true;
        draft.payPalCheckoutErrorMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/SetPayPalCheckoutErrorMessage": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { errorMessage?: string },
    ) => {
        draft.payPalCheckoutErrorMessage = action.errorMessage;
    },
    "Pages/CheckoutReviewAndSubmit/CompleteCheckoutWithPayPal": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { redirectUri: string },
    ) => {
        draft.payPalRedirectUri = action.redirectUri;
    },
    "Pages/CheckoutReviewAndSubmit/ClearMessages": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.promotionErrorMessage = undefined;
        draft.promotionSuccessMessage = undefined;
        draft.removePromotionErrorMessage = undefined;
        draft.removePromotionSuccessMessage = undefined;
        draft.removePromotionsErrorMessage = undefined;
        draft.removePromotionsSuccessMessage = undefined;
        draft.isPromotionAppliedSuccess = false;
        draft.isPromotionRemovedSuccess = false;
        draft.isPromotionsRemovedSuccess = false;
    },
    "Pages/CheckoutReviewAndSubmit/SetRequestedDeliveryDate": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { requestedDeliveryDate?: Date; requestedDeliveryDateDisabled?: boolean },
    ) => {
        draft.requestedDeliveryDate = action.requestedDeliveryDate;
        draft.requestedDeliveryDateDisabled = action.requestedDeliveryDateDisabled;
    },
    "Pages/CheckoutReviewAndSubmit/SetRequestedPickUpDate": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { requestedPickUpDate?: Date; requestedPickUpDateDisabled?: boolean },
    ) => {
        draft.requestedPickupDate = action.requestedPickUpDate;
        draft.requestedPickUpDateDisabled = action.requestedPickUpDateDisabled;
    },
    "Pages/CheckoutReviewAndSubmit/BeginPlaceOrderForApproval": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isPlacingOrder = true;
        draft.placeOrderErrorMessage = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/CompletePlaceOrderForApproval": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.isPlacingOrder = false;
        draft.cartId = undefined;
        draft.requestedDeliveryDate = undefined;
    },
    "Pages/CheckoutReviewAndSubmit/SetIsPreloadingData": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { isPreloadingData: boolean },
    ) => {
        draft.isPreloadingData = action.isPreloadingData;
    },
    "Pages/CheckoutReviewAndSubmit/SetProductRecsCalled": (draft: Draft<CheckoutReviewAndSubmitState>) => {
        draft.productRecsCalled = true;
    },
    "Pages/CheckoutReviewAndSubmit/PaymetricIFrameIsSet": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { paymetricIFrameIsSet: boolean },
    ) => {
        draft.paymetricIFrameIsSet = action.paymetricIFrameIsSet;
    },
    "Pages/CheckoutReviewAndSubmit/SetPONumber": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { poNumber: string },
    ) => {
        draft.poNumber = action.poNumber;
    },
    "Pages/CheckoutReviewAndSubmit/SetValidateForm": (
        draft: Draft<CheckoutReviewAndSubmitState>,
        action: { validateForm: () => boolean },
    ) => {
        draft.validateForm = action.validateForm;
    },
};

export default createTypedReducerWithImmer(initialState, reducer);
