import { IWebViewParams } from "@/pages/webView";
import Taro from "@tarojs/taro";
import qs from "qs";
import { IRoutesUrls } from "../routes/route.h5";

export default class RouteUtil {

    /**跳转页面 */
    public static push<T>(_url: IRoutesUrls, option?: { params: T }) {
        if (option?.params) {
            _url += "?" + qs.stringify(option.params, { "encode": false });
        }
        Taro.navigateTo({
            url: _url,
        });
    }

    public static replace<T>(_url: IRoutesUrls, option?: { params: T }) {
        if (option?.params) {
            _url += "?" + qs.stringify(option.params, { "encode": false });
        }
        Taro.redirectTo({
            url: _url,
        });
    }

    public static reLaunch(_url: IRoutesUrls) {
        Taro.reLaunch({ url: _url })
    }

    /**返回 */
    public static pop(stack: number = 1) {
        Taro.navigateBack({ "delta": stack });
    }

    /**删除最后一个画面 */
    public static removeLast() {

    }

    public static toWebViewPage(options: IWebViewParams, obj?: { replace: boolean }) {
        const { replace } = obj || {};
        options.url = encodeURIComponent(options.url);
        RouteUtil[replace ? "replace" : "push"]<IWebViewParams>("/pages/webView/index", { params: options })
    }
}