"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var handle_1 = require("./handle");
var config_1 = require("./config");
var config_2 = require("../config/config");
exports.default = {
    navigateBack: function (data) {
        return uni.navigateBack(data);
    },
    switchTab: function (data) {
        return this.router('switchTab', data, false);
    },
    reLaunch: function (data) {
        return this.router('reLaunch', data);
    },
    navigateTo: function (data) {
        return this.router('navigateTo', data);
    },
    redirectTo: function (data) {
        return this.router('redirectTo', data);
    },
    autoRouter: function (data, defaultMode) {
        if (defaultMode === void 0) { defaultMode = 'navigateTo'; }
        var resultData;
        if (typeof data === 'string') {
            resultData = { url: data };
        }
        else {
            resultData = data;
        }
        var mode = undefined;
        if (typeof data === 'string') {
            var url = resultData.url;
            var urls = url.split('?') || [];
            resultData.params = urls[1];
            url = urls[0] || '';
            if (url.indexOf('/') === 0) {
                url = url.substr(1);
            }
            urls = url.split('/') || [];
            url = urls[urls.length - 1] || '';
            resultData.name = url;
            if (config_1.default.tabBar.indexOf(url) >= 0) {
                mode = 'switchTab';
            }
        }
        console.log(mode || defaultMode, resultData);
        return this.router(mode || defaultMode, resultData);
    },
    getLivePath: function (roomId, params) {
        var path = config_1.default.live_path;
        var useParams = [];
        if (roomId) {
            useParams.push('room_id=' + roomId);
        }
        if (params) {
            var toStringParams = '';
            if (typeof params === 'object')
                toStringParams = JSON.stringify(params);
            useParams.push('custom_params=' + encodeURIComponent(toStringParams));
        }
        if (useParams.length > 0) {
            path += '?' + useParams.join('&');
        }
        return path;
    },
    getMapPath: function (params) {
        var resultParams;
        if (typeof params === "string") {
            resultParams = params;
        }
        else {
            resultParams = this.getParamsString(__assign(__assign({}, params), config_2.default.map));
        }
        return config_1.default.map_path + resultParams;
    },
    routerParams: {
        next: true,
        delay: config_1.default.routerDelay,
    },
    router: function (name, data, params) {
        var _this = this;
        if (params === void 0) { params = true; }
        if (this.routerParams.next === false)
            return false;
        this.routerParams.next = false;
        setTimeout(function () {
            _this.routerParams.next = true;
        }, this.routerParams.delay);
        var resultData;
        if (typeof data === 'string') {
            resultData = { name: data, url: '' };
        }
        else {
            resultData = data;
        }
        resultData.url = resultData.path || resultData.name || '';
        if (resultData.name && handle_1.default[resultData.name]) {
            resultData = handle_1.default[resultData.name].call(this, data);
        }
        else if (resultData.url.indexOf('/') < 0) {
            resultData.url = '/pages/' + resultData.url + '/' + resultData.url;
        }
        if (resultData.subcontract === undefined) {
            resultData.subcontract = this.inferSubPages(resultData.url);
        }
        if (resultData.subcontract) {
            resultData.url = '/' + resultData.subcontract + resultData.url;
        }
        if (resultData.params && params) {
            resultData.url += this.getParamsString(resultData.params);
            delete resultData.params;
        }
        if (resultData.memoryParams) {
            this.memorySet(resultData.name || this.getRouterName(resultData.url), resultData.memoryParams);
        }
        return uni[name](resultData);
    },
    getParamsString: function (params) {
        if (params) {
            if (typeof params === 'string') {
                if (params.indexOf('?') === 0) {
                    return params;
                }
                else {
                    return '?' + params;
                }
            }
            var params_string = '';
            for (var key in params) {
                if (params.hasOwnProperty(key)) {
                    params_string += (params_string ? '&' : '?') + key + '=' + (typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key]);
                }
            }
            return params_string;
        }
        else {
            return '';
        }
    },
    getRouterName: function (path) {
        if (path === void 0) { path = ''; }
        return path.split('/').pop();
    },
    inferSubPages: function (url) {
        url = url || '';
        var urls = url.split('/');
        var name = urls[urls.length - 1];
        if (config_1.default.subcontractIndexes[name]) {
            return config_1.default.subcontract[config_1.default.subcontractIndexes[name]];
        }
        else {
            return undefined;
        }
    },
    memoryData: {},
    memoryGet: function (name) {
        if (this.memoryData[name]) {
            var resultData = this.memoryData[name];
            delete this.memoryData[name];
            return resultData;
        }
        else {
            return undefined;
        }
    },
    memorySet: function (name, params) {

        this.memoryData[name] = params;
    }
};