// The MIT License (MIT)
//
// Copyright (c) 2016-2024 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
export var MessageType;
(function (MessageType) {
    MessageType["ERROR"] = "error";
    MessageType["INFORMATION"] = "information";
    MessageType["SUCCESS"] = "success";
    MessageType["WARNING"] = "warning";
})(MessageType || (MessageType = {}));
/**
 * Abstract class for services that display messages.
 * @class
 * @abstract
 */
export default class {
    /**
     * Show the message.
     * @abstract
     * @param message Message.
     * @protected
     */
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    showMessage(message) { }
    /**
     * Show disclaimer message string or object or list of disclame message
     * strings or objects.
     * @param object A message or list of messages as text or configuration objects.
     */
    show(object) {
        const msgObjects = this.getMessageObjects(object);
        msgObjects.forEach((msg) => this.showMessage(msg));
    }
    /**
     * Display the given error message or list of error messages.
     * @param message Message or list of messages.
     */
    error(message) {
        this.show(this.getMessageObjects(message, MessageType.ERROR));
    }
    /**
     * Display the given info message or list of info messages.
     * @param message Message or list of messages.
     */
    info(message) {
        this.show(this.getMessageObjects(message, MessageType.INFORMATION));
    }
    /**
     * Display the given success message or list of success messages.
     * @param message Message or list of messages.
     */
    success(message) {
        this.show(this.getMessageObjects(message, MessageType.SUCCESS));
    }
    /**
     * Display the given warning message or list of warning messages.
     * @param message Message or list of messages.
     */
    warn(message) {
        this.show(this.getMessageObjects(message, MessageType.WARNING));
    }
    /**
     * Returns an array of message object from any given message string, list of
     * message strings, message object or list message objects. The type can be
     * overridden here as well OR defined (if the message(s) is/are string(s),
     * defaults to 'information').
     * @param object
     *     A message or list of messages as text or configuration objects.
     * @param opt_type The type of message to override the messages with.
     * @returns List of message objects.
     * @protected
     */
    getMessageObjects(object, opt_type) {
        const msgObjects = [];
        const defaultType = MessageType.INFORMATION;
        if (typeof object === 'string') {
            msgObjects.push({
                msg: /** @type {string} */ object,
                type: opt_type !== undefined ? opt_type : defaultType,
            });
        }
        else if (Array.isArray(object)) {
            object.forEach((msg) => {
                if (typeof object === 'string') {
                    msgObjects.push({
                        msg: msg,
                        type: opt_type !== undefined ? opt_type : defaultType,
                    });
                }
                else {
                    if (typeof msg == 'string') {
                        throw new Error('Wrong msg type');
                    }
                    if (opt_type !== undefined) {
                        msg.type = opt_type;
                    }
                    msgObjects.push(msg);
                }
            });
        }
        else {
            const msgObject = object;
            if (opt_type !== undefined) {
                msgObject.type = opt_type;
            }
            if (msgObject.type === undefined) {
                msgObject.type = defaultType;
            }
            msgObjects.push(msgObject);
        }
        return msgObjects;
    }
}
//# sourceMappingURL=Message.js.map