import { FC } from 'react';

import HuTableCell from '@material-hu/components/design-system/Table/components/TableCell';
import HuTableHead from '@material-hu/components/design-system/Table/components/TableHead';
import HuTableRow from '@material-hu/components/design-system/Table/components/TableRow';

import { insertIf } from 'src/utils/arrays';
import { useLokaliseTranslation } from 'src/utils/i18n';

type Header = {
  id: string;
  text: string;
};

export const defaultHeaders: (isFinalAuthor: boolean) => Header[] =
  isFinalAuthor => [
    { id: 'publication', text: 'drafts_for_review.post' },
    {
      id: isFinalAuthor ? 'creator' : 'reviewer',
      text: isFinalAuthor
        ? 'drafts_for_review.creator'
        : 'drafts_for_review.reviewer',
    },
    { id: 'target', text: 'drafts_for_review.target' },
  ];

type PostDraftHeaderProps = {
  isFinalAuthor?: boolean;
  isApproved?: boolean;
};

const PostDraftHeader: FC<PostDraftHeaderProps> = props => {
  const { isFinalAuthor, isApproved = false } = props;
  const { t } = useLokaliseTranslation('communication');

  const headers = [
    ...defaultHeaders(!!isFinalAuthor),
    ...insertIf(!isApproved, {
      id: 'draftStatus',
      text: isFinalAuthor ? 'drafts_for_review.draft_status' : '',
    }),
  ];

  return (
    <HuTableHead>
      <HuTableRow headerRow>
        {headers.map(header => (
          <HuTableCell
            headerCell
            key={header.id}
          >
            {t(header.text)}
          </HuTableCell>
        ))}
        {!isApproved && (
          <>
            <HuTableCell aria-label="review" />
            <HuTableCell aria-label="actions" />
          </>
        )}
      </HuTableRow>
    </HuTableHead>
  );
};
export default PostDraftHeader;
