import type { AxiosError } from 'axios';
import { joinURL } from 'ufo';
import { createPassportSvg as _createPassportSvg, createKycSvg as _createKycSvg } from '@arcblock/ux/lib/Util/passport';
import { BLOCKLET_SERVICE_PATH_PREFIX } from '@arcblock/ux/lib/Util/constant';
import { CreatePassportProps } from '../../@types';

export const formatAxiosError = (err: AxiosError) => {
  const { response } = err;

  if (response) {
    return `Request failed: ${response.status} ${response.statusText}: ${JSON.stringify(response.data)}`;
  }

  return err.message;
};

export const createPassportSvg = (props: CreatePassportProps) => {
  if (props.scope === 'kyc') {
    return _createKycSvg({
      ...props,
      issuerAvatarUrl: joinURL(window.location.origin, BLOCKLET_SERVICE_PATH_PREFIX, '/blocklet/logo'),
      type: props.role,
    });
  }

  return _createPassportSvg({
    ...props,
    issuerAvatarUrl: joinURL(window.location.origin, BLOCKLET_SERVICE_PATH_PREFIX, '/blocklet/logo'),
  });
};
