
import { useState,  useEffect } from 'react';

import Request, {YWRequestConfig} from '@ywfe/request';
// @ts-ignore
import * as querystring from 'querystring';
// @ts-ignore
import { ParsedUrlQueryInput } from 'querystring';
// @ts-ignore
import * as R from 'ramda';
import {configs} from './config';


  const projectId = "PROAUTH";
  let env;
  if(typeof window === 'undefined'){
    env = (process.env.REACT_APP_ENV === 'prod' ? 'prod': process.env.REACT_APP_ENV) || 'prod';
  } else {
    env = window.localStorage.getItem('env') || (process.env.REACT_APP_ENV === 'prod' ? 'prod': process.env.REACT_APP_ENV) || 'prod';
  }
  
  const basePath = configs[projectId] ? configs[projectId][env].API_BASE_URL + `${env === 'mock' ? '/' + 88 : ''}` : '';
  const loginPath = configs['AUTH'] ? configs['AUTH'][env].APPLICATION_URL : '';
  

  

// @ts-ignore




export interface UserSendmtcodeInput {
  
    
    /** 手机号 */
    telephone?: string;
  
    
    /** REGISTER RESET_PWD OLD_PHONE NEW_PHONE WX_BIND MANAGER_BANKCARD ADD_ALIPAY_ACCOUNT ACCOUNT_AUTHENTICATION */
    codeType?: string;
  
}

export interface UserSendmtcodeData {
  
}


const reduceParams = (data: R.Dictionary<UserSendmtcodeInput>) => {
  if(data instanceof FormData){
    return data;
  }
  const result = R.filter(item => !R.isNil(item) && !R.isEmpty(item))(data) || {};
  return result as unknown as UserSendmtcodeInput;
}


// 19969 发送手机号码验证码

  export const usePostUserSendmtcode = () => {
    let isMount = true;
    const [isUserSendmtcodeLoading, setIsUserSendmtcodeLoading] = useState(false);
    const [dataUserSendmtcode, setDataUserSendmtcode] = useState<UserSendmtcodeData>({} as UserSendmtcodeData);
    const postUserSendmtcodeData = async(inputs: UserSendmtcodeInput, config?: YWRequestConfig): Promise<UserSendmtcodeData> => {
      
        const params = reduceParams(inputs as unknown as R.Dictionary<UserSendmtcodeInput>) || {};
      
      if(isMount){
        setIsUserSendmtcodeLoading(true);
      }
      
      try{
        
          
          
          const res: any = await Request.post<UserSendmtcodeData>(`${basePath}/user/sendMtCode`, params, {
            loginHost: loginPath,
            ...config
          });
          
          
          if(isMount){
            setDataUserSendmtcode(res || {});
            setIsUserSendmtcodeLoading(false);
          }
          return res;
      } catch(error){
        console.log(error);
        if(isMount){
          setIsUserSendmtcodeLoading(false);
        }
        return false as unknown as UserSendmtcodeData;
      }
      
    }

    useEffect(() => {
      return () => {
        isMount = false;
      };
    }, []);
  
    return {
      isUserSendmtcodeLoading,
      dataUserSendmtcode,
      postUserSendmtcodeData,
    }
  }

