'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const lang = require('./lang-6e59c27f.js');
const helpers = require('./helpers-a7be7ad9.js');
const theme = require('./theme-cccbc109.js');

const MAX_ELEMENTS_ON_CONTAINER = 4;
class NotificationService {
    constructor(topContainer = null, bottomContainer = null) {
        this.topContainer = topContainer;
        this.bottomContainer = bottomContainer;
        this.topContainer = helpers.getNotificationContainerElement('top');
        this.bottomContainer = helpers.getNotificationContainerElement('bottom');
    }
    call(options) {
        if (typeof options === 'string') {
            this.prepareNotification({
                message: options,
            });
        }
        else {
            this.prepareNotification(options);
        }
    }
    shouldQueue(options) {
        if (!this.topContainer && options.position === 'top') {
            this.topContainer = helpers.getNotificationContainerElement('top');
        }
        if (!this.bottomContainer && options.position === 'bottom') {
            this.bottomContainer = helpers.getNotificationContainerElement('bottom');
        }
        return (options.position === 'bottom' ? this.bottomContainer : this.topContainer).childElementCount >= MAX_ELEMENTS_ON_CONTAINER;
    }
    prepareNotification(options) {
        if (this.shouldQueue(options)) {
            setTimeout(() => this.prepareNotification(options), 250);
        }
        else {
            const el = document.createElement('x-notification');
            if (options.label) {
                const label = document.createElement('h4');
                label.innerText = options.label;
                el.appendChild(label);
            }
            if (options.onclose) {
                el.addEventListener('close', options.onclose);
            }
            const message = document.createElement('p');
            message.innerHTML = options.message;
            el.appendChild(message);
            options.color && el.setAttribute('color', options.color);
            options.icon && el.setAttribute('icon', options.icon);
            options.interval && el.setAttribute('interval', options.interval.toString());
            (options.position === 'bottom' ? this.bottomContainer : this.topContainer).insertAdjacentElement('afterbegin', el);
        }
    }
    success(message, label, icon, interval) {
        this.prepareNotification({
            color: 'success',
            label: label || lang.Lang('alert.success'),
            message,
            icon,
            interval,
        });
    }
    fail(message, label, icon, interval) {
        this.prepareNotification({
            color: 'danger',
            label: label || lang.Lang('alert.danger'),
            message,
            icon,
            interval,
        });
    }
    static create() {
        const instance = new NotificationService();
        return Object.assign((options) => instance.call(options), {
            success: (message, label, icon, interval) => instance.success(message, label, icon, interval),
            fail: (message, label, icon, interval) => instance.fail(message, label, icon, interval)
        });
    }
}
const Notification = NotificationService.create();

window.lang = lang.Lang;
window.notification = Notification;
window.store = theme.Store;
window.theme = theme.Theme;
window.query = theme.Query;
const Services = {
    lang: lang.Lang,
    notification: Notification,
    store: theme.Store,
    theme: theme.Theme,
    query: theme.Query,
};

const UI = Services;

exports.UI = UI;
