import { useNetSocketStore } from "any-hooks/communication/useNetSocketStore";
import { UserData } from "any-hooks/types/user";
import { reactive } from "vue";
import { useInjector, useRequest } from "vue-vulcan";
import { useAuthData } from "./useAuthData";

let LOGIN_STATE = false

export function useLogin() {
	const loginData = reactive({
		login_id: '',
		login_password: ''
	})
	const [, request] = useRequest<UserData>('/loginIn', { auto: false });
	const { setAuth } = useInjector(useAuthData);
	const { sendMsg } = useInjector(useNetSocketStore);

	const peer_id = new Date().getTime().toString().substr(7,).replace('0', '1');
	console.log('init peer_id', peer_id)

	const submitLogin = () => {
		return request({...loginData, peer_id})
			.then( data => {
				LOGIN_STATE = true;
				setAuth(data); 				
				return data
			} )
	}

	return {
		loginData,
		LOGIN_STATE,
		submitLogin
	}
}

