"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RowEditService = void 0;
class RowEditService {
    constructor(adaptableApi) {
        this.adaptableApi = adaptableApi;
        if (adaptableApi.optionsApi.getActionRowOptions().autoHandle) {
            adaptableApi.eventApi.on('ActionRowSubmitted', (eventInfo) => this.autoHandleActionRowButtons(eventInfo));
        }
    }
    autoHandleActionRowButtons(actionRowSubmittedInfo) {
        const { adaptableApi } = actionRowSubmittedInfo;
        if (actionRowSubmittedInfo.type === 'rowEdited') {
            const { rowNode, formData } = actionRowSubmittedInfo;
            const newRowData = Object.assign(Object.assign({}, rowNode.data), formData);
            adaptableApi.gridApi.updateGridData([newRowData]);
        }
        if (actionRowSubmittedInfo.type === 'rowDeleted') {
            const { rowNode } = actionRowSubmittedInfo;
            const deletedRowData = rowNode.data;
            adaptableApi.gridApi.deleteGridData([deletedRowData]);
        }
        if (actionRowSubmittedInfo.type === 'rowCreated') {
            const { formData } = actionRowSubmittedInfo;
            const setPrimaryKeyValueFunction = adaptableApi.optionsApi.getActionRowOptions().setPrimaryKeyValue;
            const newRowData = typeof setPrimaryKeyValueFunction === 'function'
                ? setPrimaryKeyValueFunction(Object.assign({ rowData: formData }, adaptableApi.internalApi.buildBaseContext()))
                : Object.assign({}, formData);
            const config = {
                addIndex: 0,
            };
            adaptableApi.gridApi.addGridData([newRowData], config);
        }
    }
    destroy() {
        // nothing yet
    }
}
exports.RowEditService = RowEditService;
