import { createSlice } from "@reduxjs/toolkit";
import { REQ_STATUS } from "@api/config";
import {
    buildReqReducer,
    reqBankruptcyGift,
    reqChangePayPass,
    reqCPFSave,
    reqFirstPayConfig,
    reqFirstPayGift,
    reqFirstPayGiftClaim,
    reqFirstPayMulti,
    reqForgetPayPass, reqGameInfo,
    reqGetAccountsPayInfo,
    reqGetSecond, reqKycEU,
    reqMexWithdrawSetInfo,
    reqPaymentOrder,
    reqPaymentOrderQR,
    reqPayOrderList,
    reqRechargeConfig,
    reqRuWithdrawSetInfo,
    reqWithdrawBaseInfo,
    reqWithdrawInfo,
    reqWithdrawPayout,
    reqWithdrawRecord,
    reqWithdrawSetInfo,
    reqWithdrawTax
} from "@features/api";
import { getHttpMsg } from "@utils/helpers";
import { toast } from "react-toastify";

const i18n = global.i18n;
const objInfo = {}

const reducerBankruptcyGift = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        if (payload.data) {
            state.bankruptcyGift = payload.data;
        } else {
            state.bankruptcyGift = null;
        }
    } else {
        state.bankruptcyGift = null;
    }
}

const reducerRechargeConfig = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        if (data.list) {
            state.config = data.list;
            state.showBonus = data.bonus_show;
            state.cpfFirst = data.cpf_first;
            // state.showBonus=true;
        }
    }
}
const rechargeConfigStatus = (state, status, arg) => {
    state.status.RechargeConfig = status;
}
const reducerFirstPayConfig = (state, action, arg) => {
    let payload = action.payload;
    // payload={
    //     "data": {
    //         "first_pay": {
    //             "id": "1017",
    //             "money": "10",
    //             "gear": "[{\"id\":\"28\",\"name\":\"PIX\\u76f4\\u8fde\",\"status\":1,\"type\":3}]",
    //             "status": "1",
    //             "created_at": "2022-06-09 13:57:58.000",
    //             "favorable_price": 20,
    //             "image": "11",
    //             "give": "10",
    //             "first_pay": "1",
    //             "name": "\u9996\u5145\u6863\u4f4d\u914d\u7f6e",
    //             "gift_id": "201",
    //             "channels": "-1"
    //         },
    //         "recomendar_recharge": "7"
    //     },
    //     "result": "",
    //     "msg": "success",
    //     "code": 200
    // };
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        state.firstconfig = data;
    }
}
const firstPayConfigStatus = (state, status, arg) => {
    state.status.FirstPayConfig = status;
}

const clearGiftTimer = () => {
    if (objInfo.giftTimer != null) {
        clearTimeout(objInfo.giftTimer);
        objInfo.giftTimer = null;
    }
}
const checkFirstPayGiftLeftTime = (data) => {
    clearGiftTimer();
    if (data == null) return;
    if (data.has_purchased) return;
    let timeleft = data.time_left;
    if (isNaN(timeleft) || timeleft <= 0) return;
    data.time_end = Date.now();

    data.time_end += timeleft * 1000;
    objInfo.giftTimer = setTimeout(() => {
        clearGiftTimer();
        global.dispatch(reqFirstPayGift());
    }, timeleft * 1000);
}
const reducerFirstPayGift = (state, action, arg) => {
    let payload = action.payload;
    //{"data":{"list":[{"money":"9.99","favorable_price":9.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":1},{"money":"19.99","favorable_price":19.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"39.99","favorable_price":39.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"59.99","favorable_price":59.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"99.99","favorable_price":99.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"199.99","favorable_price":199.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"299.00","favorable_price":299,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"499.99","favorable_price":499.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0}],"gift_info":{"gift_id":"301","gift_name":"\u9996\u5145\u793c\u5305","total_bonus":"270","bonus_instantly":"120","day_rewards":{"total_bonus":20,"bonus_day":6,"start_day":2,"bonus":[2.5,2.5,3,3,4,5]},"betting_bonus":{"total_bonus":50,"per_bet":100,"per_bet_bonus":2},"betting_task":{"total_bonus":80,"bet_pay_times":60}},"time_left":84084,"expire_at":1761810819},"result":"","msg":"success","code":200}
    // payload = JSON.parse(`{"data":{"list":[{"money":"9.99","favorable_price":9.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":1},{"money":"19.99","favorable_price":19.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"39.99","favorable_price":39.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"59.99","favorable_price":59.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"99.99","favorable_price":99.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"199.99","favorable_price":199.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"299.00","favorable_price":299,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0},{"money":"499.99","favorable_price":499.99,"give":".00","gear":"[{\\"id\\":\\"28\\",\\"name\\":\\"AmPay\\",\\"status\\":\\"1\\",\\"type\\":\\"2\\"}]","recommend":0}],"gift_info":{"gift_id":"301","gift_name":"\u9996\u5145\u793c\u5305","total_bonus":"270","bonus_instantly":"120","day_rewards":{"total_bonus":20,"bonus_day":6,"start_day":2,"bonus":[2.5,2.5,3,3,4,5]},"betting_bonus":{"total_bonus":50,"per_bet":100,"per_bet_bonus":2},"betting_task":{"total_bonus":80,"bet_pay_times":60}},"time_left":84084,"expire_at":1761810819},"result":"","msg":"success","code":200}`)
    // payload.data.expire_at = Math.floor(Date.now() / 1000 + payload.data.time_left);
    if (payload != null && payload.code === 200 && payload.data != null && !Array.isArray(payload.data)) {
        state.firstPayGift = payload.data;
        checkFirstPayGiftLeftTime(payload.data);
    } else {
        state.firstPayGift = null;
        checkFirstPayGiftLeftTime(null);
    }
}
const reducerFirstPayGiftClaim = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        setTimeout(() => {
            global.dispatch(reqFirstPayGift())
        }, 0)
    } else {
        if (payload != null) {
            let msg = getHttpMsg(payload);
            toast.error(msg, { toastId: "firstPayGiftClaim" });
        }
    }
}
const firstPayGiftStatus = (state, status, arg) => {
    state.status.FirstPayGift = status;
}
const firstPayGiftClaimStatus = (state, status, arg) => {
    state.status.FirstPayGiftClaim = status;
}
const reducerGetAccountsPayInfo = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        state.payconfig = data;
    }
}
const getAccountsPayInfoStatus = (state, status, arg) => {
    state.status.GetAccountsPayInfo = status;
}
const reducerPaymentOrder = (state, action, arg) => {
    let payload = action.payload;
    // payload={
    //     "data": {
    //         "content": "00020101021226830014br.gov.bcb.pix2561pix.delbank.com.br\/v1\/qrcode\/vcharge22e43499260b46a9a5f303ca55204000053039865802BR5907DELBANK6007ARACAJU62070503***6304F3DF",
    //         "money": 10,
    //         "prdOrdNo": "BR5050013857473344515256"
    //     },
    //     "result": "",
    //     "msg": "success",
    //     "code": 200
    // };

    if (payload != null && payload.code === 200) {
        let data = payload.data;
        data.createAt = Date.now();
        if (data.deadline == null) data.deadline = 15 * 60;
        data.status = "pending";
        // data.qr_link="https://qr.nspk.ru/AS10003P3RH0LJ2A9ROO038L6NT5RU1M?type=01&bank=000000000001&sum=10000&cur=RUB&crc=F3D0";
        data.arg = Object.assign({}, arg, { callback: null, user: null });
        state.currPayOrder = data;
        state.payFinishFlag = 0;
        if (arg.callback != null) arg.callback();
    }
    else {
        if (payload != null) {
            let msg = getHttpMsg(payload);
            toast.error(msg, { toastId: "paymentOrder" });
            let data = payload.data;
            if (data != null && Array.isArray(data)) {
                state.kycTypes = data;
            }
        }
        if (arg.callbackError != null) arg.callbackError(payload);
    }
}
const paymentOrderStatus = (state, status, arg) => {
    state.status.PaymentOrder = status;
}
const paymentOrderQRStatus = (state, status, arg) => {
    state.status.PaymentOrderQR = status;
}
const reducerPayOrderList = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        //data = JSON.parse(`[{"amount":7957,"created_at":"2025-11-03 05:37:43.0102","finished_at":"2025-11-03 10:11:52.0283","pay_status":1,"payment_sn":"09852"},{"amount":79612,"created_at":"2025-11-03 17:32:22.0669","finished_at":"2025-11-03 05:29:24.0609","pay_status":2,"payment_sn":"00902"},{"amount":74479,"created_at":"2025-11-03 01:24:14.0380","finished_at":"2025-11-03 12:20:01.0712","pay_status":2,"payment_sn":"03817"},{"amount":89349,"created_at":"2025-11-03 01:41:09.0173","finished_at":"2025-11-03 10:55:28.0092","pay_status":0,"payment_sn":"01257"},{"amount":35835,"created_at":"2025-11-03 15:45:48.0960","finished_at":"2025-11-03 02:48:31.0569","pay_status":1,"payment_sn":"01162"},{"amount":79916,"created_at":"2025-11-03 18:30:32.0721","finished_at":"2025-11-03 05:30:45.0316","pay_status":1,"payment_sn":"06048"},{"amount":28133,"created_at":"2025-11-03 07:51:28.0791","finished_at":"2025-11-03 18:31:47.0554","pay_status":1,"payment_sn":"08504"},{"amount":5029,"created_at":"2025-11-03 13:28:15.0654","finished_at":"2025-11-03 05:52:40.0412","pay_status":2,"payment_sn":"06896"},{"amount":40098,"created_at":"2025-11-03 21:01:20.0496","finished_at":"2025-11-03 22:46:05.0293","pay_status":1,"payment_sn":"09949"},{"amount":56415,"created_at":"2025-11-03 23:06:43.0387","finished_at":"2025-11-03 01:13:34.0297","pay_status":1,"payment_sn":"01008"},{"amount":69322,"created_at":"2025-11-03 10:34:35.0172","finished_at":"2025-11-03 06:39:50.0390","pay_status":2,"payment_sn":"05772"},{"amount":88416,"created_at":"2025-11-03 22:36:01.0134","finished_at":"2025-11-03 19:04:43.0657","pay_status":2,"payment_sn":"03751"},{"amount":35187,"created_at":"2025-11-03 09:46:34.0088","finished_at":"2025-11-03 11:30:50.0427","pay_status":2,"payment_sn":"07205"},{"amount":46924,"created_at":"2025-11-03 03:24:04.0702","finished_at":"2025-11-03 16:44:50.0776","pay_status":0,"payment_sn":"02072"},{"amount":63097,"created_at":"2025-11-03 19:13:04.0727","finished_at":"2025-11-03 20:33:40.0381","pay_status":1,"payment_sn":"01595"},{"amount":19432,"created_at":"2025-11-03 23:52:46.0579","finished_at":"2025-11-03 06:33:54.0764","pay_status":1,"payment_sn":"08709"},{"amount":25355,"created_at":"2025-11-03 17:15:47.0285","finished_at":"2025-11-03 11:29:59.0842","pay_status":0,"payment_sn":"09229"},{"amount":73751,"created_at":"2025-11-03 00:23:39.0619","finished_at":"2025-11-03 14:40:28.0248","pay_status":0,"payment_sn":"06741"},{"amount":39793,"created_at":"2025-11-03 12:56:33.0147","finished_at":"2025-11-03 06:05:25.0600","pay_status":0,"payment_sn":"06238"},{"amount":77819,"created_at":"2025-11-03 11:37:40.0838","finished_at":"2025-11-03 07:29:44.0070","pay_status":0,"payment_sn":"05577"}]`)
        if (data) {
            state.payOrderList = data.data;
        }
    }
}
const payOrderListStatus = (state, status, arg) => {
    state.status.PayOrderList = status;
}
const reducerWithdrawTax = (state, action, arg) => {
    let payload = action.payload;
    if (payload && payload.code === 200) {
        let data = payload.data;
        if (data) {
            state.WithDrawTax = data.WithDrawTax;
        }
    }
}
const reducerWithdrawBaseInfo = (state, action, arg) => {
    let payload = action.payload;
    if (payload && payload.code === 200) {
        let data = payload.data;
        if (data) {
            state.withdrawBaseInfo = data;
        }
    }
    let callback = arg;
    if (callback != null) {
        callback(payload);
    }
}
const withdrawBaseInfoStatus = (state, status, arg) => {
    state.status.WithdrawBaseInfo = status;
}
const reducerWithdrawInfo = (state, action, arg) => {
    let payload = action.payload;
    if (payload && payload.code === 200) {
        let data = payload.data;
        if (data) {
            if (data.info) state.withdrawInfo = data.info;
        }
    }
}
const withdrawInfoStatus = (state, status, arg) => {
    state.status.WithdrawInfo = status;
}
const reducerChangePayPass = (state, action, arg) => {
    let payload = action.payload;
    let msg = getHttpMsg(payload);
    if (payload && payload.code === 200) {
        let data = payload.data;
        if (data) {
            // state.withdrawInfo=data;
        }
        if (arg) {
            if (arg.newpassword != null) {
                global.setWDPass(arg.newpassword);
            }
        }
        toast.success(msg, { toastId: 'changePayPass' });
    }
    else {
        toast.error(msg, { toastId: 'changePayPass' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const changePayPassStatus = (state, status, arg) => {
    state.status.ChangePayPass = status;
}
const reducerWithdrawRecord = (state, action, arg) => {
    let payload = action.payload;
    if (payload && payload.code === 200) {
        let data = payload.data;
        //data = JSON.parse(`[{"WithDraw":94286,"ServiceFee":97860,"State":1,"OrderId":"06529","CreateDate":"2025-11-03 21:42:22.0300"},{"WithDraw":83385,"ServiceFee":73328,"State":2,"OrderId":"00113","CreateDate":"2025-11-03 20:04:45.0339"},{"WithDraw":92599,"ServiceFee":99083,"State":0,"OrderId":"07602","CreateDate":"2025-11-03 17:09:23.0377"},{"WithDraw":90310,"ServiceFee":29131,"State":5,"OrderId":"04141","CreateDate":"2025-11-03 03:57:34.0002"},{"WithDraw":19375,"ServiceFee":76406,"State":5,"OrderId":"05220","CreateDate":"2025-11-03 05:17:51.0814"},{"WithDraw":9049,"ServiceFee":44198,"State":6,"OrderId":"00864","CreateDate":"2025-11-03 11:11:26.0000"},{"WithDraw":44165,"ServiceFee":31317,"State":4,"OrderId":"08828","CreateDate":"2025-11-03 20:56:48.0408"},{"WithDraw":17251,"ServiceFee":37909,"State":1,"OrderId":"06185","CreateDate":"2025-11-03 18:37:35.0999"},{"WithDraw":54622,"ServiceFee":57519,"State":4,"OrderId":"08277","CreateDate":"2025-11-03 10:39:34.0399"},{"WithDraw":2103,"ServiceFee":94186,"State":3,"OrderId":"05921","CreateDate":"2025-11-03 12:04:25.0035"},{"WithDraw":49610,"ServiceFee":42087,"State":4,"OrderId":"08870","CreateDate":"2025-11-03 07:49:40.0514"},{"WithDraw":83806,"ServiceFee":73458,"State":4,"OrderId":"00296","CreateDate":"2025-11-03 11:23:20.0159"},{"WithDraw":80806,"ServiceFee":83570,"State":1,"OrderId":"00388","CreateDate":"2025-11-03 02:57:09.0465"},{"WithDraw":18878,"ServiceFee":48081,"State":5,"OrderId":"05284","CreateDate":"2025-11-03 05:28:50.0949"},{"WithDraw":3971,"ServiceFee":83778,"State":1,"OrderId":"02248","CreateDate":"2025-11-03 19:53:02.0792"},{"WithDraw":91242,"ServiceFee":46924,"State":1,"OrderId":"09504","CreateDate":"2025-11-03 17:55:03.0838"},{"WithDraw":48968,"ServiceFee":80636,"State":3,"OrderId":"06665","CreateDate":"2025-11-03 00:57:46.0622"},{"WithDraw":11466,"ServiceFee":35393,"State":1,"OrderId":"08428","CreateDate":"2025-11-03 13:04:11.0180"},{"WithDraw":80388,"ServiceFee":88835,"State":2,"OrderId":"04363","CreateDate":"2025-11-03 19:10:16.0372"},{"WithDraw":60282,"ServiceFee":93764,"State":3,"OrderId":"05005","CreateDate":"2025-11-03 12:48:36.0107"}]`)
        if (data && data.data) {
            state.withdrawRecord = data.data;
        }
    }
}
const withdrawRecordStatus = (state, status, arg) => {
    state.status.WithdrawRecoard = status;
}
const reducerWithdrawSetInfo = (state, action, arg) => {
    let payload = action.payload;
    let msg = payload.msg;
    if (msg == null) msg = "";
    else msg = getHttpMsg(payload);
    if (payload && payload.code === 200) {
        let data = payload.data;
        toast.success(msg, { toastId: 'withdrawSetInfo' });
    }
    else {
        toast.error(msg, { toastId: 'withdrawSetInfo' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const withdrawSetInfoStatus = (state, status, arg) => {
    state.status.WithdrawSetInfo = status;
}
const reducerWithdrawPayout = (state, action, arg) => {
    let payload = action.payload;
    let msg = getHttpMsg(payload);
    if (payload && payload.code === 200) {
        // toast.success(msg);
        toast.success(i18n.t("withdraw.tip.success"), { toastId: 'withdrawPayout' });
    }
    else {
        toast.error(msg, { toastId: 'withdrawPayout' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const withdrawPayoutStatus = (state, status, arg) => {
    state.status.WithdrawPayout = status;
}
const reducerSaveCPF = (state, action, arg) => {
    let payload = action.payload;
    let msg = getHttpMsg(payload);
    if (payload && payload.code === 200) {
        state.cpfFirst = false;
        toast.success(msg, { toastId: 'saveCPF' });
    }
    else {
        toast.error(msg, { toastId: 'saveCPF' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const saveCPFStatus = (state, status, arg) => {
    state.status.SaveCPF = status;
}
//FirstPayMulti
const clearCDTimer = () => {
    if (objInfo.cdTimer != null) {
        clearInterval(objInfo.cdTimer);
        objInfo.cdTimer = null;
    }
}
const checkPayMultiLeftTime = (data) => {
    clearCDTimer();
    if (data == null) return;
    let leftitem = data.leftitem;
    if (leftitem != null) {
        let timeleft = leftitem.timeleft;
        if (timeleft > 0) {
            objInfo.cdTimer = setInterval(() => {
                timeleft--;
                if (timeleft <= 0) {
                    global.dispatch(reqFirstPayMulti());
                    clearCDTimer();
                }
                else {
                    global.dispatch(updateMultiCD(timeleft));
                }
            }, 1000);
        }
    }
}
const reducerFirstPayMulti = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        let data = payload.data;
        // data = {
        //     "leftitem": {
        //         "gift_id": "201",
        //         "money": "10",
        //         "give": "10",
        //         "favorable_price": "10",
        //         "second_give": "5",
        //         "timeleft": 1000,
        //     },
        //     "default": 1
        // }
        data = Array.isArray(data) ? null : data;
        state.firstPayMulti = data;
        state.firstPayMulti.pay_sucess = arg != null ? arg : 0;
        checkPayMultiLeftTime(data);
    }
    else {
        state.firstPayMulti = null;
        checkPayMultiLeftTime(null);
    }
}
const firstPayMultiStatus = (state, status, arg) => {
    state.status.FirstPayMulti = status;
}
const reducerGetSecond = (state, action, arg) => {
    let payload = action.payload;
    if (payload != null && payload.code === 200) {
        toast.success(i18n.t("firstPay.claim_success"), { toastId: 'getSecond' });
        state.firstPayMulti = null;
    }
    else {
        let msg = getHttpMsg(payload);
        toast.error(msg, { toastId: 'getSecond' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const getSecondStatus = (state, status, arg) => {
    state.status.GetSecond = status;
}
//FirstPayMulti
//bonus pack
const clearBonusPackCDTimer = () => {
    if (objInfo.bpCDTimer != null) {
        clearInterval(objInfo.bpCDTimer);
        objInfo.bpCDTimer = null;
    }
}
const checkBonusPackLeftTime = (data) => {
    clearBonusPackCDTimer();
    if (data == null) return;
    let timeleft = data.timeleft;
    if (timeleft > 0) {
        objInfo.bpCDTimer = setInterval(() => {
            timeleft--;
            if (timeleft <= 0) {
                global.dispatch(reqGameInfo());
                clearBonusPackCDTimer();
            }
            else {
                global.dispatch(updateBonusPackCD(timeleft));
            }
        }, 1000);
    }
}
//bonus pack
//kyc eu
const reducerKycEU = (state, action, arg) => {
    let payload = action.payload;
    let msg = getHttpMsg(payload);
    if (payload != null && payload.code === 200) {
        toast.success(i18n.t("deposit.infomation.success"), { toastId: 'kycEU' });
    }
    else {
        toast.error(msg, { toastId: 'kycEU' });
    }
    if (arg && arg.callback != null) {
        arg.callback(payload);
    }
}
const kycEUStatus = (state, status, arg) => {
    state.status.KycEU = status;
}
//kyc eu
export const Pay = createSlice({
    name: 'pay',
    initialState: {
        status: {
            RechargeConfig: REQ_STATUS.NONE,
            FirstPayConfig: REQ_STATUS.NONE,
            FirstPayGift: REQ_STATUS.NONE,
            FirstPayGiftClaim: REQ_STATUS.NONE,
            GetAccountsPayInfo: REQ_STATUS.NONE,
            PaymentOrder: REQ_STATUS.NONE,
            ChangePayPass: REQ_STATUS.NONE,
            PayOrderList: REQ_STATUS.NONE,
            WithdrawRecoard: REQ_STATUS.NONE,
            WithdrawBaseInfo: REQ_STATUS.NONE,
            WithdrawInfo: REQ_STATUS.NONE,
            WithdrawSetInfo: REQ_STATUS.NONE,
            WithdrawPayout: REQ_STATUS.NONE,
            ForgetPayPass: REQ_STATUS.NONE,
            SaveCPF: REQ_STATUS.NONE,
            PaymentOrderQR: REQ_STATUS.NONE,
            FirstPayMulti: REQ_STATUS.NONE,
            GetSecond: REQ_STATUS.NONE,
            KycEU: REQ_STATUS.NONE,
        },
        config: [],
        bankruptcyGift: null,
        payconfig: null,
        firstconfig: null,
        firstPayGift: null,
        payType: null,
        currPayOrder: null,
        payFinishFlag: 0,
        WithDrawTax: 4,
        withdrawBaseInfo: null,
        withdrawInfo: null,
        payOrderList: null,
        withdrawRecord: null,
        changePayPassType: 0,
        showBonus: false,
        cpfFirst: false,
        firstPayMulti: null,
        bonusPack: null,
        kycTypes: []
    },
    reducers: {
        updatePayFinish: ((state, action) => {
            if (state.currPayOrder != null) {
                state.currPayOrder.status = "success";
                //充值完成 message={Golds:12.xx,PayNum:10}
                toast.success(i18n.t("deposit.order_info.success"), { toastId: 'payFinish' });

                state.payFinishFlag = 1;
            }

        }),
        resetPayFinishFlag: ((state, action) => {
            state.payFinishFlag = 0;
        }),
        setChangePayPassType: ((state, action) => {
            state.changePayPassType = action.payload;
        }),
        onSliceChangeUser: ((state, action) => {
            state.isInit = false;
        }),
        updateMultiCD: ((state, action) => {
            let payload = action.payload;
            if (payload != null) {
                if (state.firstPayMulti != null && state.firstPayMulti.leftitem != null) {
                    state.firstPayMulti.leftitem.timeleft = payload;
                }
            }
        }),
        updateFirstGiftCD: ((state, action) => {
            let payload = action.payload;
            if (payload != null) {
                if (state.firstPayGift != null && state.firstPayGift.time_left != null) {
                    state.firstPayGift.time_left = payload;
                }
            }
        }),
        updateBonusPack: ((state, action) => {
            let payload = action.payload;
            if (payload != null) {
                state.bonusPack = payload.bonus_pack;
                checkBonusPackLeftTime(payload.bonus_pack);
            }
        }),
        updateBonusPackCD: ((state, action) => {
            let payload = action.payload;
            if (payload != null) {
                if (state.bonusPack != null) {
                    state.bonusPack.timeleft = payload;
                }
            }
        })
    },
    extraReducers: (builder) => {
        buildReqReducer(builder, reqBankruptcyGift, reducerBankruptcyGift, null);
        buildReqReducer(builder, reqRechargeConfig, reducerRechargeConfig, rechargeConfigStatus);
        buildReqReducer(builder, reqFirstPayConfig, reducerFirstPayConfig, firstPayConfigStatus);
        buildReqReducer(builder, reqGetAccountsPayInfo, reducerGetAccountsPayInfo, getAccountsPayInfoStatus);
        buildReqReducer(builder, reqPaymentOrder, reducerPaymentOrder, paymentOrderStatus);
        buildReqReducer(builder, reqWithdrawTax, reducerWithdrawTax, null);
        buildReqReducer(builder, reqWithdrawBaseInfo, reducerWithdrawBaseInfo, withdrawBaseInfoStatus);
        buildReqReducer(builder, reqWithdrawInfo, reducerWithdrawInfo, withdrawInfoStatus);
        buildReqReducer(builder, reqChangePayPass, reducerChangePayPass, changePayPassStatus);
        buildReqReducer(builder, reqPayOrderList, reducerPayOrderList, payOrderListStatus);
        buildReqReducer(builder, reqWithdrawRecord, reducerWithdrawRecord, withdrawRecordStatus);
        buildReqReducer(builder, reqWithdrawSetInfo, reducerWithdrawSetInfo, withdrawSetInfoStatus);
        buildReqReducer(builder, reqWithdrawPayout, reducerWithdrawPayout, withdrawPayoutStatus);
        buildReqReducer(builder, reqForgetPayPass, reducerChangePayPass, changePayPassStatus);
        buildReqReducer(builder, reqMexWithdrawSetInfo, reducerWithdrawSetInfo, withdrawSetInfoStatus);
        buildReqReducer(builder, reqCPFSave, reducerSaveCPF, saveCPFStatus);
        buildReqReducer(builder, reqRuWithdrawSetInfo, reducerWithdrawSetInfo, withdrawSetInfoStatus);
        buildReqReducer(builder, reqPaymentOrderQR, reducerPaymentOrder, paymentOrderQRStatus);
        buildReqReducer(builder, reqFirstPayMulti, reducerFirstPayMulti, firstPayMultiStatus);
        buildReqReducer(builder, reqGetSecond, reducerGetSecond, getSecondStatus);
        buildReqReducer(builder, reqKycEU, reducerKycEU, kycEUStatus)
        buildReqReducer(builder, reqFirstPayGift, reducerFirstPayGift, firstPayGiftStatus);
        buildReqReducer(builder, reqFirstPayGiftClaim, reducerFirstPayGiftClaim, firstPayGiftClaimStatus)
    },
})
export const { updatePayFinish, resetPayFinishFlag, setChangePayPassType, updateFirstGiftCD, updateMultiCD, updateBonusPack, updateBonusPackCD } = Pay.actions;
export const getConfig = (state) => {
    return state.pay.config;
}
export const getOFirstPay = (state) => {
    return state.pay.firstconfig;
}
export const getBankRuptcyGift = (state) => {
    return state.pay.bankruptcyGift;
}
export const getFirstPay = (state) => {
    let firstconfig = state.pay.firstconfig;
    let config = state.pay.config;
    let order = null;
    if (firstconfig != null) {
        let first_pay = firstconfig.first_pay;
        let recomendar_recharge = firstconfig.recomendar_recharge;
        if (first_pay != null) {
            let gear = JSON.parse(first_pay.gear);
            if (gear) gear = gear[0];
            order = {
                goodsName: "firstpay",
                goodsPrice: first_pay.money,
                GiftsID: 201,
                cpf_first: 0,
                give: first_pay.give,
                gear: gear
            }
        }
        else {
            for (let i = 0; i < config.length; i++) {
                const elem = config[i];
                if (elem.id === recomendar_recharge) {
                    let gear = JSON.parse(elem.gear);
                    if (gear) gear = gear[0];
                    order = {
                        goodsName: "chip",
                        goodsPrice: elem.money,
                        GiftsID: elem.gift_id,
                        cpf_first: 0,
                        give: elem.give,
                        gear: gear
                    }
                    break;
                }
            }
        }
    }
    return order;
}
export const getPaymentOrderStatus = (state) => {
    return state.pay.status.PaymentOrder;
}
export const getFirstPayGiftClaimStatus = (state) => {
    return state.pay.status.FirstPayGiftClaim
}
export const getCurrPayOrder = (state) => {
    return state.pay.currPayOrder;
}
export const getWithdrawBaseInfo = (state) => {
    return state.pay.withdrawBaseInfo;
}
export const getWithdrawInfo = (state) => {
    return state.pay.withdrawInfo;
}
export const getChangePayPassStatus = (state) => {
    return state.pay.status.ChangePayPass;
}
export const getPayOrderStatus = (state) => {
    return state.pay.status.PayOrderList;
}
export const getPayOrderList = (state) => {
    //return JSON.parse(`[{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000001"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000002"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":1,"payment_sn":"0000000003"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":1,"payment_sn":"0000000004"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":1,"payment_sn":"0000000005"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":2,"payment_sn":"0000000006"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":2,"payment_sn":"0000000007"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":2,"payment_sn":"0000000008"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":2,"payment_sn":"0000000009"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000010"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000011"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000012"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000013"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000014"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000015"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000016"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000017"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000018"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000019"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000020"},{"amount":100,"created_at":"2025-10-27","finished_at":"2025-10-27","pay_status":0,"payment_sn":"0000000021"}]`)
    return state.pay.payOrderList;
}
export const getWithdrawRecordStatus = (state) => {
    return state.pay.status.WithdrawRecoard;
}
export const getWithdrawRecord = (state) => {
    //return JSON.parse(`[{"withDraw":52.39105880665225,"ServiceFee":1,"State":0,"CreateDate":"2025-10-27 19:00:00.111","OrderId":"00000000000"},{"withDraw":23.603805464384497,"ServiceFee":1,"State":4,"CreateDate":"2025-10-27 19:01:00.111","OrderId":"00000000001"},{"withDraw":58.40797670755988,"ServiceFee":1,"State":5,"CreateDate":"2025-10-27 19:02:00.111","OrderId":"00000000002"},{"withDraw":29.353635196013904,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:03:00.111","OrderId":"00000000003"},{"withDraw":39.082574939842,"ServiceFee":1,"State":0,"CreateDate":"2025-10-27 19:04:00.111","OrderId":"00000000004"},{"withDraw":37.769032992613205,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:05:00.111","OrderId":"00000000005"},{"withDraw":81.66681485858493,"ServiceFee":1,"State":0,"CreateDate":"2025-10-27 19:06:00.111","OrderId":"00000000006"},{"withDraw":87.09722892142497,"ServiceFee":1,"State":0,"CreateDate":"2025-10-27 19:07:00.111","OrderId":"00000000007"},{"withDraw":87.58353155179388,"ServiceFee":1,"State":3,"CreateDate":"2025-10-27 19:08:00.111","OrderId":"00000000008"},{"withDraw":7.570417112590078,"ServiceFee":1,"State":0,"CreateDate":"2025-10-27 19:09:00.111","OrderId":"00000000009"},{"withDraw":63.6250581995325,"ServiceFee":1,"State":4,"CreateDate":"2025-10-27 19:10:00.111","OrderId":"00000000010"},{"withDraw":26.878946502459545,"ServiceFee":1,"State":6,"CreateDate":"2025-10-27 19:11:00.111","OrderId":"00000000011"},{"withDraw":42.88306194892917,"ServiceFee":1,"State":4,"CreateDate":"2025-10-27 19:12:00.111","OrderId":"00000000012"},{"withDraw":94.82975239374265,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:13:00.111","OrderId":"00000000013"},{"withDraw":89.67639444743536,"ServiceFee":1,"State":2,"CreateDate":"2025-10-27 19:14:00.111","OrderId":"00000000014"},{"withDraw":98.44194006649265,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:15:00.111","OrderId":"00000000015"},{"withDraw":23.20808163942223,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:16:00.111","OrderId":"00000000016"},{"withDraw":51.13330504660332,"ServiceFee":1,"State":5,"CreateDate":"2025-10-27 19:17:00.111","OrderId":"00000000017"},{"withDraw":48.50943327464281,"ServiceFee":1,"State":5,"CreateDate":"2025-10-27 19:18:00.111","OrderId":"00000000018"},{"withDraw":41.394970737724314,"ServiceFee":1,"State":1,"CreateDate":"2025-10-27 19:19:00.111","OrderId":"00000000019"}]`)
    return state.pay.withdrawRecord;
}
export const getWithdrawTax = (state) => {
    return state.pay.WithDrawTax;
}
export const getWDBaseInfoStatus = (state) => {
    return state.pay.status.WithdrawBaseInfo;
}
export const getWDSetInfoStatus = (state) => {
    return state.pay.status.WithdrawSetInfo;
}
export const getWDInfoStatus = (state) => {
    return state.pay.status.WithdrawInfo;
}
export const getChangePayPassType = (state) => {
    return state.pay.changePayPassType;
}
export const getWDPayoutStatus = (state) => {
    return state.pay.status.WithdrawPayout;
}
export const getShowBonus = (state) => {
    return state.pay.showBonus;
}
export const getPayConfigStatus = (state) => {
    return state.pay.status.RechargeConfig;
}
export const getForgetPayPassStatus = (state) => {
    return state.pay.status.ForgetPayPass;
}
export const getCPFFirst = (state) => {
    return state.pay.cpfFirst;
}
export const getSaveCPFStatus = (state) => {
    return state.pay.status.SaveCPF;
}
export const getPaymentOrderQRStatus = (state) => {
    return state.pay.status.PaymentOrderQR;
}
export const getFirstPayMulti = (state) => {
    return state.pay.firstPayMulti;
}
export const getFirstPayGift = (state) => {
    return state.pay.firstPayGift;
}
export const getGetSecondStatus = (state) => {
    return state.pay.status.GetSecond;
}
//bonus pack
export const getBonusPack = (state) => {
    return state.pay.bonusPack;
}
//kyc
export const getKycEUStatus = (state) => {
    return state.pay.status.KycEU;
}
export const getKycTypes = (state) => {
    return state.pay.kycTypes;
}
export const getPayFinishFlag = (state)=>{
    return state.pay.payFinishFlag;
}
export default Pay.reducer;

export const reqPayConfig = (dispatch, userInfo) => {
    dispatch(reqRechargeConfig());
    dispatch(reqFirstPayConfig(userInfo.UserID));
    // dispatch(reqGetAccountsPayInfo(userInfo.UserID));
    // dispatch(reqWithdrawTax());
}
