import type { Treaty } from "@elysiajs/eden";
import { client, safeClient } from "@/api";

export type Wallet = Treaty.Data<typeof client.api.wallet.wallets.get>[number];

export const useWalletStore = defineStore("wallet", () => {
  const wallets = ref<Wallet[]>([]);

  async function syncWallets() {
    const { data } = await safeClient(client.api.wallet.wallets.get(), { silent: true });
    wallets.value = data.value?.sort((a, b) => a.walletType.sortOrder - b.walletType.sortOrder) || [];
  }
  async function getWalletByType(type: Wallet["walletType"]["code"]) {
    const { data } = await safeClient(client.api.wallet.wallet_by_code({ walletTypeCode: type }).get(), { silent: true });

    return data;
  }

  return {
    wallets,
    syncWallets,
    getWalletByType,
  };
});
