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

import type { DeriveProposal } from '@pezkuwi/api-derive/types';

import React, { useRef } from 'react';

import { Table } from '@pezkuwi/react-components';
import { useApi, useCall } from '@pezkuwi/react-hooks';

import { useTranslation } from '../translate.js';
import ProposalDisplay from './Proposal.js';

interface Props {
  className?: string;
}

function Proposals ({ className }: Props): React.ReactElement<Props> {
  const { t } = useTranslation();
  const { api } = useApi();
  const proposals = useCall<DeriveProposal[]>(api.derive.democracy.proposals);

  const headerRef = useRef<([React.ReactNode?, string?, number?] | false)[]>([
    [t('proposals'), 'start', 2],
    [t('proposer'), 'address'],
    [t('locked'), 'media--1200'],
    [undefined, undefined, 2]
  ]);

  return (
    <Table
      className={className}
      empty={proposals && t('No active proposals')}
      header={headerRef.current}
    >
      {proposals?.map((proposal): React.ReactNode => (
        <ProposalDisplay
          key={proposal.index.toString()}
          value={proposal}
        />
      ))}
    </Table>
  );
}

export default React.memo(Proposals);
