import * as React from 'react';
import { Box } from 'rebass';
import FormLayout from '../../../../components/FormLayout';
import { columnFilter } from './Utilities';
import { RadioGroup } from '../../../../components/Radio';
import { Tabs } from '../../../../components/Tabs';
import { Tag } from '../../../../components/Tag';
import { useAdaptable } from '../../../AdaptableContext';
import { ValueSelector } from '../../../Components/ValueSelector';
import { useOnePageAdaptableWizardContext } from '../../../Wizard/OnePageAdaptableWizard';
import ArrayExtensions from '../../../../Utilities/Extensions/ArrayExtensions';
import { normalizeLayout } from '../../../../Api/Implementation/LayoutHelpers';
import { TypeRadio } from '../../../Wizard/TypeRadio';
export const RowGroupingSectionSummary = () => {
    const adaptable = useAdaptable();
    const { data: layout } = useOnePageAdaptableWizardContext();
    if (adaptable.api.gridApi.isTreeDataGrid()) {
        return (React.createElement(Box, { display: 'flex', flexDirection: 'row' },
            React.createElement(Tag, null, "Row Grouping is not available in Tree Grids")));
    }
    return (React.createElement(Box, { display: 'flex', flexDirection: 'row' }, layout.RowGroupedColumns?.length ? (layout.RowGroupedColumns.map((columnId) => {
        return (React.createElement(Box, { mb: 2, mr: 2 },
            React.createElement(Tag, { key: columnId }, adaptable.api.columnApi.getFriendlyNameForColumnId(columnId))));
    })) : (React.createElement(Tag, null, "No Row Grouping"))));
};
export const RowGroupingSection = (props) => {
    const adaptable = useAdaptable();
    if (adaptable.api.gridApi.isTreeDataGrid()) {
        return (React.createElement(Box, { display: 'flex', flexDirection: 'row' },
            React.createElement(Tag, null, "Row Grouping is not available in Tree Grids")));
    }
    const { data: layout } = useOnePageAdaptableWizardContext();
    const rowGroupsText = 'Row Group ' + adaptable.api.internalApi.getCorrectEnglishVariant('Behaviour');
    const allGroupableColumns = adaptable.api.columnApi.getGroupableColumns();
    const sortedGroupableColumns = React.useMemo(() => {
        return ArrayExtensions.sortArrayWithOrder(allGroupableColumns.map((col) => col.columnId), layout.RowGroupedColumns ?? [], { sortUnorderedItems: false }).map((colId) => adaptable.api.columnApi.getColumnWithColumnId(colId));
    }, [layout, allGroupableColumns]);
    const onChange = (layout) => {
        props.onChange(normalizeLayout(layout));
    };
    const handleColumnsChange = (columnIds) => {
        const newLayout = {
            ...layout,
            RowGroupedColumns: columnIds,
        };
        if (!newLayout.RowGroupValues) {
            newLayout.RowGroupValues = {
                RowGroupDefaultBehavior: 'always-collapsed',
            };
        }
        onChange(newLayout);
    };
    return (React.createElement(Box, { style: { height: '100%' } },
        React.createElement(Tabs, null,
            React.createElement(Tabs.Tab, null, "Group Display Type"),
            React.createElement(Tabs.Content, null,
                React.createElement(FormLayout, null,
                    React.createElement(RadioGroup, { orientation: "vertical", value: layout.RowGroupDisplayType ?? 'single', onRadioChange: (RowGroupDisplayType) => {
                            let newLayout = {
                                ...layout,
                                RowGroupDisplayType,
                            };
                            if (RowGroupDisplayType === 'single') {
                                newLayout.TableColumns = (newLayout.TableColumns || []).filter((colId) => !adaptable.api.columnApi.isAutoRowGroupColumnForMulti(colId));
                            }
                            else {
                                newLayout.TableColumns = (newLayout.TableColumns || []).filter((colId) => !adaptable.api.columnApi.isAutoRowGroupColumnForSingle(colId));
                            }
                            onChange(newLayout);
                        } },
                        React.createElement(TypeRadio, { value: "single", text: "Single Column", description: "All Row Grouped Columns display in one hierarchical Column" }),
                        React.createElement(TypeRadio, { value: "multi", text: "Multiple Columns", description: "Each Row Grouped Columns displays in its own, separate, Column" }))))),
        React.createElement(Tabs, { mt: 2 },
            React.createElement(Tabs.Tab, null, "Row Grouped Columns"),
            React.createElement(Tabs.Tab, null, rowGroupsText),
            React.createElement(Tabs.Content, null,
                React.createElement(ValueSelector, { showFilterInput: true, showSelectedOnlyPosition: "top", filter: columnFilter, toIdentifier: (option) => `${option.columnId}`, toLabel: (option) => option.friendlyName ?? option.columnId, options: sortedGroupableColumns, value: layout.RowGroupedColumns ?? [], allowReorder: true, xSelectedLabel: () => {
                        return `Selected Columns:`;
                    }, onChange: handleColumnsChange })),
            React.createElement(Tabs.Content, null,
                React.createElement(FormLayout, null,
                    React.createElement(RadioGroup, { orientation: "vertical", value: layout.RowGroupValues?.RowGroupDefaultBehavior ?? 'always-collapsed', onRadioChange: (RowGroupDefaultBehavior) => {
                            let newLayout = {
                                ...layout,
                                RowGroupValues: {
                                    RowGroupDefaultBehavior,
                                },
                            };
                            if (newLayout.RowGroupValues.RowGroupDefaultBehavior === 'collapsed' ||
                                newLayout.RowGroupValues.RowGroupDefaultBehavior === 'expanded') {
                                newLayout.RowGroupValues.ExceptionGroupKeys =
                                    [];
                            }
                            onChange(newLayout);
                        } },
                        React.createElement(TypeRadio, { value: "always-collapsed", text: "All Collapsed", description: "Always open Layout with all Grouped Rows collapsed" }),
                        React.createElement(TypeRadio, { value: "always-expanded", text: "All Expanded", description: "Always open Layout with all Grouped Rows expanded" }),
                        React.createElement(TypeRadio, { value: "expanded", text: "Mostly Expanded", description: "Layout opens with all Grouped rows expanded, other than those collapsed when Layout last displayed" }),
                        React.createElement(TypeRadio, { value: "collapsed", text: "Mostly Collapsed", description: "Layout opens with all Grouped rows collapsed, other than those expanded when Layout last displayed" })))))));
};
