import * as React from 'react';
import { NewScopeComponent, renderScopeSummary } from '../../Components/NewScopeComponent';
import { useOnePageAdaptableWizardContext } from '../../Wizard/OnePageAdaptableWizard';
import { isScopeColumnIds } from '../../../AdaptableState/Common/ColumnScope';
export const renderFormatColumnScopeSummary = (data) => {
    return renderScopeSummary(data.Scope, {
        scopeWholeRow: 'Matching rows will be formatted',
        scopeColumns: 'Cells in selected columns will be formatted',
        scopeDataTypes: 'Cells in columns with these Data Types will be formatted',
    });
};
export const FormatColumnScopeWizardSection = (props) => {
    const { data, api } = useOnePageAdaptableWizardContext();
    return (React.createElement(React.Fragment, null,
        React.createElement(NewScopeComponent, { descriptions: {
                rowScope: 'Format all columns in the row',
                columnScope: 'Select columns to format',
            }, scope: data.Scope, updateScope: (Scope) => {
                const newData = { ...data, Scope };
                const wholeRow = api.columnScopeApi.scopeIsAll(Scope);
                if (newData.Rule?.Predicates?.length) {
                    if (wholeRow) {
                        // if scope is whole row, a predicate cannot be present, so we set the rule
                        // to be a boolean expression
                        delete newData.Rule.Predicates;
                        newData.Rule.BooleanExpression = '';
                    }
                    else {
                        const validPredicateIds = new Set(api.formatColumnApi.internalApi
                            .getFormatColumnDefsForScope(Scope)
                            .map((def) => def.id));
                        newData.Rule = {
                            Predicates: newData.Rule.Predicates.filter((p) => validPredicateIds.has(p.PredicateId)).filter((predicate) => {
                                // if there are more than 1 column, then we must eliminate the IN/NotIn predicates
                                // TODO: this should NOT be required, but the ColumnValueSelector does NOT support creatable values right now
                                if (isScopeColumnIds(Scope) && Scope.ColumnIds.length > 1) {
                                    return predicate.PredicateId !== 'In' && predicate.PredicateId !== 'NotIn';
                                }
                                return true;
                            }),
                        };
                    }
                }
                props.onChange(newData);
            } })));
};
