// #region Imports
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { fadeOut, fadeSlideIn } from '../../../../Animations/Presets/Fade';
import { Interval } from '../../../../Dom/Interval';
import { Timer } from '../../../../Dom/Timer';
import { on } from '@breadstone/mosaik-elements';
import { Animatable } from '../../../Behaviors/Animatable';
import { Busyable } from '../../../Behaviors/Busyable';
import { TextFormattable } from '../../../Behaviors/TextFormattable';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { ToastPosition } from '../../../Types/ToastPosition';
import { ButtonElement } from '../../Buttons/Button/ButtonElement';
import { StackElement } from '../../Layouts/Stack/StackElement';
import { BackdropElement } from '../../Primitives/Backdrop/BackdropElement';
import { TextElement } from '../../Primitives/Text/TextElement';
import { ProgressBarElement } from '../../Ranges/ProgressBar/ProgressBarElement';
import { ProgressRingElement } from '../../Ranges/ProgressRing/ProgressRingElement';
import { OverlayElement } from '../Abstracts/OverlayElement';
import { toastElementTemplate } from './ToastElementTemplate';
import { toastElementJoyStyle } from './Themes/ToastElement.Joy';
import { toastElementMemphisStyle } from './Themes/ToastElement.Memphis';
import { toastElementCosmopolitanStyle } from './Themes/ToastElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
/**
 * Toast - A transient message or notification that appears briefly, often near the edge of the screen.
 *
 * @element mosaik-toast
 *
 * @slot actions - The actions slot.
 *
 * @public
 */
let ToastElement = class ToastElement extends Themeable(Animatable(Variantable(TextFormattable(Busyable(OverlayElement))))) {
    // #region Fields
    _timer;
    _interval;
    _intervalTick;
    _header;
    _content;
    _timeout;
    _position;
    _showProgress;
    _mouseOverSubscription;
    _mouseOutSubscription;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._header = '';
        this._content = '';
        this._timeout = 0;
        this._position = ToastPosition.Middle;
        this._showProgress = false;
        this._intervalTick = 0;
        this._timer = Timer.empty(() => this.close());
        this._interval = new Interval(100, (tick) => this.intervalTick = tick - 100);
        this._mouseOverSubscription = null;
        this._mouseOutSubscription = null;
        this.enter = fadeSlideIn;
        this.exit = fadeOut;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-toast';
    }
    /**
     * Gets or sets the `header` property.
     *
     * @public
     */
    get header() {
        return this._header;
    }
    set header(value) {
        if (this._header !== value) {
            this._header = value;
            this.requestUpdate('header');
        }
    }
    /**
     * Gets or sets the `content` property.
     *
     * @public
     */
    get content() {
        return this._content;
    }
    set content(value) {
        if (this._content !== value) {
            this._content = value;
            this.requestUpdate('content');
        }
    }
    /**
     * Gets or sets the `timeout` property.
     * The timeout only when the given value is greater than zero.
     *
     * @public
     * @attr
     */
    get timeout() {
        return this._timeout;
    }
    set timeout(value) {
        if (this._timeout !== value) {
            this._timeout = value;
            this._timer.setDuration(this._timeout);
            this.requestUpdate('timeout');
        }
    }
    /**
     * Gets or sets the `position` property.
     *
     * @public
     * @attr
     */
    get position() {
        return this._position;
    }
    set position(value) {
        if (this._position !== value) {
            this._position = value;
            this.requestUpdate('position');
        }
    }
    /**
     * Gets or sets the `showProgress` property.
     *
     * @public
     * @attr
     */
    get showProgress() {
        return this._showProgress;
    }
    set showProgress(value) {
        if (this._showProgress !== value) {
            this._showProgress = value;
            this.requestUpdate('showProgress');
        }
    }
    /**
     * Gets or sets the `intervalTick` property.
     *
     * @public
     * @template
     * @readonly
     */
    // find a way to mark this as @internal
    get intervalTick() {
        return this._intervalTick;
    }
    set intervalTick(value) {
        if (this._intervalTick !== value) {
            this._intervalTick = value;
            this.requestUpdate('intervalTick');
        }
    }
    // #endregion
    // #region Methods
    /**
     * Opens the `ToastElement`.
     *
     * @public
     * @override
     */
    async open() {
        return super.open().then(() => {
            this._timer.start();
            this._interval.start();
        });
    }
    /**
     * Closes the `ToastElement`.
     *
     * @public
     * @override
     */
    async close() {
        return this.play(this.exit ?? { steps: [] }).then(() => super.close().then((x) => {
            this._timer.destroy();
            this._interval.destroy();
            return x;
        }));
    }
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        void this.play(this.enter ?? { steps: [] });
    }
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        this._mouseOverSubscription?.dispose();
        this._mouseOutSubscription?.dispose();
        super.disconnectedCallback();
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        const root = this.getTemplatePart('root');
        this._mouseOverSubscription = on(root, 'mouseover', () => {
            this._timer.pause();
            this._interval.stop();
        });
        this._mouseOutSubscription = on(root, 'mouseout', () => {
            this._timer.resume();
            this._interval.start();
        });
    }
    /**
     * @protected
     */
    onShowProgressPropertyChanged() {
        if (this._showProgress) {
            this._interval.start();
        }
        else {
            this._interval.stop();
        }
    }
};
__decorate([
    Property({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ToastElement.prototype, "header", null);
__decorate([
    Property({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ToastElement.prototype, "content", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ToastElement.prototype, "timeout", null);
__decorate([
    Attribute({ type: ToastPosition }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], ToastElement.prototype, "position", null);
__decorate([
    Attribute({ type: Boolean, useDefault: true }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], ToastElement.prototype, "showProgress", null);
__decorate([
    Property({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], ToastElement.prototype, "intervalTick", null);
__decorate([
    Watch('showProgress', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], ToastElement.prototype, "onShowProgressPropertyChanged", null);
ToastElement = __decorate([
    Component({
        selector: 'mosaik-toast',
        template: toastElementTemplate,
        themes: {
            joy: toastElementJoyStyle,
            memphis: toastElementMemphisStyle,
            cosmopolitan: toastElementCosmopolitanStyle
        },
        imports: [
            TextElement,
            ProgressBarElement,
            ProgressRingElement,
            StackElement,
            BackdropElement,
            ButtonElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ToastElement);
export { ToastElement };
//# sourceMappingURL=ToastElement.js.map