'use client';

type ListSelectionBarProps = {
  /** HUB-372: bulk action buttons shown in the bar. */
  actions?: React.ReactNode;
  selectedCount: number;
  total: number;
  allMatchingSelected: boolean;
  noun: string;
  /** Used when one record is selected (sheet #9: "1 contact selected"). Default: `noun`. */
  singular?: string;
  onSelectAllMatching: () => void;
  onClear: () => void;
  /** The 28 Oct design's bar (`.lp-bulk`): count, "Select all", the actions, ×. */
  design?: boolean;
  /** EV9 (Events, list-page.js:2294 `bare`): the design bar's count without the noun. */
  bare?: boolean;
};

/** Shown under filters when rows are selected: count + optional "Select all N". */
export function ListSelectionBar({
  selectedCount,
  total,
  allMatchingSelected,
  actions,
  noun,
  singular,
  onSelectAllMatching,
  onClear,
  design = false,
  bare = false,
}: ListSelectionBarProps) {
  if (selectedCount <= 0 && !allMatchingSelected) return null;
  if (design) {
    const n = allMatchingSelected ? total : selectedCount;
    return (
      <div className="lp-bulk" role="region" aria-label="Bulk actions">
        <div className="lp-bulk__left">
          <strong>
            {bare
              ? `${n.toLocaleString()} selected`
              : `${n.toLocaleString()} ${n === 1 ? (singular ?? noun) : noun} selected`}
          </strong>
          {!allMatchingSelected && total > selectedCount ? (
            <button type="button" className="lp-link" onClick={onSelectAllMatching}>
              Select all {total.toLocaleString()} {noun}
            </button>
          ) : null}
        </div>
        {actions}
        <button
          type="button"
          className="lp-icon-btn lp-bulk__x"
          aria-label="Clear selection"
          onClick={onClear}
        >
          <svg
            className="lp-i"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
            aria-hidden
          >
            <path d="M18 6 6 18M6 6l12 12" />
          </svg>
        </button>
      </div>
    );
  }

  const label = allMatchingSelected
    ? `All ${total.toLocaleString()} ${noun} selected`
    : `${selectedCount.toLocaleString()} ${selectedCount === 1 ? (singular ?? noun) : noun} selected`;

  const showSelectAll = !allMatchingSelected && total > selectedCount;

  return (
    <div className="list-selection-bar" role="status">
      <div className="list-selection-bar__section">
        <span className="list-selection-bar__count">{label}</span>
      </div>
      {showSelectAll ? (
        <div className="list-selection-bar__section">
          <button
            type="button"
            className="list-selection-bar__select-all"
            onClick={onSelectAllMatching}
          >
            Select all {total.toLocaleString()} {noun}
          </button>
        </div>
      ) : null}
      {actions ? <div className="list-selection-bar__section bulk-bar">{actions}</div> : null}
      <div className="list-selection-bar__section list-selection-bar__section--end">
        <button type="button" className="list-selection-bar__clear" onClick={onClear}>
          Clear selection
        </button>
      </div>
    </div>
  );
}
