import { FC, memo } from 'react';

import { useAuth } from 'src/contexts/JWTContext';
import { canMarkAsKeyUpdate } from 'src/utils/feed';

import { GenericPostMenu } from './GenericPostMenu';
import MarkPostAsKeyUpdateMenuItem from './MarkPostAsKeyUpdateMenuItem';

export type KeyUpdatePostMenuProps = {
  id: number;
  isGroupPost: {
    value: boolean;
    groupId: number;
  };
  isPendingApproval?: boolean;
  isSegmented?: boolean;
};

export const KeyUpdatePostMenu: FC<KeyUpdatePostMenuProps> = props => {
  const {
    id,
    isGroupPost,
    isPendingApproval = false,
    isSegmented = false,
  } = props;
  const { permissions } = useAuth();

  const showMarkAsKeyUpdate =
    !isPendingApproval && !isSegmented && canMarkAsKeyUpdate(permissions);

  const menuOptions = [
    {
      id: 'mark-as-key-update',
      enabled: showMarkAsKeyUpdate,
      option: (
        <MarkPostAsKeyUpdateMenuItem
          id={id}
          isKeyUpdate
          isGroupPost={isGroupPost.value}
          groupId={isGroupPost.groupId}
        />
      ),
    },
  ];

  return (
    <GenericPostMenu
      id={id}
      menuOptions={menuOptions}
    />
  );
};

export default memo(KeyUpdatePostMenu);
