import {z} from 'zod';
import { MD5_KEY } from '@common/constants';
import { md5_hex } from '@common/utils/md5';
import { useApi } from '@common/api/useApi.ts';

// 获取短信验证码
const useSMSVerifyCode = (
  phone: string, // 手机号
  type: 'register' | 'login' | 'reset' | 'change', // 类型：注册/登录/重置密码/未登录更改手机
  username?: string, // 用户姓名（更改手机号类型传）
  idCardNumber?: string, // 身份证号（更改手机号类型传）
) => {
  const time = new Date().getTime();
  const typeCode =
    type === 'login' ? 1 : type === 'reset' ? 2 : type === 'register' ? 3 : 5; // 1：登录；2：重置密码；3：注册 5：未登录更改手机号
  const signOrigin = `phoneNum=${phone}&time=${time}&type=${typeCode}#${MD5_KEY}`;
  const sign = md5_hex(signOrigin);

  return useApi(
    '/am/v3/userCenter/account/getSMSVerifyCode',
    'POSTJSON',
    {
      phoneNum: phone,
      userName: username,
      userIdCardNum: idCardNumber,
      type: typeCode,
      time: time,
      sign: sign,
    },
    z.unknown(),
    {},
  );
};

export {useSMSVerifyCode};
