"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewScopeComponent = exports.renderScopeSummary = exports.isScopeValid = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const rebass_1 = require("rebass");
const Radio_1 = tslib_1.__importDefault(require("../../components/Radio"));
const CheckBox_1 = require("../../components/CheckBox");
const Tabs_1 = require("../../components/Tabs");
const OnePageAdaptableWizard_1 = require("../Wizard/OnePageAdaptableWizard");
const ColumnSelector_1 = require("./ColumnSelector");
const AdaptableFormControlTextClear_1 = require("./Forms/AdaptableFormControlTextClear");
const AdaptableContext_1 = require("../AdaptableContext");
const ValueSelector_1 = require("./ValueSelector");
const AdaptableColumn_1 = require("../../AdaptableState/Common/AdaptableColumn");
const isScopeValid = ({ Scope }) => {
    const result = [];
    if (!Scope) {
        return 'No scope specified or scope is empty';
    }
    if (Scope && 'ColumnIds' in Scope && Scope.ColumnIds.length === 0) {
        result.push('No Columns selected in Scope');
    }
    if (Scope && 'DataTypes' in Scope && Scope.DataTypes.length === 0) {
        result.push('No Data Types selected in Scope');
    }
    return result.length ? result.join(', ') : true;
};
exports.isScopeValid = isScopeValid;
const DATA_TYPES_MAP = {
    Date: {
        label: 'Date',
        value: 'date',
    },
    Number: {
        label: 'Number',
        value: 'number',
    },
    String: {
        label: 'Text',
        value: 'text',
    },
    Boolean: {
        label: 'Boolean',
        value: 'boolean',
    },
};
const renderScopeSummary = (scope, labels) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const scopeApi = adaptable.api.columnScopeApi;
    const columnsInScope = scopeApi.getColumnsInScope(scope);
    return (React.createElement(React.Fragment, null,
        React.createElement(rebass_1.Box, null,
            React.createElement(OnePageAdaptableWizard_1.SummaryText, { mb: 0 }, scopeApi.scopeIsAll(scope) ? labels.scopeWholeRow : null)),
        React.createElement(rebass_1.Box, { style: { overflow: 'hidden' } }, 'ColumnIds' in scope ? (React.createElement(React.Fragment, null,
            React.createElement(OnePageAdaptableWizard_1.SummaryText, { mb: columnsInScope.length ? 2 : 0 }, labels.scopeColumns),
            columnsInScope.length ? (React.createElement(ValueSelector_1.ValueOptionsTags, { options: columnsInScope, value: columnsInScope.map((c) => c.columnId), toLabel: (c) => c.friendlyName, allowWrap: true, readOnly: true, renderLabel: (c) => React.createElement(OnePageAdaptableWizard_1.SummaryText, { mb: 0 }, c), toIdentifier: (c) => c.columnId })) : null)) : null),
        React.createElement(rebass_1.Box, null, 'DataTypes' in scope ? (React.createElement(React.Fragment, null,
            React.createElement(rebass_1.Text, { fontSize: 2, mb: 2 }, labels.scopeDataTypes),
            React.createElement(ValueSelector_1.ValueOptionsTags, { readOnly: true, options: scope.DataTypes, value: scope.DataTypes, toLabel: (c) => c, allowWrap: true, renderLabel: (c) => React.createElement(rebass_1.Text, { fontSize: 2 }, `DataType: ${c}`), toIdentifier: (c) => c }))) : null)));
};
exports.renderScopeSummary = renderScopeSummary;
const DATA_TYPES_OPTIONS = Object.values(DATA_TYPES_MAP);
const NewScopeComponent = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const { columnScopeApi: scopeApi, columnApi } = api;
    const [columnsSearchText, setColumnsSearchText] = (0, react_1.useState)('');
    const scopeColumns = React.useMemo(() => {
        const allColumns = props.scopeColumns || columnApi.getUIAvailableColumns();
        if (typeof props.isColumnAvailable === 'function') {
            return allColumns.filter((c) => props.isColumnAvailable(c));
        }
        return allColumns;
    }, []);
    const getScopeChoice = (scope) => {
        if (!scope) {
            return undefined;
        }
        if (scopeApi.scopeIsAll(scope)) {
            return 'All';
        }
        if (scopeApi.scopeHasColumns(props.scope)) {
            return 'Column';
        }
        if (scopeApi.scopeHasDataType(props.scope)) {
            return 'DataType';
        }
        if (scopeApi.scopeHasColumnType(props.scope)) {
            return 'ColumnType';
        }
        return undefined;
    };
    const onScopeSelectChanged = (value) => {
        let newScope;
        if (value == 'Column') {
            newScope = {
                ColumnIds: [],
            };
        }
        else if (value == 'DataType') {
            newScope = {
                DataTypes: [],
            };
        }
        else if (value == 'ColumnType') {
            newScope = {
                ColumnTypes: [],
            };
        }
        else {
            newScope = {
                All: true,
            };
        }
        props.updateScope(newScope);
    };
    const onColumnsSelectedChanged = (cols) => {
        const newScope = {
            ColumnIds: cols,
        };
        props.updateScope(newScope);
    };
    const onCheckBoxDataTypeChecked = (checked, item) => {
        let dataTypes = [].concat(scopeApi.getDataTypesInScope(props.scope));
        if (checked) {
            dataTypes.push(item);
        }
        else {
            const index = dataTypes.indexOf(item, 0);
            if (index > -1) {
                dataTypes.splice(index, 1);
            }
        }
        let newScope = {
            DataTypes: dataTypes,
        };
        props.updateScope(newScope);
    };
    const scopeChoice = getScopeChoice(props.scope);
    const dataTypesInScope = props.scope && 'DataTypes' in props.scope ? scopeApi.getDataTypesInScope(props.scope) : null;
    let dataTypeOptions = DATA_TYPES_OPTIONS;
    if (Array.isArray(props.availableDataTypes)) {
        dataTypeOptions = props.availableDataTypes.map((dataType) => DATA_TYPES_MAP[dataType]);
    }
    const hasColumnTypes = React.useMemo(() => {
        return api.columnApi.getColumnTypes()?.length > 0;
    }, []);
    return (React.createElement(Tabs_1.Tabs, { "data-name": 'scope-component', className: "ab-ScopeComponent", value: scopeChoice, style: { height: '100%', ...props.style }, onValueChange: onScopeSelectChanged },
        props.hideWholeRow ? null : (React.createElement(Tabs_1.Tabs.Tab, { value: "Row" },
            React.createElement(Radio_1.default, { margin: 0, checked: scopeChoice == 'All', tabIndex: -1 }, "All Columns"))),
        !props.disableColumns && (React.createElement(Tabs_1.Tabs.Tab, { value: "Column" },
            React.createElement(Radio_1.default, { margin: 0, value: "Column", checked: scopeChoice == 'Column', tabIndex: -1 }, "Selected Columns"))),
        !props.disableDataTypes && (React.createElement(Tabs_1.Tabs.Tab, { value: "DataType" },
            React.createElement(Radio_1.default, { margin: 0, value: "DataType", checked: scopeChoice == 'DataType', tabIndex: -1 }, "Data Types"))),
        hasColumnTypes && (React.createElement(Tabs_1.Tabs.Tab, { value: "ColumnType" },
            React.createElement(Radio_1.default, { margin: 0, value: "ColumnType", checked: scopeChoice == 'ColumnType', tabIndex: -1 }, "Column Types"))),
        props.hideWholeRow ? null : (React.createElement(Tabs_1.Tabs.Content, { value: "Row", style: { flex: 'none' }, "data-name": "row-scope" },
            React.createElement(rebass_1.Text, { padding: 2, pl: 0, fontSize: 2 }, props.descriptions.rowScope))),
        !props.disableColumns && (React.createElement(Tabs_1.Tabs.Content, { value: "Column", style: { flex: 1, overflow: 'auto' }, "data-name": "column-scope", padding: 0 },
            React.createElement(rebass_1.Flex, { flexDirection: "row", alignItems: "center", padding: 2 },
                React.createElement(rebass_1.Text, { fontSize: 2 }, props.descriptions.columnScope),
                React.createElement(rebass_1.Flex, { flex: 3 }),
                React.createElement(AdaptableFormControlTextClear_1.AdaptableFormControlTextClear, { value: columnsSearchText, OnTextChange: setColumnsSearchText, placeholder: "Type to search columns", style: { flex: 1 } })),
            React.createElement(rebass_1.Flex, { paddingLeft: 2, flex: 1, style: { overflow: 'hidden' } },
                React.createElement(ColumnSelector_1.NewColumnSelector, { columnFilterText: columnsSearchText, allowReorder: false, availableColumns: scopeColumns, selected: scopeApi.getColumnIdsInScope(props.scope), onChange: onColumnsSelectedChanged })))),
        !props.disableDataTypes && (React.createElement(Tabs_1.Tabs.Content, { value: "DataType", style: { flex: 'none' }, "data-name": "datatype-scope" },
            React.createElement(rebass_1.Box, null,
                props.descriptions.dataTypeScope && (React.createElement(rebass_1.Text, { p: 2, pl: 0, mb: 2, fontSize: 2 }, props.descriptions.dataTypeScope)),
                React.createElement(rebass_1.Flex, { flexDirection: "column" }, dataTypeOptions.map((dataTypeOption) => (React.createElement(CheckBox_1.CheckBox, { "data-name": "scope", "data-value": dataTypeOption.value, key: dataTypeOption.value, checked: dataTypesInScope && dataTypesInScope.includes(dataTypeOption.value), onChange: (checked) => onCheckBoxDataTypeChecked(checked, dataTypeOption.value) }, dataTypeOption.label))))))),
        hasColumnTypes && (React.createElement(Tabs_1.Tabs.Content, { value: "ColumnType", style: { flex: 'none' }, "data-name": "column-type-scope" },
            React.createElement(rebass_1.Box, null,
                React.createElement(rebass_1.Flex, { flexDirection: "column" }, api.columnApi.getColumnTypes()?.map?.((columnType) => (React.createElement(CheckBox_1.CheckBox, { "data-name": "scope", "data-value": columnType, key: columnType, checked: 'ColumnTypes' in props.scope && props.scope.ColumnTypes?.includes(columnType), onChange: (checked) => {
                        let columnTypes = [].concat(props.scope.ColumnTypes);
                        if (checked) {
                            columnTypes.push(columnType);
                        }
                        else {
                            columnTypes = columnTypes.filter((ct) => ct !== columnType);
                        }
                        let newScope = {
                            ColumnTypes: columnTypes,
                        };
                        props.updateScope(newScope);
                    } }, (0, AdaptableColumn_1.getColumnTypeFriendlyName)(columnType))))))))));
};
exports.NewScopeComponent = NewScopeComponent;
