import { AxiosResponse } from 'axios';
import { accountClient } from '../axios';

interface IUseLogin {
	register: (nickname: string | undefined, login: string | undefined, password: string | undefined) => Promise<AxiosResponse>,
	canRegister: (nickname: string | undefined, login: string | undefined, password: string | undefined) => boolean,
  }

export default function useRegister(): IUseLogin {
	const register = async (nickname: string | undefined, login: string | undefined, password: string | undefined) => {
			if (!canRegister(nickname, login, password)) return Promise.reject('invalig nicikname or login or password');
			// return userClient.post('register', { nickname: nickname, login: login, password: password });
            try{
                const r = await accountClient.post('register', { nickname: nickname, login: login, password: password });
                return Promise.resolve(r);
            }
            catch (exception)
            {
                console.log('eerror');
                console.log(exception);
                return Promise.reject(exception);
            }
		}

	const canRegister = (nickname: string | undefined, login: string | undefined, password: string | undefined) => {
		return nickname != undefined && nickname.length > 0 && 
			login != undefined && login.length > 0 &&
			password != undefined && password.length > 0;
	}

	return { register, canRegister };
}