export class RowFormService {
    adaptableApi;
    constructor(adaptableApi) {
        this.adaptableApi = adaptableApi;
        if (adaptableApi.optionsApi.getRowFormOptions().autoHandle) {
            adaptableApi.eventApi.on('RowFormSubmitted', (eventInfo) => this.autoHandleRowFormButtons(eventInfo));
        }
    }
    autoHandleRowFormButtons(rowFormSubmittedInfo) {
        const { adaptableApi } = rowFormSubmittedInfo;
        if (rowFormSubmittedInfo.type === 'rowEdited') {
            const { rowNode, formData } = rowFormSubmittedInfo;
            const newRowData = {
                ...rowNode.data,
                ...formData,
            };
            adaptableApi.gridApi.updateGridData([newRowData]);
        }
        if (rowFormSubmittedInfo.type === 'rowDeleted') {
            const { rowNode } = rowFormSubmittedInfo;
            const deletedRowData = rowNode.data;
            adaptableApi.gridApi.deleteGridData([deletedRowData]);
        }
        if (rowFormSubmittedInfo.type === 'rowCreated') {
            const { formData } = rowFormSubmittedInfo;
            let newRowData = formData;
            const setPrimaryKeyValueFunction = adaptableApi.optionsApi.getRowFormOptions().setPrimaryKeyValue;
            if (setPrimaryKeyValueFunction) {
                const context = {
                    rowData: formData,
                    ...adaptableApi.internalApi.buildBaseContext(),
                };
                const pkValue = setPrimaryKeyValueFunction(context);
                const primaryKeyColumn = this.adaptableApi.columnApi.getPrimaryKeyColumn();
                newRowData[primaryKeyColumn.columnId] = pkValue;
            }
            const config = {
                addIndex: 0,
            };
            adaptableApi.gridApi.addGridData([newRowData], config);
        }
    }
    destroy() {
        // nothing yet
    }
}
