import type { AxiosResponse } from 'axios';
import { default as Axios } from 'axios'
// import { IPayload } from 'model'
import { GlobalConfig, PayloadMessageTypes } from '../common'
import { tools } from '../store/Modules/tools'

import { helpers } from '@vuelidate/validators'
const { withAsync } = helpers

// const SITE_URL = GlobalConfig.uri.site
const VALIDATE_USER_URL = import.meta.env.VITE_MONGODB_HOST + '/users/'

export const aportadorexist = withAsync(_aportadorexist)

export function _aportadorexist(userName: string) {
  if (userName === tools.APORTADOR_NONE)
    return true

  let onSuccess = (res: AxiosResponse) => {
    // console.log('res.status', res.status)
    return res.status === PayloadMessageTypes.statusfound
  }

  return Axios.get(VALIDATE_USER_URL + tools.getEnv('VITE_APP_ID') + '/' + userName)
    .then(onSuccess)
    .catch((err) => {
      // console.log('err', err)
      return false
    })

}
