import { createContext, useContext, useState } from "react";
import { FC } from "react";
import { Login } from "./login";

export interface AuthContext {
  accessToken: string | undefined;
  setAccessToken: (token: string) => void;
  setRefreshToken: (token: string) => void;
  refreshToken: string | undefined;
  login: (dto: any) => void;
  account?: any;
  setAccount: (dto: any) => void;
}
const Context = createContext({} as AuthContext);

export const useAuth = () => useContext(Context);

export const AuthProvider: FC = ({ children }) => {
  const [accessToken, setAccessToken] = useState<string>();
  const [refreshToken, setRefreshToken] = useState<string>();
  const [account, setAccount] = useState<any>();

  const login = (dto: any) => {
    setAccessToken(dto.accessToken);
    setRefreshToken(dto.refreshToken);
    setAccount(dto.account);
  };

  return (
    <Context.Provider
      value={{
        accessToken,
        setAccessToken,
        refreshToken,
        setRefreshToken,
        login,
        account,
        setAccount,
      }}
    >
      {children}
      {/* {accessToken ? null : <Login />} */}
    </Context.Provider>
  );
};
