export class Router {
    constructor(loading, location) {
        this._routes = new Map();
        this._paramsResolvers = new Map();
        this._loading = loading;
        this._location = location;
        this._urlParams = this.parseURLParams();
        document.body.addEventListener('click', (event) => {
            var _a;
            const target = event.target;
            if ((target === null || target === void 0 ? void 0 : target.tagName) === 'A' && ((_a = target === null || target === void 0 ? void 0 : target.dataset) === null || _a === void 0 ? void 0 : _a.link) !== undefined) {
                const href = target.getAttribute('href');
                if (href.indexOf('://') > 0) {
                    return true;
                }
                event.preventDefault();
                this.show(href);
            }
        });
    }
    add(id, fn) {
        if (typeof id === 'string') {
            this._routes.set(id, fn);
            return;
        }
        for (const v of id) {
            this._routes.set(v, fn);
        }
    }
    addParamsResolver(param, fn, weight) {
        if (!weight) {
            weight = 0;
        }
        this._paramsResolvers.set(param, {
            fn,
            weight,
        });
    }
    show(id, data) {
        const ele = document.querySelector(`.l-${id}`);
        const currentEle = document.querySelector('.l-block:not(.hidden)');
        if (currentEle) {
            currentEle.classList.add('hidden');
            if (currentEle instanceof HTMLFormElement) {
                currentEle.reset();
            }
        }
        ele.classList.remove('hidden');
        const action = this._routes.get(id);
        if (action && ele instanceof HTMLFormElement) {
            action(ele, data);
        }
    }
    start() {
        if (this._urlParams.size > 0) {
            let callback;
            this._urlParams.forEach((value, key) => {
                const tmp = this._paramsResolvers.get(key);
                if (tmp) {
                    if (!callback) {
                        callback = tmp;
                    }
                    else {
                        if (tmp.weight > callback.weight) {
                            callback = tmp;
                        }
                    }
                }
            });
            if (callback) {
                callback.fn(this._urlParams);
                return;
            }
        }
        this.show('main');
    }
    loading() {
        this._loading.style.display = 'block';
    }
    loaded() {
        this._loading.style.display = 'none';
    }
    redirectURIorBackTo(backTo) {
        const redirectUri = decodeURIComponent(this._urlParams.get('redirect_uri') || '');
        if (redirectUri) {
            this._location.replace(backTo || redirectUri);
        }
    }
    getUrlParams(key) {
        return this._urlParams.get(key);
    }
    setUrlParams(key, value) {
        this._urlParams.set(key, value);
    }
    checkIfHasParamsResolver() {
        for (const [key] of this._paramsResolvers) {
            if (this._urlParams.get(key)) {
                return true;
            }
        }
        return false;
    }
    parseURLParams() {
        let search;
        const location = this._location;
        if (location.search.length > 2) {
            search = location.search.substring(1);
        }
        else if (location.hash.indexOf('=') > 0) {
            search = location.hash.substring(1);
        }
        const params = new Map();
        if (!search) {
            return params;
        }
        search.split('&').forEach((item) => {
            const [key, value] = item.split('=');
            params.set(key, value);
        });
        return params;
    }
}
