import React, { useEffect } from 'react';
import { useState } from 'react';
import type { IDataUserBank } from 'src/interface/IDataUserBank';
import { AuthContext } from './AuthContext';

type Props = {
  children: React.ReactNode;
};

export interface AuthState {
  token: string | null;
}

const AUTH_INITIAL_STATE = {
  token: null,
};

export const AuthProvider = ({ children }: Props) => {
  const [state, setState] = useState<AuthState>(AUTH_INITIAL_STATE);
  const [dataBank,setDataBank]=useState<IDataUserBank>()
  const [dataUserCreditor,setDataUserCreditor]=useState<IDataUserBank|null|undefined>(null)
  const [isTransaccion,setIsTransaccion]=useState<Boolean>(false)
  useEffect(() => {
    if(state.token){
      console.log(state.token)
    }
  }, [state.token]);
  const cargarUserCredit= (dataCreditor: IDataUserBank|null|undefined) =>{
    setDataUserCreditor(dataCreditor)
  }
  const sendTransaccion=(transaccion:boolean)=>{
    setIsTransaccion(transaccion)
  }
   const cargarToken = (token : string|null) =>{
          setState({token}); 
    }
  return (
    <AuthContext.Provider
      value={{
        ...state,
        cargarToken,
        dataBank,
        setDataBank,
        dataUserCreditor,
        cargarUserCredit,
        sendTransaccion,
        isTransaccion
      }
      }
    >
      {children}
    </AuthContext.Provider>
  );
};
