import AsyncStorage from "@react-native-async-storage/async-storage";

export const getStorage = (name:string) => {
    return new Promise(async (resolve,reject)=>{
        try{
            let data:any = await AsyncStorage.getItem(name);
            data = JSON.parse(atob(data))
            resolve(data);
        }catch(e){
            reject(e)
        }
    })
}
export const addStorage = (name:string, datasave:any) => {
    return new Promise(async (resolve,reject)=>{
        try{
            let data:any = await AsyncStorage.setItem(name, btoa(JSON.stringify(datasave)) );
            resolve(data);
        }catch(e){
            reject(e)
        }
    })
}

export let cfg = {
    call: function(name:string|any){
        this.action[name]? this.action[name]():(function(){
            console.log("call", name)
        })();
    },
    action: {},
    input: {},
    inputAction: {},
    formClear: function(){
        this.input = {}
    },
    counterHtml: 0,
    activeTabs: '',
    color: { 
        primary:'#f3f4f6'
    },
    dataPilihan:{
        negara:[],
        perusahaan:[],
        customer:[],
        uom:[],
        uomkat:[],
        product: []
    },
    inputChange : {},
    invoice : {
        token_access:null,
        data : {
            sales_order:[],
            sales_order_line:[]
        }
    }
} as { 
    [key: string]: any 
}   

export const generateUUID = () => {
    let timestamp = new Date().getTime();
    let performanceTime = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;

    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let random = Math.random() * 16;
        if (timestamp > 0) {
            random = (timestamp + random) % 16 | 0;
            timestamp = Math.floor(timestamp / 16);
        } else {
            random = (performanceTime + random) % 16 | 0;
            performanceTime = Math.floor(performanceTime / 16);
        }
        return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
    });
}