import {Module} from "vuex";

import $request from '$utils/request';

export default <Module<any,any>>{

    state:{
        giftData:[],
        giftObjectData:{},
        giftUserData:[],
        giftUserObjectData:{}
    },

    mutations:{
        setGiftData(state,data){
            let storageGiftData = JSON.stringify(data);
            if (storageGiftData !== this.storageGiftData) {
                this.storageGiftData = storageGiftData;

                let giftObjectData = {};
                data.map((item)=>{
                    giftObjectData[item.gid] = item;
                });
                state.giftObjectData = giftObjectData;
                state.giftData = data;
            }
        },
        setUserGiftData(state,data){
            let storageUserGiftData = JSON.stringify(data);
            if (storageUserGiftData !== this.storageUserGiftData) {
                this.storageUserGiftData = storageUserGiftData;
                let giftObjectData = {};
                data.map((item)=>{
                    giftObjectData[item.gid] = item;
                });
                state.giftUserObjectData = giftObjectData;
                state.giftUserData = data;
            }
        }
    },

    actions:{

        initializationUserGiftPromise({commit}){
            return new Promise(function (resolve, reject) {
                $request({
                    url:'playergift/get_gift_list',
                    token:true,
                    login:false,
                    cache:true,
                    failMessage:true
                }).then((data)=>{
                    commit('setUserGiftData',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });
        },

        initializationGiftPromise({commit}){

            return new Promise(function (resolve, reject) {
                $request({
                    url:'gift/get_gift_list',
                    token:true,
                    login:false,
                    cache:true,
                    failMessage:true
                }).then((data)=>{
                    commit('setGiftData',data.data);
                    return resolve(data.data);
                }).catch(reject)
            });

        }

    }

}