import { Controller } from '@hotwired/stimulus';

import { forwardsToSelection } from './row_select.ts';

/**
 * Forwards a click on a selectable list row (the table row or the narrow-screen card) to its
 * selection control, so the whole row selects. Clicks on interactive elements — the control
 * itself, buttons, links, fields and anything with its own action — are left alone, so one
 * click toggles exactly once.
 */
export default class RowSelectController extends Controller<HTMLElement> {
    select(event: Event): void {
        const target = event.target;
        if (target instanceof Element && !forwardsToSelection(this.element, target)) {
            return;
        }
        this.element.querySelector<HTMLInputElement>('input[data-row-select]')?.click();
    }
}
