/* eslint-disable no-unsafe-optional-chaining */ /* eslint-disable react/jsx-no-constructed-context-values */ import type { Dispatch, PropsWithChildren } from "react"; import { createContext, useContext, useReducer } from "react"; import localStateCache from "@/libs/helpers/local-state-cache-helper"; import type { IOptionalAddOns } from "@/services/eva-display/hotel/hotel-add-to-cart/hotel-add-to-card-services.types"; import { useHotelSummary } from "../hooks/use-hotel-summary"; type SelectedOptionalAddOnsType = { addOn: IOptionalAddOns; selectedPassengerValues: Array>; }; interface SummaryCtxDataTypes { optionalAddOns: SelectedOptionalAddOnsType[] | null; cancelWarranty: { searchId: string; totalPrice: string; totalExchangePrice: string; description: string; name: string; } | null; campaing: { key?: any; code?: any; name: string; discountAmount?: any; discountAmountDefaultCurrency?: any; } | null; totalPrice: number; tryTotalPrice: number; } interface CancelWarrantyActionPayload { cancelWarranty: SummaryCtxDataTypes["cancelWarranty"]; totalPrice: SummaryCtxDataTypes["totalPrice"]; tryTotalPrice: SummaryCtxDataTypes["tryTotalPrice"]; } interface CampaingActionPayload { campaing: SummaryCtxDataTypes["campaing"]; totalPrice: SummaryCtxDataTypes["totalPrice"]; tryTotalPrice: SummaryCtxDataTypes["tryTotalPrice"]; } interface OptionalAddOnsActionPayload { optionalAddOns: SelectedOptionalAddOnsType[]; totalPrice: SummaryCtxDataTypes["totalPrice"]; tryTotalPrice: SummaryCtxDataTypes["tryTotalPrice"]; } const SET_CANCELWARRANTY_TYPE = "SET_CANCELWARRANTY_TYPE"; const SET_CAMPAING_TYPE = "SET_CAMPAING_TYPE"; const SET_OPTIONAL_ADDONS = "SET_OPTIONAL_ADDONS"; function setCancelWarranty(payload: CancelWarrantyActionPayload) { return { type: SET_CANCELWARRANTY_TYPE, payload, } as const; } function setCampaing(payload: CampaingActionPayload) { return { type: SET_CAMPAING_TYPE, payload, } as const; } function setOptionalAddOns(payload: OptionalAddOnsActionPayload) { return { type: SET_OPTIONAL_ADDONS, payload, } as const; } export type SummaryCtxActionType = | ReturnType | ReturnType | ReturnType; const summaryReducer = ( state: SummaryCtxDataTypes, action: SummaryCtxActionType, ): SummaryCtxDataTypes => { switch (action.type) { case SET_CANCELWARRANTY_TYPE: return { ...state, cancelWarranty: action.payload.cancelWarranty, totalPrice: action.payload.totalPrice, tryTotalPrice: action.payload.tryTotalPrice, }; case SET_CAMPAING_TYPE: return { ...state, campaing: action.payload.campaing, totalPrice: action.payload.totalPrice, tryTotalPrice: action.payload.tryTotalPrice, }; case SET_OPTIONAL_ADDONS: return { ...state, totalPrice: action.payload.totalPrice, tryTotalPrice: action.payload.tryTotalPrice, optionalAddOns: action.payload.optionalAddOns, }; default: return state; } }; export const SummaryContext = createContext<{ state: SummaryCtxDataTypes; dispatch: Dispatch; }>( {} as { state: SummaryCtxDataTypes; dispatch: Dispatch; }, ); export const SummaryCtxProvider: React.FC< PropsWithChildren> > = ({ children, totalPrice, tryTotalPrice }) => { const { data: summaryData } = useHotelSummary(); const cachedAddons = localStateCache.get("addons"); if (cachedAddons) { if ( summaryData && summaryData.payload.searchItemKey !== cachedAddons?.productKey ) { localStateCache.delete("addons"); } } const initializerArg = { cancelWarranty: null, optionalAddOns: localStateCache.get("addons")?.data || null, campaing: null, totalPrice, tryTotalPrice, } as SummaryCtxDataTypes; const [state, dispatch] = useReducer(summaryReducer, initializerArg); return ( {children} ); }; export const useSummaryCtxState = () => { const { state } = useContext(SummaryContext); return state; }; export const useSummaryCtxActions = () => { const { dispatch, state } = useContext(SummaryContext); return { setCancelWarranty: (payload: CancelWarrantyActionPayload) => dispatch(setCancelWarranty(payload)), setCampaing: (payload: CampaingActionPayload) => dispatch(setCampaing(payload)), clearCampaign: () => dispatch( setCampaing({ ...state, campaing: null, totalPrice: state.totalPrice + (!state.campaing ? 0 : state.campaing?.discountAmountDefaultCurrency.slice(3)), tryTotalPrice: state.tryTotalPrice + (!state.campaing ? 0 : state.campaing?.discountAmount.slice(3)), }), ), setOptionalAddOns: (payload: OptionalAddOnsActionPayload) => dispatch(setOptionalAddOns(payload)), }; };