import type { HTMLAttributes } from 'react';

type DetailTriggerRowOptions = {
  expanded: boolean;
  label: string;
  onOpen: () => void;
  className?: string;
  testId?: string;
};

type DetailTriggerRowProps = HTMLAttributes<HTMLElement> & {
  role: 'button';
  tabIndex: 0;
  'aria-expanded': boolean;
  'data-testid'?: string;
};

export function detailTriggerRow({
  expanded,
  label,
  onOpen,
  className = '',
  testId
}: DetailTriggerRowOptions): DetailTriggerRowProps {
  return {
    className,
    role: 'button',
    tabIndex: 0,
    'aria-expanded': expanded,
    'aria-label': label,
    ...(testId ? { 'data-testid': testId } : {}),
    onClick: onOpen,
    onKeyDown: (event) => {
      if (event.key !== 'Enter' && event.key !== ' ') return;
      event.preventDefault();
      onOpen();
    }
  };
}
