import { assert } from "../../core/assert.js";
import List from '../../core/collection/list/List.js';
import { Notification } from './Notification.js';

class NotificationLog {
    /**
     * @readonly
     * @type {List<Notification>}
     */
    elements = new List();

    /**
     * Once number of entries in the log reaches this amount, the earliest entries will be removed to make space for new ones
     * @type {number}
     */
    maxLength = 1000;

    /**
     *
     * @param {{}} options See {@link Notification.constructor} for details
     * @returns {Notification}
     */
    add(options) {
        const notification = new Notification(options);

        this.addNotification(notification);

        return notification;
    }

    /**
     *
     * @param {Notification} notification
     */
    addNotification(notification) {
        assert.defined(notification, 'notification');
        assert.notNull(notification, 'notification');

        assert.ok(notification.isNotification, 'not a Notification');

        // Crop notification log to size
        const length = this.elements.length;

        const target = this.maxLength - 1;

        if (length > target) {
            // too many elements, drop some
            this.elements.crop(length - target, length);
        }

        this.elements.add(notification);
    }
}

export default NotificationLog;
