import { ApiBase } from '../Implementation/ApiBase';
import isEqual from 'lodash/isEqual';
export class EventInternalApi extends ApiBase {
    fireGridSortedEvent() {
        if (this.isAdapTableReady()) {
            const adaptableSortState = this.getStateApi().getAdaptableSortState();
            const gridSortedInfo = {
                adaptableSortState: adaptableSortState,
                ...this.getAdaptableInternalApi().buildBaseContext(),
            };
            this.getEventApi().emit('GridSorted', gridSortedInfo);
        }
    }
    fireCellChangedEvent(cellDataChangedInfo) {
        if (this.isAdapTableReady()) {
            const cellChangedInfo = {
                ...this.getAdaptableInternalApi().buildBaseContext(),
                cellDataChange: cellDataChangedInfo,
            };
            this.getEventApi().emit('CellChanged', cellChangedInfo);
        }
    }
    fireRowChangedEvent(rowDataChangedInfo) {
        if (this.isAdapTableReady()) {
            const rowChangedInfo = {
                ...this.getAdaptableInternalApi().buildBaseContext(),
                rowDataChange: rowDataChangedInfo,
            };
            this.getEventApi().emit('RowChanged', rowChangedInfo);
        }
    }
    fireDashboardChangedEvent(trigger, oldDashboardState, newDashboardState) {
        const isToolbarStateChangedToVisible = (toolbarName) => {
            const visibleInNewState = this.getDashboardApi().internalApi.isToolbarInActiveTab(toolbarName, newDashboardState);
            const visibleInOldState = this.getDashboardApi().internalApi.isToolbarInActiveTab(toolbarName, oldDashboardState);
            if (visibleInNewState && visibleInOldState) {
                // check for dashboard collapse/expand/hidden changes
                return ((oldDashboardState.IsCollapsed && !newDashboardState.IsCollapsed) ||
                    (oldDashboardState.IsHidden && !newDashboardState.IsHidden));
            }
            return visibleInNewState && !visibleInOldState;
        };
        const isToolbarStateChangedToHidden = (toolbarName) => {
            const visibleInNewState = this.getDashboardApi().internalApi.isToolbarInActiveTab(toolbarName, newDashboardState);
            const visibleInOldState = this.getDashboardApi().internalApi.isToolbarInActiveTab(toolbarName, oldDashboardState);
            if (visibleInNewState && visibleInOldState) {
                // check for dashboard collapse/expand/hidden changes
                return ((!oldDashboardState.IsCollapsed && newDashboardState.IsCollapsed) ||
                    (!oldDashboardState.IsHidden && newDashboardState.IsHidden));
            }
            return visibleInOldState && !visibleInNewState;
        };
        const dashboardChangedInfo = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            actionName: trigger,
            oldDashboardState,
            newDashboardState,
            isToolbarStateChangedToVisible,
            isToolbarStateChangedToHidden,
        };
        if (!isEqual(oldDashboardState, newDashboardState)) {
            setTimeout(() => {
                const eventApi = this.getEventApi();
                if (eventApi) {
                    eventApi.emit('DashboardChanged', dashboardChangedInfo);
                }
            });
        }
    }
    fireAlertFiredEvent(alertToFire) {
        const alertFiredInfo = {
            alert: alertToFire,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('AlertFired', alertFiredInfo);
    }
    fireThemeChangedEvent(theme, trigger) {
        let themeChangedInfo = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            trigger: trigger,
            theme: theme,
        };
        this.getEventApi().emit('ThemeChanged', themeChangedInfo);
    }
    fireLayoutChangedEvent(trigger, oldSate, newState) {
        if (isEqual(oldSate, newState)) {
            return;
        }
        const layoutChangedInfo = {
            actionName: trigger,
            oldLayoutState: oldSate,
            newLayoutState: newState,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('LayoutChanged', layoutChangedInfo);
    }
    fireScheduleTriggeredEvent(schedule) {
        const scheduleTriggeredInfo = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            schedule: schedule,
        };
        this.getEventApi().emit('ScheduleTriggered', scheduleTriggeredInfo);
    }
    fireDataSetSelectedEvent(dataSet) {
        if (this.isAdapTableReady()) {
            const dataSetSelectedInfo = {
                ...this.getAdaptableInternalApi().buildBaseContext(),
                dataSet,
            };
            this.getEventApi().emit('DataSetSelected', dataSetSelectedInfo);
        }
    }
    fireSystemStatusMessageDisplayedEvent(systemStatusMessageInfo) {
        const systemStatusMessageDisplayedInfo = {
            systemStatusMessageInfo: systemStatusMessageInfo,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('SystemStatusMessageDisplayed', systemStatusMessageDisplayedInfo);
    }
    fireDataImportedEvent(importData, addedRows, updatedRows) {
        const dataImportedInfo = {
            importData,
            addedRows,
            updatedRows,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('DataImported', dataImportedInfo);
        return dataImportedInfo;
    }
    fireAdaptableStateChangedEvent(action, oldState, newState) {
        const adaptableStateChangedInfo = {
            actionName: action.type,
            ...this.getAdaptableInternalApi().buildBaseContext(),
            action: action,
            oldState: oldState,
            newState: newState,
        };
        this.getEventApi().emit('AdaptableStateChanged', adaptableStateChangedInfo);
    }
    fireAdaptableStateReloadedEvent(oldState, newState) {
        const stateReloadedInfo = {
            oldState,
            newState,
            adaptableStateKey: this.getOptionsApi().getAdaptableStateKey(),
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('AdaptableStateReloaded', stateReloadedInfo);
    }
    fireCalculatedColumnChangedEvent(trigger, calculatedColumn) {
        const calculatedColumnChangedInfo = {
            actionName: trigger,
            calculatedColumn: calculatedColumn,
            calculatedColumnExpressionAST: this.getExpressionApi().getASTForExpression(this.getCalculatedColumnApi().internalApi.getExpressionFromCalculatedColumn(calculatedColumn)),
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        this.getEventApi().emit('CalculatedColumnChanged', calculatedColumnChangedInfo);
    }
    fireColumnFilterAppliedEvent() {
        if (this.isAdapTableReady()) {
            const columnFilterAppliedInfo = {
                columnFilters: this.getColumnFilterApi().getColumnFilters(),
                ...this.getAdaptableInternalApi().buildBaseContext(),
            };
            this.getEventApi().emit('ColumnFilterApplied', columnFilterAppliedInfo);
        }
    }
    fireGridFilterAppliedEvent() {
        if (this.isAdapTableReady()) {
            const currentGridFilter = this.getGridFilterApi().getCurrentGridFilter();
            const gridFilterAppliedInfo = {
                gridFilter: currentGridFilter,
                gridFilterExpressionAST: currentGridFilter
                    ? this.getExpressionApi().getASTForExpression(currentGridFilter?.Expression)
                    : null,
                ...this.getAdaptableInternalApi().buildBaseContext(),
            };
            this.getEventApi().emit('GridFilterApplied', gridFilterAppliedInfo);
        }
    }
    fireRowFormSubmittedEvent(rowFormSubmittedInfo) {
        this.getEventApi().emit('RowFormSubmitted', rowFormSubmittedInfo);
    }
    fireTeamSharingEntityChangedEvent(sharedEntity) {
        if (this.isAdapTableReady()) {
            const teamSharingEntityChangedInfo = {
                ...this.getAdaptableInternalApi().buildBaseContext(),
                sharedEntity: sharedEntity,
            };
            this.getEventApi().emit('TeamSharingEntityChanged', teamSharingEntityChangedInfo);
        }
    }
    fireFlashingCellDisplayedEvent(flashingCellToShow) {
        const flashingCellFiredInfo = {
            ...this.getAdaptableInternalApi().buildBaseContext(),
            flashingCell: flashingCellToShow,
        };
        const results = this.getEventApi().emitSync('FlashingCellDisplayed', flashingCellFiredInfo);
    }
}
