import { useContext } from "react";
import { Account, AccountContext, AccountContextValue } from "./AccountProvider";
import { useDatabase } from "@/app/providers/DatabaseProvider/useDatabase";

export function useAccount() : [
    Account, () => void
] {
    const {runQuery} = useDatabase();
    const context : AccountContextValue = useContext(AccountContext);
    if(!context){
        throw new Error("useAccount must be used within a AccountProvider");
    }

    const deleteAccount = () => {
        runQuery("DELETE FROM `accounts` WHERE `public_key` = ?", [context.loginedAccount.publicKey])
        context.removeAccountsFromArrayOfAccounts(context.loginedAccount);
        context.loginAccount({
            privateHash: "",
            privatePlain: "",
            publicKey: "",
            privateKey: "",
            seedPhraseEncrypted: ""
        });
        localStorage.removeItem("last_logined_account");
    }

    return [context.loginedAccount, deleteAccount];
}