import extend from "./extend";
const urlkey = 'url_key';
/**
 * 地址类
 */
class Url {
    /**
     * 认证地址
     */
    get auth() {
        let url = this.get();
        return url.auth;
    }

    /**
     * 接口地址
     */
    get api() {
        let url = this.get();
        return url.api;
    }

    /**
     * 设置地址
     * @param {object} url url对象
     * @param {string} url.auth 认证地址
     * @param {string} url.api 接口地址
     */
    set(url) {
        if (extend.isObject(url)) {
            if (extend.isEmptyString(url.auth)) {
                let oldUrl = this.get();
                url.auth = oldUrl.auth;
            }
            if (extend.isEmptyString(url.api)) {
                let oldUrl = this.get();
                url.api = oldUrl.api;
            }
            extend.addStorage(urlkey, url);
        } else {
            this.set(oldUrl);
        }
    }

    /**
     * 获取地址
     * @returns {{auth: string, api: string}}
     */
    get() {
        let isStorage = true;
        let url = extend.readStorage(urlkey);
        if (extend.isNull(url)) {
            url = {};
            isStorage = false;
        }
        if (extend.isEmptyString(url.authUrl)) {
            url.auth = 'http://219.151.179.47:8087/'
        }
        if (extend.isEmptyString(url.authUrl)) {
            url.api = 'http://219.151.179.47:8081/'
        }
        if (!isStorage) {
            this.set(url);
        }
        return url;
    }
}

/**
 * 地址类
 */
export default new Url();