import VillageApi from "@/api/inhert/VillageApi";
import { confirm } from "@/components/dialog/CommonRoot";
import type { RequestApiError } from "@imengyu/imengyu-utils";

export function checkIsNotVolunteerError(e: unknown) {
  return (e as RequestApiError).errorMessage.includes('请认领')
}
export async function checkAndGoBindVolunteer() {
  //检查是否有志愿者信息，跳转至不同的页面
  //已认领志愿者，跳转至首页
  //未认领志愿者，跳转至绑定账号页面
  try {
    await VillageApi.getVolunteerInfo();
  } catch(e) {

    //已登录但是没绑定志愿者信息，提示用户绑定
    if (checkIsNotVolunteerError(e)) {
      const goBind = await confirm({
        title: '提示',
        content: '欢迎进入平台，您的账号未绑定志愿者账号，目前不能提交信息，是否前往绑定？',
        confirmText: '前往绑定',
        cancelText: '先看看，稍后绑定',
        width: 580,
      });
      if (goBind) {
        uni.redirectTo({ url: '/pages/dig/sharereg/bind' });
        return true;
      }
    }
    throw e;
  }
  return false;
}