"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemStatusApiImpl = void 0;
const tslib_1 = require("tslib");
const SystemStatusRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/SystemStatusRedux"));
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const ApiBase_1 = require("./ApiBase");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ObjectFactory_1 = tslib_1.__importDefault(require("../../Utilities/ObjectFactory"));
const react_1 = require("react");
const UIHelper_1 = tslib_1.__importDefault(require("../../View/UIHelper"));
const StringExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/StringExtensions"));
const SystemStatusInternalApi_1 = require("../Internal/SystemStatusInternalApi");
const resolveContainerElement_1 = require("../../Utilities/resolveContainerElement");
class SystemStatusApiImpl extends ApiBase_1.ApiBase {
    systemStatusDiv;
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new SystemStatusInternalApi_1.SystemStatusInternalApi(_adaptable);
    }
    setSystemStatus(statusMessage, messageType, statusFurtherInformation) {
        let systemStatusMessageInfo = ObjectFactory_1.default.CreateSystemStatusMessageInfo(statusMessage, messageType, statusFurtherInformation ? statusFurtherInformation : '');
        this.internalApi.addSystemStatusMessageInfo(systemStatusMessageInfo);
        // publish the event
        this.getEventApi().internalApi.fireSystemStatusMessageDisplayedEvent(systemStatusMessageInfo);
        if (this.getNotificationsOptions().showSystemStatusMessageNotifications) {
            const alertApi = this.getAlertApi();
            let fullMessage = statusMessage;
            if (StringExtensions_1.default.IsNotNullOrEmpty(statusFurtherInformation)) {
                fullMessage += '\n' + statusFurtherInformation;
            }
            switch (messageType) {
                case 'Success':
                    alertApi.showAlertSuccess('System Status', fullMessage);
                    return;
                case 'Info':
                    alertApi.showAlertInfo('System Status', fullMessage);
                    return;
                case 'Warning':
                    alertApi.showAlertWarning('System Status', fullMessage);
                    return;
                case 'Error':
                    alertApi.showAlertError('System Status', fullMessage);
                    return;
            }
        }
        if (this.getContainerOptions().systemStatusContainer != null) {
            this.displayMessageInDiv(statusMessage, statusFurtherInformation, messageType);
        }
        // doesnt do anything but useful for Audit
        this.dispatchAction(SystemStatusRedux.SystemStatusSetMesage(systemStatusMessageInfo));
    }
    displayMessageInDiv(statusMessage, statusFurtherInformation, messageType) {
        if (!this.systemStatusDiv) {
            this.systemStatusDiv = (0, resolveContainerElement_1.resolveContainerElement)(this.getContainerOptions().systemStatusContainer, this.getAdaptableInternalApi().buildBaseContext());
        }
        if (this.systemStatusDiv) {
            if (StringExtensions_1.default.IsNotNullOrEmpty(statusFurtherInformation)) {
                statusMessage += statusFurtherInformation;
            }
            const foreColor = messageType ? UIHelper_1.default.getColorByMessageType(messageType) : 'black';
            this._adaptable.renderReactRoot((0, react_1.createElement)('span', { style: { color: foreColor } }, statusMessage), this.systemStatusDiv);
        }
    }
    setErrorSystemStatus(statusMessage, statusFurtherInformation) {
        this.setSystemStatus(statusMessage, 'Error', statusFurtherInformation);
    }
    setWarningSystemStatus(statusMessage, statusFurtherInformation) {
        this.setSystemStatus(statusMessage, 'Warning', statusFurtherInformation);
    }
    setSuccessSystemStatus(statusMessage, statusFurtherInformation) {
        this.setSystemStatus(statusMessage, 'Success', statusFurtherInformation);
    }
    setInfoSystemStatus(statusMessage, statusFurtherInformation) {
        this.setSystemStatus(statusMessage, 'Info', statusFurtherInformation);
    }
    openSystemStatusSettingsPanel() {
        this.showModulePopup(ModuleConstants.SystemStatusModuleId);
    }
    getCurrentSystemStatusMessageInfo() {
        const messageInfos = this.internalApi.getSystemStatusMessageInfos() || [];
        const orderedArray = messageInfos.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
        return orderedArray[0];
    }
    deleteAllSystemStatusMessages() {
        this.dispatchAction(InternalRedux.SystemStatusMessageInfoDeleteAll());
    }
    destroy() {
        this.systemStatusDiv = null;
        super.destroy();
    }
}
exports.SystemStatusApiImpl = SystemStatusApiImpl;
