"use strict";
const common_vendor = require("../../../../common/vendor.js");
class Router {
  // route: (options?: string | RouterConfig, params?: Record<string, any>) => Promise<void>;
  constructor() {
    this.config = {
      type: "navigateTo",
      url: "",
      delta: 1,
      // navigateBack页面后退时,回退的层数
      params: {},
      // 传递的参数
      animationType: "pop-in",
      // 窗口动画,只在APP有效
      animationDuration: 300,
      // 窗口动画持续时间,单位毫秒,只在APP有效
      intercept: false
      // 是否需要拦截
    };
    this.route = this.route.bind(this);
  }
  // 判断url前面是否有"/"，如果没有则加上，否则无法跳转
  addRootPath(url) {
    return url[0] === "/" ? url : `/${url}`;
  }
  // 整合路由参数
  mixinParam(url, params) {
    url = url && this.addRootPath(url);
    let query = "";
    if (/.*\/.*\?.*=.*/.test(url)) {
      query = common_vendor.index.$u.queryParams(params, false);
      return url + "&" + query;
    } else {
      query = common_vendor.index.$u.queryParams(params);
      return url + query;
    }
  }
  /**
   * 路由跳转主方法
   * @param options 跳转配置或url字符串
   * @param params 跳转参数
   */
  async route(options = {}, params = {}) {
    let mergeConfig = {};
    if (typeof options === "string") {
      mergeConfig.url = this.mixinParam(options, params);
      mergeConfig.type = "navigateTo";
    } else {
      mergeConfig = common_vendor.index.$u.deepMerge(this.config, options);
      mergeConfig.url = this.mixinParam(options.url || "", options.params || {});
    }
    if (params.intercept) {
      this.config.intercept = params.intercept;
    }
    mergeConfig.params = params;
    mergeConfig = common_vendor.index.$u.deepMerge(this.config, mergeConfig);
    if (common_vendor.index.$u.routeIntercept && typeof common_vendor.index.$u.routeIntercept === "function") {
      const isNext = await new Promise((resolve) => {
        common_vendor.index.$u.routeIntercept(mergeConfig, resolve);
      });
      isNext && this.openPage(mergeConfig);
    } else {
      this.openPage(mergeConfig);
    }
  }
  // 执行路由跳转
  openPage(config) {
    const { url = "", type = "", delta = 1, animationDuration = 300 } = config;
    if (type == "navigateTo" || type == "to") {
      common_vendor.index.navigateTo({ url, animationDuration });
    }
    if (type == "redirectTo" || type == "redirect") {
      common_vendor.index.redirectTo({ url });
    }
    if (type == "switchTab" || type == "tab") {
      common_vendor.index.switchTab({ url });
    }
    if (type == "reLaunch" || type == "launch") {
      common_vendor.index.reLaunch({ url });
    }
    if (type == "navigateBack" || type == "back") {
      common_vendor.index.navigateBack({ delta });
    }
  }
}
const route = new Router().route;
exports.route = route;
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/uview-pro/libs/function/route.js.map
