import { get, post, postL } from './http';

const apis = {
    PRE_ONE_KEY: '/api/substituteOrder/preOneKeySubmit',
    COUPON: '/api/coupon/pageMyValidCoupon',
    BILL: '/api/wallet/bill',
    
    WITHDRAW_PRE: '/api/wallet/withdraw/preInfo',
    WITHDRAW: '/api/wallet/withdraw/submit',
    WITHDRAW_INFO: '/api/user/withdraw/info', // Bank info
    WITHDRAW_SAVE: '/api/user/withdraw/save',

    RECHARGE_SUBMIT: '/api/wallet/recharge/submit',
    RECHARGE_GENERATE_LINK: '/api/user/recharge/generatePaymentLink',
    RECHARGE_CHECK_PAYMENT_STATUS: '/api/user/recharge/checkPaymentStatus',
    
    INFO: '/api/wallet/getByType',
    
    NEW_USER_PROMOTION: '/api/activity/newUserPromotion',
};

export const preOneKeySubmit = async () => {
    const res = await post(apis.PRE_ONE_KEY);
    return res;
};

export const info = async (type: string, loading = false) => {
    // loading handled manually if needed, or pass header
    const res = await get(apis.INFO, { type }, { loading });
    return res.data;
};

export const bill = async (current: number, size: number, walletType: string, type: string) => {
    const res = await post(apis.BILL, { current, size, walletType, type });
    return res.data;
};

export const withdrawPre = async (type: string, walletType: string) => {
    const res = await get(apis.WITHDRAW_PRE, { type, walletType });
    return res.data;
};

export const getWithdraw = async () => {
    const res = await get(apis.WITHDRAW_INFO);
    return res.data;
};

export const saveWithdraw = async (data: any) => {
    const res = await post(apis.WITHDRAW_SAVE, data);
    return res.success;
};

export const withdraw = async (data: any) => {
    const res = await postL(apis.WITHDRAW, data);
    return res.success;
};

export const generatePaymentLink = async (amount: number, payType: string, walletType: string) => {
    const res = await postL(apis.RECHARGE_GENERATE_LINK, { amount, payType, walletType });
    return res;
};

export const rechargeSubmit = async (amount: number, paymentType: string, walletType: string) => {
    const res = await postL(apis.RECHARGE_SUBMIT, { amount, paymentType, walletType });
    return res;
};

export const checkPaymentStatus = async (data: { tradeNo: string }) => {
    const res = await post(apis.RECHARGE_CHECK_PAYMENT_STATUS, data);
    return res;
};

export const getNewUserPromotion = async () => {
    const res = await get(apis.NEW_USER_PROMOTION);
    return res.data;
};

export default {
    preOneKeySubmit,
    info,
    bill,
    withdrawPre,
    getWithdraw,
    saveWithdraw,
    withdraw,
    generatePaymentLink,
    rechargeSubmit,
    checkPaymentStatus,
    getNewUserPromotion,
    coupons: async (size = 30) => {
        const res = await post(apis.COUPON, { size }, { loading: true });
        return res.data;
    },
};
