/* Minification failed. Returning unminified contents.
(9,5-6): run-time error JS1009: Expected '}': [
(9,39): run-time error JS1004: Expected ';'
(9,39-40): run-time error JS1195: Expected expression: :
(10,36): run-time error JS1004: Expected ';'
(10,36-37): run-time error JS1195: Expected expression: :
(11,39): run-time error JS1004: Expected ';'
(11,39-40): run-time error JS1195: Expected expression: :
(12,37): run-time error JS1004: Expected ';'
(12,37-38): run-time error JS1195: Expected expression: :
(13,1-2): run-time error JS1195: Expected expression: }
(13,1-2): run-time error JS1002: Syntax error: }
(16,5-6): run-time error JS1009: Expected '}': [
(16,39): run-time error JS1004: Expected ';'
(16,39-40): run-time error JS1195: Expected expression: :
(17,36): run-time error JS1004: Expected ';'
(17,36-37): run-time error JS1195: Expected expression: :
(18,39): run-time error JS1004: Expected ';'
(18,39-40): run-time error JS1195: Expected expression: :
(19,37): run-time error JS1004: Expected ';'
(19,37-38): run-time error JS1195: Expected expression: :
(20,1-2): run-time error JS1195: Expected expression: }
(20,1-2): run-time error JS1002: Syntax error: }
 */
var POPUP_NOTIFICATIONS_TYPE = {
    SUCCESS: 'success',
    INFO: 'info',
    WARNING: 'warning',
    ERROR: 'error',
}

var POPUP_NOTIFICATIONS_TYPE_CLASS = {
    [POPUP_NOTIFICATIONS_TYPE.SUCCESS]: 'cmn--alert--green',
    [POPUP_NOTIFICATIONS_TYPE.INFO]: 'cmn--alert--blue',
    [POPUP_NOTIFICATIONS_TYPE.WARNING]: 'cmn--alert--yellow',
    [POPUP_NOTIFICATIONS_TYPE.ERROR]: 'cmn--alert--red',
}

var POPUP_NOTIFICATIONS_TYPE_ICON_CLASS = {
    [POPUP_NOTIFICATIONS_TYPE.SUCCESS]: 'fa-check-circle',
    [POPUP_NOTIFICATIONS_TYPE.INFO]: 'fa-info-circle',
    [POPUP_NOTIFICATIONS_TYPE.WARNING]: 'fa-exclamation-triangle',
    [POPUP_NOTIFICATIONS_TYPE.ERROR]: 'fa-exclamation-circle',
}

var POPUP_NOTIFICATIONS_DURATION = 10000; // 10 seconds per notification

var popupNotifications = (function () {
    var pub = {
        TYPE: POPUP_NOTIFICATIONS_TYPE,
        notifications: ko.observableArray(),
    };

    pub.add = add;
    function add({ title, type, text = '', duration = POPUP_NOTIFICATIONS_DURATION }) {
        var id = (new Date().getTime()) + Math.random();

        pub.notifications.push({
            id,
            title,
            text,
            type,
            class: POPUP_NOTIFICATIONS_TYPE_CLASS[type],
            iconClass: POPUP_NOTIFICATIONS_TYPE_ICON_CLASS[type],
        });

        setTimeout(function () {
            close(id);
        }, duration);
    }

    pub.close = close;
    function close(id) {
        const removingNotificationElement = document.getElementById('popup-notification-' + id);

        if (removingNotificationElement) {
            removingNotificationElement.classList.add('removing');
        }

        setTimeout(() => {
            remove(id);
        }, 200)
    }

    function remove(id) {
        const notificationIndex = popupNotifications.notifications().findIndex((element) => element.id === id);
        if (notificationIndex === -1) {
            return;
        }

        pub.notifications.splice(notificationIndex, 1);
    }

    return pub;
})();

$(document).ready(function () {
    ko.applyBindings(popupNotifications, document.getElementById("popup-notifications"));
});;
