export function getTargetRect(target) {
    return target !== window ? target.getBoundingClientRect() : { top: 0, left: 0, bottom: 0 };
}


export function getDefaultTarget() {
    return typeof window !== 'undefined' ? window : null;
}

/**
 * @method setUrlValue
 * @description 设置地址栏参数值
 * @param url
 * @param pName
 * @param pValue
 * 
 * @example: urlAppend("http://www.fdauto.com","userid=03870");�����http://www.fdauto.com?userid=03870;1`
 *           urlAppend("http://www.fdauto.com?userid=03870","bumen=aj")�����http://www.fdauto.com?userid=03870&bumen=aj;
 */

export function setUrlValue(url, key, value) {
    var returnUrl = ''
    if (url.indexOf('?') == -1) {
        returnUrl += url + '?' + key + '=' + value
    } else {
        if (url.indexOf('?' + key + '=') == -1 && url.indexOf('&' + key + '=') == -1) {
            returnUrl += url + '&' + key + '=' + value
        } else {
            var isDone = false
            var startIndex = 0
            var endIndex = url.length - 1
            var parm = '?' + key + '='
            for (var i = 0; i < url.length; i++) {
                if (url.substr(i, parm.length) == parm) {
                    startIndex = i + parm.length
                    for (var j = startIndex; j < url.length; j++) {
                        if (url[j] == '&') {
                            endIndex = j
                            break
                        } else if (j == url.length - 1) {
                            endIndex = url.length
                        }
                    }
                    isDone = true
                    break
                }
            }
            if (!isDone) {
                parm = '&' + key + '='
                for (var i = 0; i < url.length; i++) {
                    if (url.substr(i, parm.length) == parm) {
                        startIndex = i + parm.length
                        for (var j = startIndex; j < url.length; j++) {
                            if (url[j] == '&') {
                                endIndex = j
                                break
                            } else if (j == url.length - 1) {
                                endIndex = url.length
                            }
                        }
                        break
                    }
                }
            }
            var parmKeyValue = parm + url.substring(startIndex, endIndex)
            returnUrl = url.replace(parmKeyValue, parm + value)
        }
    }
    return returnUrl
}
/**
 * @method getUrlValue
 * @description 获取地址栏参数值
 * @param name
 *            string
 * @param url
 *            string
 * @example: getUrlValue("p","http://www.baidu.com?p=5"); 
 */
export function getUrlValue(url, name) {

    var str = url || window.location.search;
    if (str.indexOf("&" + name) != -1 || str.indexOf("?" + name) != -1) {
        var pos_start = ""
        if (str.indexOf("?" + name) > -1)
            pos_start = str.indexOf("?" + name) + name.length + 2;
        else
            pos_start = str.indexOf("&" + name) + name.length + 2;
        var pos_end = str.indexOf("&", pos_start);
        if (pos_end == -1) {
            return str.substring(pos_start);
        } else {
            return str.substring(pos_start, pos_end)
        }
    } else {
        return "";
    }
}
/**
 * 测试字符串
 * http://localhost:8080/sso-middle.html#pn=xxx&pn33=x3x3x3&redirect_uri=http://localhost:8080/aa.html?access_token=AT-382-oGjoq4985zM5GHT-Ylc8JJzKel-RcthN&token_type=bearer&expires_in=28800&id_token=xxx&state=67085c12-c157-11ec-b5fb-005056ba54a4&redirect_uri666=http://localhost:8080/aa.html?access_token=AT-382-oGjoq4985zM5GHT-Ylc8JJzKel-RcthN&token_type=bearer&expires_in=28800&id_token=xxx&state=67085c12-c157-11ec-b5fb-005056ba54a4
 * 
 * 
 */
export function getUrlSearch(url) {
    var name, value;
    var str = url; //取得整个地址栏
    var num = str.indexOf("?")
    str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]

    var arr = str.split("&"); //各个参数放到数组里
    var search ={};
    var lastSearchName="";
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substring(0, num);
            value = arr[i].substr(num + 1);
            if(lastSearchName){
                search[lastSearchName]+="&"+arr[i];
            }else{
                if(arr[i].indexOf("?")!=-1){
                    lastSearchName=name;
                }
                search[name] = value;
            }
        }
    }
    return search;
}
/**
 * var str='这是一个测试的字符串：{0} {1}'.format('Hello','world');
 * var str='这是一个测试的字符串：{str0} {str1}'.format({str0:'Hello',str1:'world'});
 */
export function strFormat(str, args) {
    var result = str;
    if (arguments.length > 0) {
        if (arguments.length == 2 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 1; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({)" + (i - 1) + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}