"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionColumnInternalApi = void 0;
const ApiBase_1 = require("../Implementation/ApiBase");
const Uuid_1 = require("../../AdaptableState/Uuid");
const ActionColumnRenderer_1 = require("../../agGrid/cellRenderers/ActionColumnRenderer");
const AdaptableColumn_1 = require("../../AdaptableState/Common/AdaptableColumn");
class ActionColumnInternalApi extends ApiBase_1.ApiBase {
    getColDefsForActionColumns() {
        const defaultSpecialColumnSettings = this.getGridApi().internalApi.deriveSpecialColumnSettingsFromAgGridDefaultColDef();
        const defaultActionColumnSettings = {
            ...defaultSpecialColumnSettings,
            resizable: true,
            suppressMenu: false,
            suppressMovable: false,
        };
        return this.getActionColumnApi()
            .getActionColumns()
            .map((actionColumn) => {
            const actionColumnSettings = {
                ...defaultActionColumnSettings,
                ...actionColumn.actionColumnSettings,
            };
            const newColDef = {
                headerName: actionColumn.friendlyName ? actionColumn.friendlyName : actionColumn.columnId,
                colId: actionColumn.columnId,
                hide: false,
                editable: false,
                width: actionColumnSettings.width,
                resizable: actionColumnSettings.resizable,
                suppressHeaderMenuButton: actionColumnSettings.suppressMenu,
                suppressMovable: actionColumnSettings.suppressMovable,
                filter: false,
                sortable: false,
                enableRowGroup: false,
                cellRenderer: this.getGridApi().getVariant() === 'react'
                    ? ActionColumnRenderer_1.ReactActionColumnRenderer
                    : ActionColumnRenderer_1.ActionColumnRenderer,
                type: [AdaptableColumn_1.ACTION_COLUMN_TYPE],
            };
            this.logInfo('Setting up Action Column: ' + actionColumn.columnId);
            return newColDef;
        });
    }
    getActionColumnsAndButtons(colDef) {
        let actionColumn;
        // this should probably be in FDC3 api...
        if (!actionColumn && this.getFdc3Api().internalApi.isFdc3MainActionColumn(colDef.colId)) {
            const actionButtons = this.getFdc3Api().internalApi.getButtonsForFdc3MainActionColumn();
            const actionColumnDefaultConfiguration = this.getFdc3Options().actionColumnDefaultConfiguration;
            actionColumn = {
                columnId: actionColumnDefaultConfiguration.columnId,
                actionColumnButton: actionButtons,
                rowScope: actionColumnDefaultConfiguration.rowScope,
            };
        }
        if (!actionColumn && this.getFdc3Api().internalApi.isFdc3StandaloneActionColumn(colDef.colId)) {
            const actionButtons = this.getFdc3Api().internalApi.getButtonsForFdc3StandaloneActionColumn(colDef.colId);
            const actionColumnDefaultConfiguration = this.getFdc3Options().actionColumnDefaultConfiguration;
            actionColumn = {
                columnId: colDef.colId,
                actionColumnButton: actionButtons,
                rowScope: actionColumnDefaultConfiguration.rowScope,
            };
        }
        if (!actionColumn) {
            actionColumn = this.getActionColumnApi()
                .getActionColumns()
                ?.find((ac) => ac.columnId == colDef.colId);
        }
        if (!actionColumn?.actionColumnButton) {
            return {
                actionButtons: [],
                actionColumn,
            };
        }
        let actionButtons = Array.isArray(actionColumn.actionColumnButton)
            ? actionColumn.actionColumnButton
            : [actionColumn.actionColumnButton];
        actionButtons = this.updateAllActionColumnButtons(actionButtons);
        return { actionButtons, actionColumn };
    }
    updateAllActionColumnButtons(actionButtons) {
        return actionButtons.map((actionButton) => {
            actionButton = { ...actionButton };
            actionButton.Uuid = (0, Uuid_1.createUuid)();
            if (actionButton.command) {
                this.updateActionButtonCommand(actionButton);
            }
            return actionButton;
        });
    }
    updateActionButtonCommand(button) {
        switch (button.command) {
            case 'create':
                button.onClick = () => {
                    this.getRowFormApi().displayCreateRowForm();
                };
                button.tooltip = button.tooltip ? button.tooltip : 'Create Row';
                button.icon = button.icon ?? {
                    name: 'add',
                };
                break;
            case 'clone':
                button.onClick = (button, context) => {
                    this.getRowFormApi().displayCloneRowForm(context.primaryKeyValue);
                };
                button.tooltip = button.tooltip ? button.tooltip : 'Clone Row';
                button.icon = button.icon ?? {
                    name: 'clone',
                };
                break;
            case 'delete':
                button.onClick = (button, context) => {
                    this.getRowFormApi().displayDeleteRowForm(context.primaryKeyValue);
                };
                button.tooltip = button.tooltip ? button.tooltip : 'Delete Row';
                button.icon = button.icon ?? { name: 'delete' };
                break;
            case 'edit':
                button.onClick = (button, context) => {
                    this.getRowFormApi().displayEditRowForm(context.primaryKeyValue);
                };
                button.tooltip = button.tooltip ? button.tooltip : 'Edit Row';
                button.icon = button.icon ?? {
                    name: 'edit',
                };
                break;
        }
    }
}
exports.ActionColumnInternalApi = ActionColumnInternalApi;
