/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { isFunction } from '@tylertech/forge-core';
import { TOAST_CONSTANTS } from './toast-constants';
/**
 * The foundation class behind the `ToastComponent` class that manages the state of a toast component instance.
 */
export class ToastFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._duration = TOAST_CONSTANTS.defaults.DURATION;
        this._placement = TOAST_CONSTANTS.defaults.PLACEMENT;
        this._showClose = true;
        this._hasAction = false;
        this._isActive = false;
        this._actionListener = (evt) => this._onAction(evt);
        this._closeListener = (evt) => this._onClose(evt);
    }
    initialize() {
        this._adapter.setPlacement(this._placement);
        this._adapter.registerCloseListener(this._closeListener);
        this._adapter.setCloseButtonVisibility(this._showClose);
        this._adapter.setActionVisibility(!!this._actionText);
        this.show();
    }
    /**
     * Starts the animation of the toast and hides it after the duration.
     */
    show() {
        // Check if we were provided a builder function and call it if so (this will override the message text)
        if (isFunction(this._builder)) {
            const tpl = this._builder();
            this._adapter.setMessageTemplate(tpl);
        }
        this._isActive = true;
        this._adapter.setActive(true);
        if (isFinite(this._duration) && this._duration > 0) {
            this._hideTimeout = setTimeout(() => this.hide(true), this._duration);
        }
    }
    /**
     * Hides the toast and removes it from the DOM.
     */
    hide(emitEvent = false) {
        if (!this._isActive) {
            return;
        }
        this._isActive = false;
        this._adapter.setActive(false);
        if (this._hideTimeout) {
            clearTimeout(this._hideTimeout);
            this._hideTimeout = undefined;
        }
        if (emitEvent) {
            this._adapter.emitHostEvent(TOAST_CONSTANTS.events.CLOSE);
        }
    }
    /**
     * Handles click events from the optional action button.
     * @param {MouseEvent} evt The mouse event.
     */
    _onAction(evt) {
        evt.stopPropagation();
        this._adapter.emitHostEvent(TOAST_CONSTANTS.events.ACTION);
    }
    _onClose(evt) {
        this.hide(true);
    }
    /** The message to display in the toast. */
    get message() {
        return this._message;
    }
    set message(value) {
        if (this._message !== value) {
            this._message = value;
            this._adapter.setMessage(this._message);
            this._adapter.setHostAttribute(TOAST_CONSTANTS.attributes.MESSAGE, this._message);
        }
    }
    /** The text to display in the action button. */
    get actionText() {
        return this._actionText;
    }
    set actionText(value) {
        if (this._actionText !== value) {
            this._actionText = value;
            if (this._actionText) {
                if (this._hasAction) {
                    this._adapter.setActionText(this._actionText);
                }
                else {
                    this._adapter.setActionText(this._actionText);
                    this._adapter.setActionVisibility(true);
                    this._adapter.registerActionListener('click', this._actionListener);
                    this._hasAction = true;
                }
            }
            else if (this._hasAction) {
                this._adapter.setActionText('');
                this._adapter.setActionVisibility(false);
                this._adapter.deregisterActionListener('click', this._actionListener);
                this._hasAction = false;
            }
            this._adapter.setHostAttribute(TOAST_CONSTANTS.attributes.ACTION_TEXT, this._actionText);
        }
    }
    /** The time in milliseconds to show the toast. */
    get duration() {
        return this._duration;
    }
    set duration(value) {
        if (this._duration !== value) {
            this._duration = value;
            if (this._hideTimeout) {
                clearTimeout(this._hideTimeout);
                this._hideTimeout = setTimeout(() => this.hide(true), this._duration);
            }
        }
    }
    /** The placement of the toast. */
    get placement() {
        return this._placement;
    }
    set placement(value) {
        if (this._placement !== value) {
            this._placement = value;
            this._adapter.setPlacement(this._placement);
        }
        this._adapter.setHostAttribute(TOAST_CONSTANTS.attributes.PLACEMENT, this._placement);
    }
    get builder() {
        return this._builder;
    }
    set builder(value) {
        this._builder = value;
    }
    get showClose() {
        return this._showClose;
    }
    set showClose(value) {
        if (this._showClose !== value) {
            this._showClose = value;
            this._adapter.setCloseButtonVisibility(this._showClose);
            this._adapter.setHostAttribute(TOAST_CONSTANTS.attributes.SHOW_CLOSE, this._showClose.toString());
        }
    }
}
