/**
 * 项目环境配置文件
 * 集中管理所有环境相关的配置参数
 */

// 开发环境配置
const development = {
    // API基础URL
    baseUrl: 'https://sqqp.zpc-xy.com',
    // baseUrl: 'http://1.15.21.245:2401',
    host: ['https://sqqp.zpc-xy.com'],
    // baseUrl: 'http://192.168.1.21:2016',
    // host: ['http://192.168.1.21:2016'],
    imageUrl: 'https://guyu-1308826010.cos.ap-shanghai.myqcloud.com',

};

// 生产环境配置
const production = {
    baseUrl: 'https://guyu.zpc-xy.com',
    host: ['https://guyu.zpc-xy.com', 'http://192.168.1.41:90'],
    imageUrl: 'https://guyu-1308826010.cos.ap-shanghai.myqcloud.com',

};

// 根据环境变量选择对应配置
let currentEnv = development; //production;//testing;
const config = {
    ...currentEnv,
    apiBaseUrl: currentEnv.baseUrl + '/api/',
    // verificationHost: null,
};
//
export const verificationHost = async () => {
    // if(){}
    async function verification(host) {
        const res = await uni.request({
            url: host + "/api/common/h",
            method: 'GET',
        });
        return { success: true, host, res };
    }

    if (currentEnv.host.length > 0) {
        const timeoutPromise = new Promise((resolve, reject) =>
            setTimeout(() =>
                resolve({ success: false }),
                5000
            )
        );
        //将所有host地址转成请求
        let requests = currentEnv.host.map(it => verification(it));

        requests.push(timeoutPromise);
        //判断host中哪一个请求最快
        const result = await Promise.race(requests);
        //返回最快的host
        if (result.success) {
            console.log("result", result.host);
            config.baseUrl = result.host;
            config.apiBaseUrl = result.host + "/api/";
            return true;
        } else {
            console.log("访问网络失败");
            return false;
        }
    }
    return true;

}
// config.verificationHost = verificationHost;
// verificationHost();
// 衍生配置

export default config;