import { css } from '@patternfly/react-styles';
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
import { Button, ButtonProps, ButtonVariant } from '../Button';

export interface DataListToggleProps extends React.HTMLProps<HTMLDivElement> {
  /** Additional classes added to the data list cell */
  className?: string;
  /** Flag to show if the expanded content of the data list item is visible */
  isExpanded?: boolean;
  /** Identify the data list toggle number */
  id: string;
  /** Id for the row */
  rowid?: string;
  /** Adds accessible text to the data list toggle */
  'aria-labelledby'?: string;
  /** Adds accessible text to the data list toggle */
  'aria-label'?: string;
  /** Allows users of some screen readers to shift focus to the controlled element. Should be used when the controlled contents are not adjacent to the toggle that controls them. */
  'aria-controls'?: string;
  /** Additional properties spread to the toggle button */
  buttonProps?: ButtonProps;
}

export const DataListToggle: React.FunctionComponent<DataListToggleProps> = ({
  className = '',
  isExpanded = false,
  'aria-controls': ariaControls = '',
  'aria-label': ariaLabel = 'Details',
  rowid = '',
  id,
  buttonProps,
  ...props
}: DataListToggleProps) => (
  <div className={css(styles.dataListItemControl, className)} {...props}>
    <div className={css(styles.dataListToggle)}>
      <Button
        id={id}
        variant={ButtonVariant.plain}
        aria-controls={ariaControls !== '' && ariaControls}
        aria-label={ariaLabel}
        aria-labelledby={ariaLabel !== 'Details' ? null : `${rowid} ${id}`}
        aria-expanded={isExpanded}
        {...buttonProps}
        icon={
          <div className={css(styles.dataListToggleIcon)}>
            <RhMicronsCaretDownIcon />
          </div>
        }
      />
    </div>
  </div>
);
DataListToggle.displayName = 'DataListToggle';
