"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridInfoPopup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const Panel_1 = tslib_1.__importDefault(require("../../../components/Panel"));
const Tabs_1 = require("../../../components/Tabs");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../../Utilities/Extensions/ArrayExtensions"));
const Helper_1 = tslib_1.__importDefault(require("../../../Utilities/Helpers/Helper"));
const AdaptablePopover_1 = require("../../AdaptablePopover");
const AdaptableObjectCollection_1 = require("../../Components/AdaptableObjectCollection");
const AdaptableObjectRow_1 = require("../../Components/AdaptableObjectRow");
const PopupPanel_1 = require("../../Components/Popups/AdaptablePopup/PopupPanel");
const AdaptableObjectsSummary_1 = require("./AdaptableObjectsSummary");
const EnvVars_1 = require("../../../EnvVars");
const AgGridModulesAdapter_1 = require("../../../agGrid/AgGridModulesAdapter");
const adaptableVersion = EnvVars_1.ADAPTABLE_VERSION;
const agGridVersion = AgGridModulesAdapter_1.AG_GRID_VERSION;
const GridInfoPopup = (props) => {
    const api = props.api;
    const CreateGridSummaries = (colItems) => {
        const returnRows = [];
        const calcColumns = api.calculatedColumnApi
            .getCalculatedColumns()
            .map((c) => c.ColumnId);
        const actionColumns = api.actionColumnApi
            .getActionColumns()
            .map((ac) => ac.columnId);
        const freeTextColumns = api.freeTextColumnApi
            .getFreeTextColumns()
            .map((c) => c.ColumnId);
        const columnFilterDescription = api.filterApi.columnFilterApi.columnFiltersToString(api.filterApi.columnFilterApi.getColumnFilters());
        const gridFilterExpression = api.filterApi.gridFilterApi.getCurrentGridFilterExpression();
        const sorts = ArrayExtensions_1.default.IsNotNullOrEmpty(props.api.gridApi.getColumnSorts())
            ? api.gridApi.getColumnSorts().map((gs) => {
                return api.columnApi.getFriendlyNameForColumnId(gs.ColumnId) + ': ' + gs.SortOrder;
            })
            : null;
        const selectedRowInfo = api.gridApi.getSelectedRowInfo();
        if (api.optionsApi.getUserInterfaceOptions()?.showAdapTableVersion) {
            returnRows.push(createReadOnlyColItem(colItems, 'AdapTable Version', adaptableVersion));
        }
        if (api.optionsApi.getUserInterfaceOptions()?.showAgGridVersion) {
            returnRows.push(createReadOnlyColItem(colItems, 'AG Grid Version', agGridVersion));
        }
        returnRows.push(createReadOnlyColItem(colItems, 'Sorted Columns', ArrayExtensions_1.default.IsNotNullOrEmpty(sorts) ? sorts.join('; ') : 'None'));
        returnRows.push(createReadOnlyColItem(colItems, 'Column Filters', columnFilterDescription));
        returnRows.push(createReadOnlyColItem(colItems, 'Grid Filter', gridFilterExpression));
        returnRows.push(createReadOnlyColItem(colItems, 'All Rows', props.api.gridApi.getRowCount()));
        returnRows.push(createReadOnlyColItem(colItems, 'Visible Rows', props.api.gridApi.getVisibleRowCount()));
        returnRows.push(createReadOnlyColItem(colItems, 'Selected Rows', selectedRowInfo?.gridRows.length));
        returnRows.push(createReadOnlyColItem(colItems, 'Visible Selected Rows', selectedRowInfo?.gridRows.filter((gr) => gr.rowNode?.displayed == true).length));
        returnRows.push(createReadOnlyColItem(colItems, 'All Columns', props.api.gridApi.getColumnCount()));
        returnRows.push(createReadOnlyColItem(colItems, 'Visible Columns', props.api.gridApi.getVisibleColumnCount()));
        returnRows.push(createReadOnlyColItem(colItems, 'Calculated Columns', ArrayExtensions_1.default.IsNotNullOrEmpty(calcColumns)
            ? ArrayExtensions_1.default.createCommaSeparatedString(calcColumns)
            : 'None'));
        returnRows.push(createReadOnlyColItem(colItems, 'Free Text Columns', ArrayExtensions_1.default.IsNotNullOrEmpty(freeTextColumns)
            ? ArrayExtensions_1.default.createCommaSeparatedString(freeTextColumns)
            : 'None'));
        returnRows.push(createReadOnlyColItem(colItems, 'Action Columns', ArrayExtensions_1.default.IsNotNullOrEmpty(actionColumns)
            ? ArrayExtensions_1.default.createCommaSeparatedString(actionColumns)
            : 'None'));
        return returnRows;
    };
    const createReadOnlyColItem = (colItems, item1, item2, item3) => {
        let rowColItems = Helper_1.default.cloneObject(colItems);
        rowColItems[0].Content = item1;
        rowColItems[1].Content = item2;
        if (item3) {
            let infoButton = React.createElement(AdaptablePopover_1.AdaptablePopover, { headerText: null, bodyText: [item3] });
            rowColItems[2].Content = infoButton;
        }
        return rowColItems;
    };
    let propValueColItems = [
        { Content: 'Property', Size: 5 },
        { Content: 'Value', Size: 7 },
    ];
    let gridSummaries = CreateGridSummaries(propValueColItems).map((x, index) => {
        return React.createElement(AdaptableObjectRow_1.AdaptableObjectRow, { key: index, colItems: x });
    });
    const baseClassName = 'ab-GridInfo';
    return (React.createElement(PopupPanel_1.PopupPanel, { className: baseClassName, headerText: 'Grid Info', glyphicon: 'info' },
        React.createElement(rebass_1.Flex, { flexDirection: "column", flex: 1, height: "100%" },
            React.createElement(Panel_1.default, { flex: 1 },
                React.createElement(Tabs_1.Tabs, null,
                    React.createElement(Tabs_1.Tabs.Tab, null, "Grid Summary"),
                    React.createElement(Tabs_1.Tabs.Tab, null, "Grid State"),
                    React.createElement(Tabs_1.Tabs.Content, null,
                        React.createElement(AdaptableObjectCollection_1.AdaptableObjectCollection, { margin: 2, colItems: propValueColItems, items: gridSummaries })),
                    React.createElement(Tabs_1.Tabs.Content, null,
                        React.createElement(AdaptableObjectsSummary_1.AdaptableObjectsSummary, null)))))));
};
exports.GridInfoPopup = GridInfoPopup;
