import MathUtil from "./MathUtil";
const Config = {
    logUrl:'//conecli.com/send',     
    whiteList:['jshop2013.conecli.com']
};
function getTimer(){
    return (new Date()).getTime();
}
function getVenderId(){
    if(window.page_data && window.page_data.venderId){
        return window.page_data.venderId;
    }
    return null;
}
function trimUrl(url) {
    var result = url;
    if(url && url.indexOf('?')>=0){
        result = url.substring(0, url.indexOf('?'));
    }
    return result;
}
export function needSendLog(url) {
    let result = false;
    if(url){
        let _url = url;
        let index = _url.indexOf('?');
        if(index>0){
            _url = _url.substring(0, index);
        }
        let list = Config.whiteList;
        for(let i=0;i<list.length;i++){
            let domain = list[i];
            if(domain && _url.indexOf(domain)>=0){
                result = true;
                break;
            }
        }
    }
    return result;
}
export function filterSend(info) {
    let result = true;
    if(info){
        if(info.jqXHR){
            var status = parseInt(info.jqXHR.status);
            if(status >= 300 && status<400){
                result = false;
            }
        }
        if(info.data && info.data.hasOwnProperty('code')){
            var code = parseInt(info.data.code);
            if(code >= 300 && code<400){
                result = false;
            }
        }
    }
    return result;
}
export function getUID(){
    let result = '';
    result += getTimer() + '' + MathUtil.random(10000000, 99999999);
    return result;
}
export function sendLog(uid, status, info){
    return;
    try{
        let f_msg = getVenderId();
        let isSend = true;
        if(info){
            isSend = filterSend(info);
            f_msg += '|_|' + trimUrl(info.url) + '|_|web ';
            if(info.jqXHR){
                f_msg += 'textStatus:'+info.textStatus+',status:'+info.jqXHR.status+',statusText:'+info.jqXHR.statusText+',errorThrown:'+info.errorThrown;
            }
            else if(info.data){
                f_msg += 'data:'+ JSON.stringify(info.data);
            }
        }
        if(isSend){
            let param = {
                timeout: 1000,
                dataType: "jsonp",
                xhrFields:{
                    withCredentials:true
                },
                data:{uid:uid, f_msg:encodeURIComponent(f_msg)},
                url: Config.logUrl
            };
            return $.ajax(param);
        }
    }
    catch(e){
        console.log(e);
    }
}