exports.greeting = function () {};

/**
 * 依赖库可以懒加载引入
 * @param src
 * @param callback
 * @param options
 */
exports.require = function (src,options,callback) {
    if(typeof  options === "function"){
        callback = options;
        options = {};
    }

    if(typeof src === 'object'){
        let l = src.length;
        src.forEach(function (s) {
            loadScript(s,function () {
                if(--l===0){
                    callback()
                }
            })
        })
    }else{
        loadScript(src,callback);
    }

};

exports.extend = function (dist, ...source) {
    //108389 extend支持dist参数为null
    if(!dist) dist={}
    source.forEach(function (so) {
        for (let k in so) {
            if(so.hasOwnProperty(k)) dist[k] = so[k];
        }
    });
    return dist;
};

exports.noop = function () {
};

let res = [];
function loadScript(src, callback){
    if(res.indexOf(src) > -1){
        callback();
        return;
    }

    let script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0];
    script.type = 'text/javascript';
    script.charset = 'UTF-8';
    script.src = src;
    if(script.addEventListener){
        script.addEventListener('load', function(){
            callback();
            res.push(src);
        }, false);
    } else if(script.attachEvent){
        script.attachEvent('onreadystatechange', function(){
            var target = event.srcElement;
            if(target.readyState === 'loaded'){
                callback();
                res.push(src);
            }
        });
    }
    head.appendChild(script);
}

/*日期时间*/
let format = function (fmt) {
    let o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
try {Date.prototype.Format = format;}catch (e){}
Date.Format = format;

function parse2query(data) {
    let string = "";
    Object.keys(data).forEach(function (key) {
        if ((data[key]) instanceof Object) {
            data[key] = JSON.stringify(data[key])
        }
        string += `${key}=${encodeURIComponent(data[key])}&`
    });
    string = string.substring(0, string.length - 1);
    
    return string;
};

//166093【框架】优化light.navigate路由跳转时无入参的路由
exports.parse2query = parse2query;

exports.resolvePath = function(url, params) {
    return parse2query(params) ? `${url}?${parse2query(params)}` : url;
}

//支持打开gmu://web?startPage=xxx（将query转为对象）
exports.query2params = function(query) {
    let params = {};
    let matchArr = query.split('&');
    if(matchArr&&matchArr.length>0) {
        matchArr.forEach((item)=>{
            let res = item.split('=');
            params[res[0]] = decodeURIComponent(res[1])
        })
    }
    return params
}