"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilterSection = exports.FilterSectionSummary = exports.isColumnFiltersValid = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const ModuleConstants = tslib_1.__importStar(require("../../../../Utilities/Constants/ModuleConstants"));
const Tabs_1 = require("../../../../components/Tabs");
const AdaptableContext_1 = require("../../../AdaptableContext");
const AdaptableObjectList_1 = require("../../../Components/AdaptableObjectList/AdaptableObjectList");
const OnePageAdaptableWizard_1 = require("../../../Wizard/OnePageAdaptableWizard");
const getLayoutFilterViewItems_1 = require("../../../../Strategy/Utilities/Layout/getLayoutFilterViewItems");
const Tag_1 = require("../../../../components/Tag");
const PredicateEditor_1 = require("../../../Components/PredicateEditor/PredicateEditor");
const FormLayout_1 = tslib_1.__importStar(require("../../../../components/FormLayout"));
const ColumnSelector_1 = require("../../../Components/Selectors/ColumnSelector");
const isColumnFiltersValid = (layout) => {
    var _a, _b;
    const invalidColumnFilters = (_b = ((_a = layout.ColumnFilters) !== null && _a !== void 0 ? _a : [])) === null || _b === void 0 ? void 0 : _b.filter((columnFilter) => { var _a; return !((_a = columnFilter === null || columnFilter === void 0 ? void 0 : columnFilter.Predicate) === null || _a === void 0 ? void 0 : _a.PredicateId); });
    if (invalidColumnFilters.length > 0) {
        return 'Please select a Predicate for each Column Filter';
    }
    return true;
};
exports.isColumnFiltersValid = isColumnFiltersValid;
const FilterSectionSummary = () => {
    const { data: layout } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const fitlerViewItems = (0, getLayoutFilterViewItems_1.getLayoutFilterViewItems)(layout, adaptable.api);
    return (React.createElement(rebass_1.Box, null, fitlerViewItems.values.length ? (fitlerViewItems.values.map((value) => (React.createElement(Tag_1.Tag, { key: value, mb: 1, mr: 1 }, value)))) : (React.createElement(Tag_1.Tag, null, "No Column Filters"))));
};
exports.FilterSectionSummary = FilterSectionSummary;
const FilterSection = (props) => {
    var _a;
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const filterModule = adaptable.ModuleService.getModuleById(ModuleConstants.ColumnFilterModuleId);
    const { data: layout } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const layoutFilters = (_a = layout.ColumnFilters) !== null && _a !== void 0 ? _a : [];
    const handleDelete = React.useCallback((columnFilter) => {
        props.onChange(Object.assign(Object.assign({}, layout), { ColumnFilters: layoutFilters.filter((layoutFilter) => layoutFilter.ColumnId !== columnFilter.ColumnId) }));
    }, [layout]);
    const handlePredicateEdit = React.useCallback((predicate, columnFilter) => {
        props.onChange(Object.assign(Object.assign({}, layout), { ColumnFilters: layoutFilters.map((layoutFilter) => layoutFilter.ColumnId === columnFilter.ColumnId
                ? Object.assign(Object.assign({}, columnFilter), { Predicate: predicate }) : layoutFilter) }));
    }, [layout.ColumnFilters]);
    const columnsOptions = React.useMemo(() => adaptable.api.columnApi
        .getFilterableColumns()
        .filter((abColumn) => {
        var _a;
        return !((_a = layout.ColumnFilters) === null || _a === void 0 ? void 0 : _a.some((columnFilter) => abColumn.columnId === columnFilter.ColumnId));
    })
        .map(({ columnId }) => ({
        label: adaptable.api.columnApi.getFriendlyNameForColumnId(columnId),
        value: columnId,
    })), [layout, layoutFilters]);
    return (React.createElement(Tabs_1.Tabs, { style: { height: '100%' } },
        React.createElement(Tabs_1.Tabs.Tab, null, "Column Filters"),
        React.createElement(Tabs_1.Tabs.Content, null,
            React.createElement(FormLayout_1.default, { mb: 2 },
                React.createElement(FormLayout_1.FormRow, { label: "Add Column Filter" },
                    React.createElement(ColumnSelector_1.ColumnSelector, { filterColumn: (column) => column.filterable && !layoutFilters.some((f) => f.ColumnId === column.columnId), placeholder: "Select Column to create Filter", onChange: (option) => {
                            props.onChange(Object.assign(Object.assign({}, layout), { ColumnFilters: [
                                    ...layoutFilters,
                                    {
                                        ColumnId: option,
                                        Predicate: null,
                                    },
                                ] }));
                        } }))),
            (layoutFilters === null || layoutFilters === void 0 ? void 0 : layoutFilters.length) > 0 && (React.createElement(rebass_1.Box, { p: 0, as: "ul" }, layoutFilters.map((columnFilter) => {
                const predicateDefs = adaptable.api.columnFilterApi.getFilterPredicateDefsForColumnId(columnFilter.ColumnId);
                const items = [
                    ...filterModule.toView(columnFilter).items,
                    {
                        name: 'Column Filter',
                        view: (React.createElement(PredicateEditor_1.PredicateEditor, { columnId: columnFilter.ColumnId, predicate: columnFilter.Predicate, predicateDefs: predicateDefs, onChange: (predicate) => handlePredicateEdit(predicate, columnFilter), onClear: () => handlePredicateEdit(null, columnFilter) })),
                    },
                ];
                return (React.createElement(AdaptableObjectList_1.AdaptableObjectListItemView, { module: filterModule, abObject: columnFilter, entityType: "Column Filter", showActions: true, showEditButton: false, items: items, onDelete: () => handleDelete(columnFilter), key: columnFilter.ColumnId }));
            }))))));
};
exports.FilterSection = FilterSection;
