// $(function () {
//     Promise.all([
//         I18nBook.changeLocale(navigator.language),
//         Promise.resolve(buildRpc()),
//     ]).then(function (values) {
//         var runtime = {
//             i18n: values[0],
//             rpc: values[1],
//         };
//         runtime.rpc.onError = function (e) {
//             var msg = runtime.i18n.translate("Error_" + e.code);
//             if (!msg) {
//                 msg = e.message;
//             }
//             layer.alert(msg, {
//                 icon: 5,
//                 title: runtime.i18n.translate("Error"),
//                 btn: [runtime.i18n.translate("AlertOK")]
//             });
//         }
//         runApp && runApp(runtime);
//     });

//     function buildRpc() {
//         var rpc = new JsonRpcClient({
//             host: "http://192.168.6.117:8303/"
//         });
//         return rpc;
//     }
// });

function getQueryString(name) {
    const url_string = window.location.href;
    const url = new URL(url_string);
    return url.searchParams.get(name);
}

function getOpenUid() {
    const key = 'fis_open_uid';
    let uid = localStorage[key];
    if (!uid) {
        uid = generateUUID();
        localStorage[key] = uid;
    }
    return uid;
}

function generateUUID() {
    if (typeof crypto === 'object') {
        if (typeof crypto.randomUUID === 'function') {
            return crypto.randomUUID();
        }
        if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
            const callback = (c) => {
                const num = Number(c);
                return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16);
            };
            return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, callback);
        }
    }
    let timestamp = new Date().getTime();
    let perforNow = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
        let random = Math.random() * 16;
        if (timestamp > 0) {
            random = (timestamp + random) % 16 | 0;
            timestamp = Math.floor(timestamp / 16);
        } else {
            random = (perforNow + random) % 16 | 0;
            perforNow = Math.floor(perforNow / 16);
        }
        return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
    });
};

/**
 * 添加页面Resize回调
 * @param {Function} fn 回调函数
 */
export function addResizeCallback(fn) {
    window.addEventListener('resize', fn);
}

/**
 * 加载提示器
 * @param {string|HTMLDivElement} el 寄生元素
 */
export function Loading(el) {

    /**
         * 显示
         */
    this.show = function () {
        var loadingEl = document.createElement('div');
        loadingEl.classList.add('loading');
        loadingEl.innerHTML = '<div></div>';
        document.querySelector(el).appendChild(loadingEl);
    }
    /**
   * 隐藏
   */
    this.hide = function () {
        var loadingEl = document.querySelector(el).querySelector('.loading');
        if (loadingEl) {
            loadingEl.parentNode.removeChild(loadingEl);
        }
    }


}

/**
 * 查找网站根目录
 */
function findRoot() {
    var scripts = Array.from(document.getElementsByTagName('script'));
    for (let i = 0; i < scripts.length; i++) {
        var src = scripts[i].src;
        var index = src.indexOf("/js/common.js");
        if (index > -1) {
            var path = src.slice(0, index);
            return path;
        }
    }
}