import { ApiBase } from './ApiBase';
import { RowFormInternalApi } from '../Internal/RowFormInternalApi';
import { PopupShowForm } from '../../Redux/ActionsReducers/PopupRedux';
export class RowFormApiImpl extends ApiBase {
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new RowFormInternalApi(_adaptable);
    }
    async displayEditRowForm(primaryKey) {
        const rowNode = this.getGridApi().getRowNodeForPrimaryKey(primaryKey);
        if (!rowNode) {
            this.logWarn(`Cannot edit row: rowNode not found for primaryKey ${primaryKey}`);
            return;
        }
        const editForm = await this.internalApi.buildRowEditForm(rowNode);
        this.dispatchAction(PopupShowForm({
            Id: 'edit_row_form',
            Form: editForm,
            // formProps are added to the formContext
            FormProps: {
                rowNode,
            },
        }));
    }
    async displayCreateRowForm() {
        const createForm = await this.internalApi.buildRowCreateForm();
        this.dispatchAction(PopupShowForm({
            Id: 'create_row_form',
            Form: createForm,
        }));
    }
    async displayCloneRowForm(primaryKey) {
        const rowNode = this.getGridApi().getRowNodeForPrimaryKey(primaryKey);
        if (!rowNode) {
            this.logWarn(`Can NOT clone row: rowNode not found for primaryKey ${primaryKey}`);
            return;
        }
        const createForm = await this.internalApi.buildRowCreateForm(rowNode);
        this.dispatchAction(PopupShowForm({
            Id: 'create_row_form',
            Form: createForm,
            // formProps are added to the formContext
            FormProps: {
                clonedRowNode: rowNode,
            },
        }));
    }
    displayDeleteRowForm(primaryKey) {
        const rowNode = this.getGridApi().getRowNodeForPrimaryKey(primaryKey);
        if (!rowNode) {
            this.logWarn(`Can NOT delete row: rowNode not found for primaryKey ${primaryKey}`);
        }
        const eventInfo = {
            type: 'rowDeleted',
            rowNode: rowNode,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().internalApi.fireRowFormSubmittedEvent(eventInfo);
        this.getRowFormOptions().onRowFormSubmit?.(eventInfo);
    }
}
