import config from './config';

import UserConfigType from './const/type';

import storage from "$utils/tool/storage";

import store from '@/store/index';

export default {

    // 使用的用户配置
    useUserConfig:{},

    // 获取数据data
    getData(type:UserConfigType){
        if (this.useUserConfig[type] === undefined) {
            this.useUserConfig[type] = storage.getItem(config[type]) || {};
            // store.state.user[type] =
            store.commit('setUserOtherInfo',{
                key:type,
                data:this.useUserConfig[type]
            });
        }
        return this.useUserConfig[type] || {};
    },

    // 设置
    setData(type:UserConfigType,data:any){
        if (data) {
         let key:string = '_time_'+type;

         clearTimeout(this[key]);
         this.useUserConfig[type] = data;
         store.commit('setUserOtherInfo',{
            key:type,
            data:this.useUserConfig[type]
         });
         this[key] = setTimeout( ()=> {
             storage.setItem(config[type],this.useUserConfig[type]);
         },300);
        }
    },

    // 设置单个item
    setDataItem(type:UserConfigType,key:any,value:any) {
        let useData = this.getData(type);
        useData[key] = value;
        this.setData(type,useData);
    },

    // 获取单个item
    getDataItem(type:UserConfigType,key:any){
        let useData = this.getData(type);

        return useData[key];
    }


}
