/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import React__default from 'react';
import { DefaultCellWidth } from './index.js';
import { getBuildColumns } from './util.js';

function ColGroup(_a) {
    var columns = _a.columns, colWidths = _a.colWidths, isFixed = _a.isFixed, syncTableWidth = _a.syncTableWidth, initTableWidth = _a.initTableWidth, selectable = _a.selectable, expandable = _a.expandable, draggable = _a.draggable, rowSelectionColumnWidth = _a.rowSelectionColumnWidth, expandableColumnWidth = _a.expandableColumnWidth, isRightExpandable = _a.isRightExpandable, isLeftExpandable = _a.isLeftExpandable, showReal = _a.showReal;
    var tdColumns = getBuildColumns(columns).tdColumns;
    var domRef = React__default.createRef();
    React__default.useEffect(function () {
        if (domRef.current) {
            initTableWidth === null || initTableWidth === void 0 ? void 0 : initTableWidth();
            syncTableWidth === null || syncTableWidth === void 0 ? void 0 : syncTableWidth();
        }
    }, []);
    React__default.useEffect(function () {
        if (!syncTableWidth) {
            return;
        }
        var table = domRef.current.parentElement;
        var observer = new MutationObserver(function () {
            syncTableWidth();
        });
        observer.observe(table, {
            attributes: true,
            childList: true,
            subtree: true
        });
        return function () {
            observer.disconnect();
        };
    }, []);
    return (React__default.createElement("colgroup", { ref: domRef },
        draggable ? React__default.createElement("col", { style: { width: DefaultCellWidth + 'px' } }) : null,
        selectable ? (React__default.createElement("col", { style: { width: rowSelectionColumnWidth + 'px' } })) : null,
        expandable && isLeftExpandable ? (React__default.createElement("col", { style: { width: expandableColumnWidth + 'px' } })) : null,
        tdColumns.map(function (col, index) {
            var _a, _b;
            var style = {};
            if ((_a = colWidths[col === null || col === void 0 ? void 0 : col.name]) === null || _a === void 0 ? void 0 : _a.width) {
                style.width = colWidths[col === null || col === void 0 ? void 0 : col.name].width;
            }
            else if (col.width) {
                style.width = col.width;
            }
            else if (showReal) {
                style.width = (_b = colWidths[col === null || col === void 0 ? void 0 : col.name]) === null || _b === void 0 ? void 0 : _b.realWidth;
            }
            if (!isFixed && style.width) {
                style.minWidth = style.width;
            }
            return React__default.createElement("col", { style: style, key: index });
        }),
        expandable && isRightExpandable ? (React__default.createElement("col", { style: { width: expandableColumnWidth + 'px' } })) : null));
}

export { ColGroup as default };
