import {z} from 'zod';
import { useAuth } from '@common/contexts/useAuth.ts';
import { useApi } from '@common/api/useApi.ts';

const autoSignRequestInfoSchema = z
  .object({
    clientId: z.string(), // 厂商id
    clientNme: z.string().optional(), // 厂商名
    clientName: z.string().optional(), // 另一个可能的厂商名
    userId: z.string().optional(),
    sysTag: z.string().optional(),
    cossSignId: z.string().nullish(),
  })
  .transform(data => {
    // 如果没有 clientName 但有 clientNme，将其重命名为 clientName
    if (!data.clientName && data.clientNme) {
      return {...data, clientName: data.clientNme};
    }
    return data;
  });

// 获取用户是否发起了自动签名授权的请求以及请求信息
// {"clientId":"2015112716143758","clientNme":"数字医信开发","forceQr":false,"notifyUrl":"http://8.131.71.70:12015/ywxSignBC/autoSignBC","sessionTime":4,"sysTag":"zht","userId":"99b00468e337456cb0005ad10d6d88da"}
const useAutoSignRequestInfo = () => {
  const {
    state: {userInfo},
  } = useAuth();
  return useApi(
    '/am/v3/selfSign/getSelfSignRequest',
    'POSTFORM',
    {
      userId: userInfo?.userId ?? '',
    },
    autoSignRequestInfoSchema,
    {
      loadingDelay: 500,
    },
  );
};
const useConfirmGrantAsk = (firmId?: string) => {
  return useApi(
    'am/v3/grant/getGrantToConfirm',
    'POSTJSON',
    {
      firmId,
    },
    z.array(
      z.object({
        businessType: z.string(), // 业务类型
        content: z.string(), // 授权信息
      }),
    ),
    {
      loadingDelay: 500,
    },
  );
};

export {useAutoSignRequestInfo, useConfirmGrantAsk};
