import { UmbNotificationHandler } from './notification-handler.js';
import { UmbContextToken } from '../../../libs/context-api/index.js';
import { UmbContextBase } from '../../../libs/class-api/index.js';
import { UmbBasicState } from '../../../libs/observable-api/index.js';
export class UmbNotificationContext extends UmbContextBase {
    constructor(host) {
        super(host, UMB_NOTIFICATION_CONTEXT);
        // Notice this cannot use UniqueBehaviorSubject as it holds a HTML Element. which cannot be Serialized to JSON (it has some circular references)
        this._notifications = new UmbBasicState([]);
        this.notifications = this._notifications.asObservable();
    }
    /**
     * @private
     * @param {UmbNotificationOptions<UmbNotificationData>} options
     * @returns {*}  {UmbNotificationHandler}
     * @memberof UmbNotificationContext
     */
    #open(options) {
        const notificationHandler = new UmbNotificationHandler(options);
        notificationHandler.element?.addEventListener('closed', () => this._handleClosed(notificationHandler));
        this._notifications.setValue([...this._notifications.getValue(), notificationHandler]);
        return notificationHandler;
    }
    /**
     * @private
     * @param {string} key
     * @memberof UmbNotificationContext
     */
    _close(key) {
        this._notifications.setValue(this._notifications.getValue().filter((notification) => notification.key !== key));
    }
    /**
     * @private
     * @param notificationHandler
     * @param {string} key
     * @memberof UmbNotificationContext
     */
    _handleClosed(notificationHandler) {
        notificationHandler.element.removeEventListener('closed', () => this._handleClosed(notificationHandler));
        this._close(notificationHandler.key);
    }
    /**
     * Opens a notification that automatically goes away after 6 sek.
     * @param {UmbNotificationColor} color
     * @param {UmbNotificationOptions<UmbNotificationData>} options
     * @returns {*}
     * @memberof UmbNotificationContext
     */
    peek(color, options) {
        return this.#open({ color, ...options });
    }
    /**
     * Opens a notification that stays on the screen until dismissed by the user or custom code
     * @param {UmbNotificationColor} color
     * @param {UmbNotificationOptions<UmbNotificationData>} options
     * @returns {*}
     * @memberof UmbNotificationContext
     */
    stay(color, options) {
        return this.#open({ ...options, color, duration: null });
    }
}
export const UMB_NOTIFICATION_CONTEXT = new UmbContextToken('UmbNotificationContext');
