import { clsx } from 'clsx';

import ActionButton from '../actionButton';
import {
  CommonProps,
  Priority,
  PriorityPrimary,
  PrioritySecondary,
  PriorityTertiary,
} from '../common';
import Option from '../common/Option';

export type ActionOptionProps = {
  complex?: boolean;
  content?: React.ReactNode;
  disabled?: boolean;
  media?: React.ReactNode;
  showMediaAtAllSizes?: boolean;
  onClick: () => void;
  title: React.ReactNode;
  action: React.ReactNode;
  priority?: PriorityPrimary | PrioritySecondary | PriorityTertiary;
  showMediaCircle?: boolean;
  isContainerAligned?: boolean;
  as?: React.ElementType;
} & CommonProps;

const ActionOption = ({
  action,
  priority = Priority.PRIMARY,
  disabled,
  onClick,
  media,
  title,
  content,
  complex,
  className,
  showMediaAtAllSizes,
  showMediaCircle,
  isContainerAligned,
  as,
}: ActionOptionProps) => {
  const sharedProps = {
    media,
    title,
    content,
    complex,
    disabled,
    showMediaAtAllSizes,
    showMediaCircle,
    className: clsx('tw-action-option', className),
    isContainerAligned,
    as,
  };

  const getAriaLabel = (): string | undefined => {
    const labels: string[] = [];

    if (typeof title === 'string') {
      labels.push(title);
    }

    if (typeof action === 'string') {
      labels.push(action);
    }

    return labels.join(', ') || undefined;
  };

  return (
    <Option
      {...sharedProps}
      button={
        <ActionButton
          priority={priority}
          disabled={disabled}
          aria-label={getAriaLabel()}
          onClick={onClick}
        >
          {action}
        </ActionButton>
      }
    />
  );
};

export default ActionOption;
