"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnInfo = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const FormLayout_1 = tslib_1.__importStar(require("../../components/FormLayout"));
const InfiniteTable_1 = require("../../components/InfiniteTable");
const Panel_1 = tslib_1.__importDefault(require("../../components/Panel"));
const Tabs_1 = require("../../components/Tabs");
const AdaptableContext_1 = require("../AdaptableContext");
const AdaptableObjectList_1 = require("../Components/AdaptableObjectList");
const ButtonNew_1 = require("../Components/Buttons/ButtonNew");
const ColumnSelector_1 = require("../Components/Selectors/ColumnSelector");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const Flex_1 = require("../../components/Flex");
const BASE_CLASS_NAME = 'ab-AdaptableColumns';
const domProps = {
    className: 'twa:flex-1 twa:min-h-0 twa:w-full',
};
// TODOS:
// - use column from props as initial value
// - table summary
// - access level for edit/new buttons
const MODULES_WITH_COLUMN = [
    'Alert', // Scope
    'CalculatedColumn', // ColumnId
    'CustomSort', // ColumnId
    'FormatColumn', // Scope
    'FlashingCell',
    'PlusMinus', // Scope
    'Shortcut', // Scope
    'StyledColumn', // Scope
];
const MODULES_WITH_SCOPE = [
    'Alert',
    'FormatColumn',
    'PlusMinus',
    'Shortcut',
    'StyledColumn',
];
const ColumnDefPreview = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    if (props.column) {
        const api = adaptable.api;
        const summary = api.columnApi.getColumnSummaryForColumnId(props.column.columnId);
        const keyValuePairs = [
            {
                Key: 'Column Id',
                Value: props.column.columnId,
            },
            {
                Key: 'Header',
                Value: props.column.friendlyName,
            },
            {
                Key: 'Data Type',
                Value: props.column.dataType,
            },
            {
                Key: 'Column Types',
                Value: ArrayExtensions_1.default.createCommaSeparatedString(props.column.columnTypes),
            },
            {
                Key: 'Primary Key Column',
                Value: props.column.isPrimaryKey ? 'Yes' : 'Ignore',
            },
            {
                Key: 'Calculated Column',
                Value: props.column.isCalculatedColumn ? 'Yes' : 'Ignore',
            },
            {
                Key: 'Free Text Column',
                Value: props.column.isFreeTextColumn ? 'Yes' : 'Ignore',
            },
            {
                Key: 'Action Column',
                Value: props.column.isActionColumn ? 'Yes' : 'Ignore',
            },
            {
                Key: 'Sparkline Column',
                Value: props.column.isSparkline ? 'Yes' : 'Ignore',
            },
            {
                Key: 'Column Group',
                Value: props.column.columnGroup ? props.column.columnGroup.friendlyName : 'Ignore',
            },
            {
                Key: 'Visible',
                Value: props.column.visible ? 'Yes' : 'No',
            },
            {
                Key: 'Width',
                Value: props.column.width,
            },
            {
                Key: 'Row Grouped',
                Value: props.column.groupable ? (props.column.isGrouped ? 'Yes' : 'No') : 'Ignore',
            },
            {
                Key: 'Groupable',
                Value: props.column.groupable ? 'Yes' : 'No',
            },
            {
                Key: 'Aggregatable',
                Value: props.column.aggregatable ? 'Yes' : 'No',
            },
            {
                Key: 'Aggregation Function',
                Value: props.column.aggregatable ? props.column.aggregationFunction : 'Ignore',
            },
            {
                Key: 'Filterable',
                Value: props.column.filterable ? 'Yes' : 'No',
            },
            {
                Key: 'Column Filter',
                Value: props.column.filterable
                    ? api.filterApi.columnFilterApi.columnFilterToString(summary.columnFilter)
                    : 'Ignore',
            },
            {
                Key: 'Hideable',
                Value: props.column.hideable ? 'Yes' : 'No',
            },
            {
                Key: 'Editable',
                Value: !props.column.IsReadOnly ? 'Yes' : 'No',
            },
            {
                Key: 'Moveable',
                Value: props.column.moveable ? 'Yes' : 'No',
            },
            {
                Key: 'Pivotable',
                Value: props.column.pivotable ? 'Yes' : 'No',
            },
            {
                Key: 'Queryable',
                Value: props.column.queryable ? 'Yes' : 'No',
            },
            {
                Key: 'Exportable',
                Value: props.column.exportable ? 'Yes' : 'No',
            },
            {
                Key: 'Sortable',
                Value: props.column.sortable ? 'Yes' : 'No',
            },
            {
                Key: 'No. Distinct Values',
                Value: summary.uniqueGridCells.length,
            },
        ]
            .map((keyValuePair) => {
            return {
                Key: keyValuePair.Key,
                Value: keyValuePair.Value === 'Ignore' ? null : keyValuePair.Value,
            };
        })
            .filter((x) => x.Value);
        const columnsMap = {
            Key: { field: 'Key', header: 'Property', defaultFlex: 1 },
            Value: { field: 'Value', header: 'Value', defaultFlex: 3 },
        };
        return (React.createElement(InfiniteTable_1.DataSource, { data: keyValuePairs, primaryKey: "Key" },
            React.createElement(InfiniteTable_1.InfiniteTableGrid, { domProps: domProps, columns: columnsMap })));
    }
};
const ModuleView = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const module = adaptable.ModuleService.getModuleById(props.moduleName);
    if (!module?.isModuleVisible()) {
        return null;
    }
    const moduleInfo = module.moduleInfo;
    const [isWizardOpen, setIsWizardOpen] = React.useState(false);
    const EditWizard = module?.getViewProperties?.()?.getEditWizard?.();
    const column = adaptable.api.columnApi.getColumnWithColumnId(props.selectedColumnId);
    const allItems = module?.toViewAll?.();
    // Dont show calculated column for non calc cols
    if (module.moduleInfo.ModuleName == 'CalculatedColumn' && !column.isCalculatedColumn) {
        return React.createElement(React.Fragment, null);
    }
    // if (MODULES_WITH_VIEW_ONLY.includes(props.moduleName) && !allItems?.length) {
    //   return <></>;
    // }
    const itemsInScope = allItems.filter((item) => {
        if ('Scope' in item.abObject) {
            return adaptable.api.columnScopeApi.isColumnInScope(column, item.abObject.Scope);
        }
        if ('ColumnId' in item.abObject) {
            return item.abObject?.ColumnId === props.selectedColumnId;
        }
        return false;
    });
    if (!itemsInScope) {
        return null;
    }
    const handleWizardClose = React.useCallback(() => {
        setIsWizardOpen(false);
    }, []);
    // filter out items with scope
    return (React.createElement(Flex_1.Box, { "data-name": module.moduleInfo.ModuleName, className: `${BASE_CLASS_NAME}__module-list twa:mb-4`, key: module.moduleInfo.ModuleName },
        React.createElement(Flex_1.Flex, null,
            React.createElement("b", { className: `${BASE_CLASS_NAME}__header` }, module.moduleInfo.FriendlyName),
            React.createElement(Flex_1.Box, { className: "twa:flex-1" }),
            React.createElement(ButtonNew_1.ButtonNew, { onClick: () => setIsWizardOpen(true) }, "New")),
        itemsInScope?.length ? React.createElement(AdaptableObjectList_1.AdaptableObjectList, { items: itemsInScope, module: module }) : null,
        isWizardOpen && EditWizard && (React.createElement(EditWizard, { popupParams: {
                column,
                action: 'New',
                source: 'ModuleButton',
            }, moduleInfo: moduleInfo, data: null, configEntities: null, onCloseWizard: handleWizardClose, onFinishWizard: handleWizardClose }))));
};
const ColumnInfo = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const [selectedColumnId, setSelectedColumnId] = React.useState(props.selectedColumnId);
    const column = adaptable.api.columnApi.getColumnWithColumnId(selectedColumnId);
    return (React.createElement(Panel_1.default, { className: "twa:flex-1 twa:border-none twa:shadow-md twa:overflow-hidden" },
        React.createElement(Panel_1.default.FlexBody, { className: "twa:gap-3" },
            React.createElement(FormLayout_1.default, null,
                React.createElement(FormLayout_1.FormRow, { label: "Select Column" },
                    React.createElement(ColumnSelector_1.ColumnSelector, { value: selectedColumnId, onChange: (columnId) => setSelectedColumnId(columnId) }))),
            selectedColumnId && (React.createElement(Tabs_1.Tabs, { className: "twa:min-h-0 twa:flex-1" },
                React.createElement(Tabs_1.Tabs.Tab, null, "Column Summary"),
                React.createElement(Tabs_1.Tabs.Tab, null, "Column State"),
                React.createElement(Tabs_1.Tabs.Content, null, selectedColumnId && React.createElement(ColumnDefPreview, { column: column })),
                React.createElement(Tabs_1.Tabs.Content, null, selectedColumnId && (React.createElement(Flex_1.Box, { className: BASE_CLASS_NAME }, MODULES_WITH_COLUMN.map((moduleName) => {
                    return (React.createElement(ModuleView, { key: moduleName, moduleName: moduleName, selectedColumnId: selectedColumnId }));
                })))))))));
};
exports.ColumnInfo = ColumnInfo;
