"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormatColumnModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const FormatColumnRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/FormatColumnRedux"));
const PopupRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/PopupRedux"));
const FormatColumnWizard_1 = require("../View/FormatColumn/Wizard/FormatColumnWizard");
const FormatColumnFormatWizardSection_1 = require("../View/FormatColumn/Wizard/FormatColumnFormatWizardSection");
const getFormatColumnSettingsViewItems_1 = require("./Utilities/FormatColumn/getFormatColumnSettingsViewItems");
const getFormatColumnStyleViewItems_1 = require("./Utilities/FormatColumn/getFormatColumnStyleViewItems");
const MoveFormatColumn_1 = require("../View/FormatColumn/MoveFormatColumn");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
const getRuleViewItems_1 = require("../Utilities/getRuleViewItems");
const getScopeViewItems_1 = require("../Utilities/getScopeViewItems");
const getFormatColumnSettingsTargetItems_1 = require("./Utilities/FormatColumn/getFormatColumnSettingsTargetItems");
class FormatColumnModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.FormatColumnModuleId, ModuleConstants.FormatColumnFriendlyName, 'color-palette', 'FormatColumnPopup', 'Create a column style, display format or cell alignment', api);
    }
    getModuleAdaptableObjects(config) {
        return this.api.formatColumnApi.getFormatColumns(config);
    }
    getExplicitlyReferencedColumnIds(formatColumn) {
        const queryExpression = formatColumn.Rule && this.api.expressionApi.getAdaptableQueryExpression(formatColumn.Rule);
        if (queryExpression) {
            return this.api.expressionApi.getColumnsFromExpression(queryExpression);
        }
        else if (this.api.columnScopeApi.scopeHasColumns(formatColumn.Scope)) {
            return this.api.columnScopeApi
                .getColumnsInScope(formatColumn.Scope)
                .map((adaptableColumn) => adaptableColumn.columnId);
        }
        return [];
    }
    getReferencedNamedQueryNames(formatColumn) {
        const queryExpression = formatColumn.Rule && this.api.expressionApi.getAdaptableQueryExpression(formatColumn.Rule);
        if (!queryExpression) {
            return [];
        }
        return this.api.namedQueryApi.internalApi.getReferencedNamedQueryNames(queryExpression);
    }
    createColumnMenuItems(column) {
        if (!column || !this.isModuleEditable() || column.isSparkline) {
            return;
        }
        let returnColumnMenuItems = [];
        let createPopupParam = {
            column: column,
            action: 'New',
            source: 'ColumnMenu',
        };
        returnColumnMenuItems.push(this.createMenuItemShowPopup('format-column-add', 'Create Format Column', this.moduleInfo.Popup, this.moduleInfo.Glyph, createPopupParam));
        const formatColumnsForColumn = this.api.formatColumnApi.getFormatColumnsForColumnId(column.columnId);
        if (formatColumnsForColumn.length === 1) {
            let editPopupParam = {
                column: column,
                action: 'Edit',
                value: formatColumnsForColumn[0],
                source: 'ColumnMenu',
            };
            const editFormatColumnMenuItem = this.createMenuItemShowPopup('format-column-edit', 'Edit Format Column', this.moduleInfo.Popup, 'edit', editPopupParam);
            returnColumnMenuItems.push(editFormatColumnMenuItem);
        }
        else if (formatColumnsForColumn.length > 1) {
            // if more than one then display a Create and a Show
            // we should probably do the Show better - perhaps use Column Info in some way?
            // or to filter the format columns ?
            const moduleInfo = this.api.internalApi
                .getModuleService()
                .getModuleInfoByModule('FormatColumn');
            const editFormatColumnMenuItem = this.createMenuItemReduxAction('format-column-edit', 'Show Current', this.moduleInfo.Glyph, PopupRedux.PopupShowScreen('FormatColumn', moduleInfo.Popup));
            returnColumnMenuItems.push(editFormatColumnMenuItem);
        }
        return returnColumnMenuItems;
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.formatColumnApi.getFormatColumns(),
            AddAction: FormatColumnRedux.FormatColumnAdd,
            EditAction: FormatColumnRedux.FormatColumnEdit,
        };
    }
    toView(formatColumn) {
        return {
            abObject: formatColumn,
            items: [
                (0, getScopeViewItems_1.getScopeViewItems)(formatColumn.Scope, this.api),
                (0, getFormatColumnSettingsTargetItems_1.getFormatColumnSettingsTargetItems)(formatColumn),
                formatColumn.Rule && (0, getRuleViewItems_1.getRuleViewItems)(formatColumn.Rule, this.api),
                {
                    name: 'Style',
                    view: () => (0, getFormatColumnStyleViewItems_1.getFormatColumnStyleViewItems)(formatColumn, this.api),
                },
                {
                    name: 'Display Format',
                    values: [(0, FormatColumnFormatWizardSection_1.getFormatColumnFormatSummaryValue)(formatColumn)],
                },
                (0, getFormatColumnSettingsViewItems_1.getFormatColumnSettingsViewItems)(formatColumn),
                (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(formatColumn, this.api),
            ].filter(Boolean),
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects({
            includeLayoutNotExtendedObjects: this.showLayoutNonExtendedObjects(),
        }).map((formatColumn) => this.toView(formatColumn));
    }
    getViewProperties() {
        return {
            actions: [MoveFormatColumn_1.MoveFormatColumn],
            getEditAction: FormatColumnRedux.FormatColumnEdit,
            getDeleteAction: FormatColumnRedux.FormatColumnDelete,
            getSuspendAction: FormatColumnRedux.FormatColumnSuspend,
            getUnSuspendAction: FormatColumnRedux.FormatColumnUnSuspend,
            getSuspendAllAction: FormatColumnRedux.FormatColumnSuspendAll,
            getUnSuspendAllAction: FormatColumnRedux.FormatColumnUnSuspendAll,
            getEditWizard() {
                return FormatColumnWizard_1.FormatColumnWizard;
            },
        };
    }
    containsLayoutExtensions() {
        return true;
    }
}
exports.FormatColumnModule = FormatColumnModule;
