import React from "react";
import { AuthStore } from "auth/domain/authStore";
import { authenticationUseCase } from "auth/useCases/authenticationUseCase";
import { authInitUseCase } from "auth/useCases/authInitUseCase";
import { signoutUseCase } from "auth/useCases/signoutUseCase";

function useAuthViewModel(store: AuthStore) {
  const authenticate = React.useCallback(
    function (code: string) {
      authenticationUseCase(
        {
          authenticate: store.authenticate,
          isLogedIn: store.isLogedIn,
        },
        code
      );
    },
    [store.isLogedIn, store.authenticate]
  );

  const init = React.useCallback(
    function () {
      authInitUseCase({
        init: store.init,
      });
    },
    [store.init]
  );

  const signOut = React.useCallback(
    function() {
      console.warn("Sign out!")
      signoutUseCase({
        signOut: store.signOut
      })
    },
    [store.signOut]
  );

  return {
    isLogedIn: store.isLogedIn,
    isLoading: store.isLoading,
    isFailed: store.isFailed,
    authenticate,
    init,
    signOut,
  };
}

export { useAuthViewModel };
