import { useCallback, useState, useEffect } from 'react';
import { KeyStore } from './keystore';
import type { Accounts, Hooks as HooksType } from './../types';

export const Hooks = {
  useKeyStore: () => {
    const [accounts, setAccounts] = useState<Accounts>();
    const [error, setError] = useState<string>();
    useEffect(() => {
      async function getAccounts() {
        try {
          setAccounts(await KeyStore.getAccounts());
        } catch (err: any) {
          setError(err);
        }
      }
      getAccounts();
    }, []);
    const newAccount = useCallback((passphrase: string) => {
      async function addAccount() {
        try {
          await KeyStore.newAccount(passphrase);
          setAccounts(await KeyStore.getAccounts());
        } catch (err: any) {
          setError(err);
        }
      }
      addAccount();
    }, []);
    return { accounts, error, newAccount };
  },
  useEthereumClient: () => {},
} as HooksType;
