// Copyright 2017-2026 @pezkuwi/app-nis authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Vec } from '@pezkuwi/types';
import type { BalanceOf, ProxyDefinition } from '@pezkuwi/types/interfaces';
import type { ITuple } from '@pezkuwi/types/types';

import { useEffect, useState } from 'react';

import { createNamedHook, useAccounts, useApi, useIsMountedRef } from '@pezkuwi/react-hooks';

type ProxyResult = ITuple<[Vec<ProxyDefinition>, BalanceOf]>;

function useProxiesImpl (): Record<string, string[]> {
  const { api } = useApi();
  const { allAccounts } = useAccounts();
  const mountedRef = useIsMountedRef();
  const [state, setState] = useState<Record<string, string[]>>({});

  useEffect((): void => {
    if (allAccounts.length) {
      api.query.proxy.proxies
        .multi<ProxyResult>(allAccounts)
        .then((result) =>
          mountedRef.current && setState(
            result
              .map(([p], index): [string, string[]] => [
                allAccounts[index],
                p.map(({ delegate }) => delegate.toString())
              ])
              .filter(([, p]) => p.length)
              .reduce((all, [a, p]) => ({ ...all, [a]: p }), {})
          )
        )
        .catch(console.error);
    }
  }, [allAccounts, api, mountedRef]);

  return state;
}

export const useProxies = createNamedHook('useProxies', useProxiesImpl);
