"use client"
import { setCacheRedirectUrlCallback } from "@/lib/redux/features/payment";
import { getServicePath } from "@/lib/redux/features/serviceAdmin";
import { useAppDispatch, useAppSelector } from "@/lib/redux/hooks";
import { ServiceViewSettings } from "@/types/serviceAdmin";
import { useMounted } from "@/hooks/useMounted";
import { showToast } from "../toast/showToast";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import AppLoading from "../ui/loading";

type PropsType = { viewSettings?: ServiceViewSettings };


export default function GakkenPaymentProcessing (props: PropsType) {
    const {
    //   paymentActions: {
    //     resetPaymentError,
    //     gmoCharge,
    //     createOrderReceive,
    //     setCacheCouponCode,
    //     setCacheSubscriptionId,
    //     resetCreateOrderReceive,
    //     veritransCreateOrderReceive,
    //     resetCreateOrderReceiveVeritrans
    //   }
    } = props;
    const t = useTranslations("");
    const mounted = useMounted();
    const dispatch = useAppDispatch();
    const router = useRouter();
    // const { below768, getBelow768 } = useResponsive();
    // const params = useParams();
  
    const servicePath = useAppSelector(getServicePath);
    const [loading, setIsLoading] = useState(false);

    useEffect(() => {
        setIsLoading(true);
        if (mounted) {
            const search =window.location.search;
            const queryURL = new URLSearchParams(search);
            const gidOrderNo = queryURL.get('gid_order_no');
            const periodicPaymentId = queryURL.get('periodic_payment_id');
            const cancelPayment = queryURL.get('payment_type');
            const state = queryURL.get('state');
            dispatch(setCacheRedirectUrlCallback({
                gidOrderNo: gidOrderNo,
                periodicPaymentId: periodicPaymentId,
                state: state,
                cancelPayment: cancelPayment
            }));

            if (gidOrderNo) {
                router.push(`${servicePath ? '/'+servicePath : ''}/gakken-payment/confirm`);
            } else {
                if (typeof cancelPayment === 'string') {
                    router.push(`${state}`);
                    if (cancelPayment === '0') {
                        showToast('error', t('toast.success.cancelGakkenPayment'));
                    } else {
                        showToast('error', t('toast.error.payment'));
                    }
                }
            } 
        }
    }, [mounted]);
    return (
        <div>
            {loading && <AppLoading />}
        </div>
    );
};