"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataGrid = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const ConfigProvider_1 = require("../ConfigProvider");
const ContentState_1 = require("../ContentState");
const classNames_1 = require("../utils/classNames");
const cva_1 = require("../utils/cva");
const getInertProps_1 = require("../utils/getInertProps");
const Body_1 = require("./Body");
const constants_1 = require("./constants");
const DataGridContext_1 = require("./DataGridContext");
const enums_1 = require("./enums");
const Head_1 = require("./Head");
const Loader_1 = require("./Loader");
const MobileSkeleton_1 = require("./MobileSkeleton");
const NoData_1 = require("./NoData");
const PinnedSections_1 = require("./PinnedSections");
const styles_1 = require("./styles");
const useLogic_1 = require("./useLogic");
const pinnedSectionsCva = (0, cva_1.cva)(constants_1.dataGridClassnames.pinnedSections, {
    variants: {
        isDisabled: {
            true: constants_1.dataGridClassnames.disabled,
        },
    },
});
const dataGridCva = (0, cva_1.cva)(constants_1.dataGridClassnames.root, {
    variants: {
        hasPinnedColumns: {
            true: constants_1.dataGridClassnames.hasPinnedSections,
        },
        isAllowHorizontalScroll: {
            true: constants_1.dataGridClassnames.isAllowHorizontalScroll,
        },
        gridViewMode: {
            full: constants_1.dataGridClassnames.gridViewFull,
            standard: constants_1.dataGridClassnames.gridViewStandard,
        },
        hasGroupedColumns: {
            true: constants_1.dataGridClassnames.hasGroupedColumns,
        },
    },
});
const bodyCva = (0, cva_1.cva)(constants_1.dataGridClassnames.body, {
    variants: {
        isNoData: {
            true: constants_1.dataGridClassnames.bodyEmpty,
        },
    },
});
const DataGrid = (props) => {
    const { dataGridClassName, isDataGridDisabled, treeRenderConfig, headColumns, headProps, bodyProps, gridColumns, loaderProps, renderRows, isAllowHorizontalScroll, isHideHead, hasPinnedColumns, contentStateProps, rowHoverProps, containerRef, sections, isNoData, isMobile, mobileSkeletonProps, isLoading, containerCssVars, hasGroupedColumns, gridViewMode, } = (0, useLogic_1.useLogic)(props);
    const { emptySymbol } = (0, react_1.useContext)(ConfigProvider_1.ConfigContext);
    const { columns, selectedRows = [], minDisplayRows = constants_1.MIN_DISPLAY_ROWS_BY_DEFAULT, variant = enums_1.Variant.Tree, footer, noDataPlaceholder, subrows, keyId, activeRowId, emptyCellValue = emptySymbol, onRowClick, noDataOptions, headPreAddon, } = props;
    const { moreButtonColumnPosition = 1, isVisibleCollapseButton = true } = subrows || {};
    const { isInitialExpanded = false, expandedLevel = constants_1.EXPANDED_LEVEL_BY_DEFAULT, initialVisibleChildrenCount = constants_1.INITIAL_OPENED_NESTED_CHILDREN_COUNT_BY_DEFAULT, } = treeRenderConfig || {};
    const Wrapper = isDataGridDisabled
        ? styles_1.DisabledDataGridWrapper
        : styles_1.DataGridWrapper;
    const renderedPlaceholder = (0, react_1.useCallback)(() => {
        if (!isLoading) {
            return noDataPlaceholder || (0, jsx_runtime_1.jsx)(NoData_1.NoData, { ...noDataOptions });
        }
        return null;
    }, [noDataPlaceholder, noDataOptions, isLoading]);
    const commonBodyProps = {
        ...bodyProps,
        activeRowId,
        keyId,
        selectedRows,
        minDisplayRows,
        rows: renderRows,
        variant,
        emptyCellValue,
        isInitialExpanded,
        expandedLevel,
        initialVisibleChildrenCount,
        moreButtonColumnPosition,
        isVisibleCollapseButton,
        noDataPlaceholder: renderedPlaceholder(),
        onRowClick,
        ...rowHoverProps,
    };
    const renderTableContent = () => {
        if (!hasPinnedColumns || isMobile) {
            return ((0, jsx_runtime_1.jsxs)(Wrapper, { ...(0, getInertProps_1.getInertProps)(isDataGridDisabled), className: constants_1.dataGridClassnames.wrapper, children: [headPreAddon, !isHideHead && ((0, jsx_runtime_1.jsx)(Head_1.Head, { ...headProps, columns: headColumns, gridColumns: gridColumns })), (0, jsx_runtime_1.jsx)(ContentState_1.ContentState, { ...contentStateProps, loadingContent: isMobile && (0, jsx_runtime_1.jsx)(MobileSkeleton_1.MobileSkeleton, { ...mobileSkeletonProps }), children: (0, jsx_runtime_1.jsx)(Body_1.Body, { ...commonBodyProps, columns: columns, gridColumns: gridColumns, className: bodyCva({
                                isNoData,
                            }) }) })] }));
        }
        return ((0, jsx_runtime_1.jsx)(ContentState_1.ContentState, { ...contentStateProps, children: (0, jsx_runtime_1.jsx)(PinnedSections_1.PinnedSections, { sections: sections, className: pinnedSectionsCva({
                    isDisabled: isDataGridDisabled,
                }), isDisabled: isDataGridDisabled, headProps: headProps, gridColumns: gridColumns, bodyProps: commonBodyProps, isNoData: isNoData }) }));
    };
    return ((0, jsx_runtime_1.jsx)(DataGridContext_1.DataGridContextProvider, { isLoading: isLoading, children: (0, jsx_runtime_1.jsxs)(styles_1.Container, { ref: containerRef, style: containerCssVars, className: (0, classNames_1.classNames)(dataGridClassName, dataGridCva({
                isAllowHorizontalScroll,
                hasPinnedColumns,
                gridViewMode,
                hasGroupedColumns,
            })), children: [renderTableContent(), (0, jsx_runtime_1.jsx)(Loader_1.Loader, { ...loaderProps }), footer && footer] }) }));
};
exports.DataGrid = DataGrid;
