import parseQueryString from "@insite/client-framework/Common/Utilities/parseQueryString";
import Zone from "@insite/client-framework/Components/Zone";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import loadOrderStatusMappings from "@insite/client-framework/Store/Data/OrderStatusMappings/Handlers/LoadOrderStatusMappings";
import { getOrderStatusMappingDataView } from "@insite/client-framework/Store/Data/OrderStatusMappings/OrderStatusMappingsSelectors";
import { getLocation } from "@insite/client-framework/Store/Data/Pages/PageSelectors";
import preloadConfirmationData from "@insite/client-framework/Store/Pages/OnePageCheckoutConfirmation/Handlers/PreloadConfirmationData";
import setIsPreloadingData from "@insite/client-framework/Store/Pages/OnePageCheckoutConfirmation/Handlers/SetIsPreloadingData";
import PageModule from "@insite/client-framework/Types/PageModule";
import PageProps from "@insite/client-framework/Types/PageProps";
import Page from "@insite/mobius/Page";
import { HasToasterContext, withToaster } from "@insite/mobius/Toast/ToasterContext";
import React, { useEffect } from "react";
import { connect, ResolveThunks } from "react-redux";

const mapStateToProps = (state: ApplicationState) => ({
    isPreloadingData: state.pages.onePageCheckoutConfirmation.isPreloadingData,
    location: getLocation(state),
    shouldLoadOrderStatusMappings: !getOrderStatusMappingDataView(state).value,
});

const mapDispatchToProps = {
    setIsPreloadingData,
    preloadConfirmationData,
    loadOrderStatusMappings,
};

type Props = PageProps &
    ReturnType<typeof mapStateToProps> &
    ResolveThunks<typeof mapDispatchToProps> &
    HasToasterContext;

const OnePageCheckoutConfirmationPage = ({
    id,
    location,
    toaster,
    isPreloadingData,
    setIsPreloadingData,
    preloadConfirmationData,
    shouldLoadOrderStatusMappings,
    loadOrderStatusMappings,
}: Props) => {
    useEffect(() => {
        const parsedQuery = parseQueryString<{ cartId?: string }>(location.search);
        const cartId = parsedQuery.cartId;
        if (cartId && !isPreloadingData) {
            preloadConfirmationData({
                cartId,
                onSuccess: () => {
                    setIsPreloadingData({ isPreloadingData: false });
                },
                onError: (errorMessage: string) => {
                    toaster.addToast({ body: errorMessage, messageType: "danger", timeoutLength: 9000 });
                },
            });
        } else {
            setIsPreloadingData({ isPreloadingData: false });
        }
    }, []);

    useEffect(() => {
        if (shouldLoadOrderStatusMappings) {
            loadOrderStatusMappings();
        }
    }, [shouldLoadOrderStatusMappings]);

    return (
        <Page>
            <Zone zoneName="Content" contentId={id} />
        </Page>
    );
};

const pageModule: PageModule = {
    component: connect(mapStateToProps, mapDispatchToProps)(withToaster(OnePageCheckoutConfirmationPage)),
    definition: {
        hasEditableUrlSegment: true,
        hasEditableTitle: true,
        pageType: "System",
    },
};

export default pageModule;
