// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useRef } from 'react';
import clsx from 'clsx';
import { useSingleTabStopNavigation } from '@cloudscape-design/component-toolkit/internal';
import { useUniqueId } from '@cloudscape-design/component-toolkit/internal';
import { Divider, Resizer } from '../resizer';
import { useStickyCellStyles } from '../sticky-columns';
import { DEFAULT_COLUMN_WIDTH, useColumnWidths } from '../use-column-widths';
import { getStickyClassNames } from '../utils';
import { TableThElement } from './th-element';
import styles from './styles.css.js';
export function TableGroupHeaderCell({ group, colspan, rowspan, colIndex, groupId, resizableColumns, resizableStyle, onResizeFinish, updateGroupWidth, childColumnIds, focusedComponent, tabIndex, sticky, hidden, stripedRows, stickyState, cellRef, tableRole, resizerRoleDescription, resizerTooltipText, variant, tableVariant, columnGroupId, stickyColumnId, stickyBoundaryColumnId, isLast, wrapLines, }) {
    var _a, _b;
    const headerId = useUniqueId('table-group-header-');
    const { columnWidths } = useColumnWidths();
    // Effective min = sum of non-rightmost children's current widths (fixed) + rightmost child's minWidth
    const lastChild = childColumnIds[childColumnIds.length - 1];
    const groupMinWidth = childColumnIds.reduce((sum, id) => {
        if (id === lastChild) {
            return sum + DEFAULT_COLUMN_WIDTH;
        }
        return sum + (columnWidths.get(id) || DEFAULT_COLUMN_WIDTH);
    }, 0);
    const clickableHeaderRef = useRef(null);
    const { tabIndex: clickableHeaderTabIndex } = useSingleTabStopNavigation(clickableHeaderRef, { tabIndex });
    // Subscribe to the boundary column's sticky state to inherit shadow/clip-path classes.
    // The offset/position comes from stickyColumnId (first child); this only adds boundary classes.
    const boundaryStyles = useStickyCellStyles({
        stickyColumns: stickyState,
        columnId: (_a = stickyBoundaryColumnId !== null && stickyBoundaryColumnId !== void 0 ? stickyBoundaryColumnId : stickyColumnId) !== null && _a !== void 0 ? _a : groupId,
        getClassName: props => getStickyClassNames(styles, props),
    });
    // boundaryStyles.className is populated by scroll/intersection observers in the browser.
    // In JSDOM these observers don't fire, so this branch is only exercised in integration tests.
    const boundaryClassName = stickyBoundaryColumnId && boundaryStyles.className ? boundaryStyles.className : undefined;
    return (React.createElement(TableThElement, { resizableStyle: resizableStyle, cellRef: cellRef, focusedComponent: focusedComponent, sticky: sticky, resizable: resizableColumns, hidden: hidden, stripedRows: stripedRows, colIndex: colIndex, columnId: stickyColumnId !== null && stickyColumnId !== void 0 ? stickyColumnId : groupId, stickyState: stickyState, tableRole: tableRole, variant: variant, tableVariant: tableVariant, colSpan: colspan, rowSpan: rowspan, scope: "colgroup", isLast: isLast, columnGroupId: columnGroupId, className: boundaryClassName, boundaryRef: stickyBoundaryColumnId ? boundaryStyles.ref : undefined },
        React.createElement("div", { ref: clickableHeaderRef, "data-focus-id": `group-header-${groupId}`, className: clsx(styles['header-cell-content'], {
                [styles['header-cell-fake-focus']]: focusedComponent === `group-header-${groupId}`,
            }), "aria-label": (_b = group.ariaLabel) === null || _b === void 0 ? void 0 : _b.call(group, { sorted: false, descending: false, disabled: true }), tabIndex: clickableHeaderTabIndex },
            React.createElement("div", { className: clsx(styles['header-cell-text'], wrapLines && styles['header-cell-text-wrap']), id: headerId }, group.header)),
        resizableColumns ? (React.createElement(Resizer, { tabIndex: tabIndex, focusId: `resize-group-${groupId}`, showFocusRing: focusedComponent === `resize-group-${groupId}`, onWidthUpdate: newWidth => updateGroupWidth(groupId, newWidth), onWidthUpdateCommit: onResizeFinish, ariaLabelledby: headerId, minWidth: groupMinWidth, roleDescription: resizerRoleDescription, tooltipText: resizerTooltipText, isBorderless: variant === 'full-page' || variant === 'embedded' || variant === 'borderless', isLast: isLast, dividerPosition: columnGroupId ? 'full' : 'bottom' })) : (React.createElement(Divider, { position: columnGroupId ? 'full' : 'bottom' }))));
}
//# sourceMappingURL=group-header-cell.js.map