export function to(path: string) {
  console.log(path)
  if (nextPageIsPrevPage(path)) {
    uni.navigateBack();
    return;
  }

  uni.navigateTo({
    url: path,
  });
}

export function back() {
  uni.navigateBack();
}

export function redirect(url: string) {
  uni.redirectTo({
    url,
  });
}

export function reLaunch() {
  uni.reLaunch({
    url: "/pages/map/map",
  });
}

export function nextPageIsPrevPage(path: string): boolean {
  const isFull = path[0] === "/";
  const pages = getCurrentPages();
  const prevPage = pages.length >= 2 ? pages[pages.length - 2] : undefined;
  const prevPageQuery = prevPage
    ? Object.keys(prevPage.options)
        .map((key) => `${key}=${prevPage.options[key]}`)
        .join("&")
    : "";
  if (prevPage) {
    return (
      `${prevPage.route}${prevPageQuery ? "?" + prevPageQuery : ""}` ===
      (isFull ? path.substring(1) : path)
    );
  }
  return false;
}
