import {APP_APK_LOADNEW} from "@api/config";
import Http from "@api/request";
import {initTrackAll} from "./TrackAll";

let pixid = '871551844325920'
let _org_pixid = pixid
let user = {uid:0,udid:'',v:0,cv:"1.0.0",package:"",cookie:"",rbinfo:{uuid:0,link_id:0},params:{pixel:0,fbclid:0,gclid:0},type:"fb",isfb(){return this.type==="fb"||!!user.params.pixel},isgg(){return this.type==="gg"},isrb(){return this.type==="rb"}}

if(localStorage.getItem("user")){
    Object.assign(user,JSON.parse(localStorage.getItem("user")));
}
if(localStorage.getItem("UserID")){
    user.uid=localStorage.getItem("UserID");
}

let inited=false;

export async function loadInfoNew(us,userid){
    // if (localStorage.getItem("loadinfo") === "1") return;
    let url = APP_APK_LOADNEW;

    const response = await Http.get({
        url,
        params:{us}
    })

    console.log(response);
    try {
        if (response.data) {
            let res = response.data;
            if (res.cookie) {
                var cookie = {}
                cookie=eval("({" + res.cookie.replaceAll("=", ":'").replaceAll(";", "',") + "'})");
                for (let k in cookie) {
                    document.cookie = k + "=" + cookie[k] + "; path=/; domain=" + window.location.hostname + "; max-age=10368000;";
                }

            }
            if(res.params&&typeof res.params=="string")res.params=JSON.parse(res.params);

            Object.assign(user,res);
            localStorage.setItem("user",JSON.stringify(user))
            localStorage.setItem("loadinfo","1");
            //没有gclid
            // if(!params.gclid&&!!user.params.gclid){
            //     window.location=location.origin+location.pathname+"?" + new URLSearchParams(Object.assign({},params,user.params));
            //     return;
            // }
            if(user.isfb()){
                global.pf.type="fb"
                initTrackAll('fb',user.params.pixel,userid)
            }

        }
    }catch (e) {
        console.error(e);
    }
}
