// #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 { on } from '@breadstone/mosaik-elements';
import { fadeOut, fadeSlideIn } from '../../../../Animations/Presets/Fade';
import { Timer } from '../../../../Dom/Timer';
import { Animatable } from '../../../Behaviors/Animatable';
import { Busyable } from '../../../Behaviors/Busyable';
import { TextFormattable } from '../../../Behaviors/TextFormattable';
import { Themeable } from '../../../Behaviors/Themeable';
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 { ProgressRingElement } from '../../Ranges/ProgressRing/ProgressRingElement';
import { OverlayElement } from '../Abstracts/OverlayElement';
import { toastElementCosmopolitanStyle } from './Themes/ToastElement.Cosmopolitan';
import { toastElementJoyStyle } from './Themes/ToastElement.Joy';
import { toastElementMemphisStyle } from './Themes/ToastElement.Memphis';
import { toastElementTemplate } from './ToastElementTemplate';
/**
 * Toast - A transient notification that appears briefly, typically near the edge of the screen.
 *
 * @description
 * Displays temporary messages with configurable position, timeout, and progress indicator.
 * Header and content text must be set via JavaScript. Only `timeout`, `position`, and
 * `show-progress` are reflected as HTML attributes.
 *
 * @name Toast
 * @element mosaik-toast
 * @category Overlays
 *
 * @slot actions - The actions slot.
 *
 * @csspart backdrop - The backdrop part.
 * @csspart content - The content part.
 * @csspart header - The header part.
 * @csspart progressFill - The progress fill bar that animates from right to left during timeout.
 * @csspart progressRing - The progressRing part.
 * @csspart root - The root part.
 *
 * @cssprop {String} --toast-background-color - The background color CSS custom property.
 * @cssprop {String} --toast-border - The border CSS custom property.
 * @cssprop {String} --toast-border-color - The border color CSS custom property.
 * @cssprop {String} --toast-border-radius - The border radius CSS custom property.
 * @cssprop {String} --toast-border-style - The border style CSS custom property.
 * @cssprop {String} --toast-border-width - The border width CSS custom property.
 * @cssprop {String} --toast-font-family - The font family CSS custom property.
 * @cssprop {String} --toast-font-letter-spacing - The font letter spacing CSS custom property.
 * @cssprop {String} --toast-font-line-height - The font line height CSS custom property.
 * @cssprop {String} --toast-font-size - The font size CSS custom property.
 * @cssprop {String} --toast-font-text-decoration - The font text decoration CSS custom property.
 * @cssprop {String} --toast-font-text-transform - The font text transform CSS custom property.
 * @cssprop {String} --toast-font-weight - The font weight CSS custom property.
 * @cssprop {String} --toast-foreground-color - The foreground color CSS custom property.
 * @cssprop {String} --toast-gap - The gap CSS custom property.
 * @cssprop {String} --toast-padding-bottom - The padding bottom CSS custom property.
 * @cssprop {String} --toast-padding-left - The padding left CSS custom property.
 * @cssprop {String} --toast-padding-right - The padding right CSS custom property.
 * @cssprop {String} --toast-padding-top - The padding top CSS custom property.
 * @cssprop {String} --toast-offset-y - The vertical edge offset used for top and bottom positions.
 * @cssprop {String} --toast-progress-fill-color - The progress fill bar color CSS custom property.
 * @cssprop {String} --toast-progress-ring-fill-color - The progress ring fill color CSS custom property.
 * @cssprop {String} --toast-shadow - The shadow CSS custom property.
 * @cssprop {String} --toast-shadow-blur - The shadow blur CSS custom property.
 * @cssprop {String} --toast-shadow-color - The shadow color CSS custom property.
 * @cssprop {String} --toast-shadow-offset-x - The shadow offset x CSS custom property.
 * @cssprop {String} --toast-shadow-offset-y - The shadow offset y CSS custom property.
 * @cssprop {String} --toast-shadow-spread - The shadow spread CSS custom property.
 * @cssprop {String} --toast-transition-duration - The transition duration CSS custom property.
 * @cssprop {String} --toast-transition-mode - The transition mode CSS custom property.
 * @cssprop {String} --toast-transition-property - The transition property CSS custom property.
 * @cssprop {String} --toast-translate - The translate CSS custom property.
 *
 * @dependency mosaik-backdrop - The Backdrop element.
 * @dependency mosaik-button - The Button element.
 * @dependency mosaik-progress-ring - The Progress Ring element.
 * @dependency mosaik-stack - The Stack element.
 * @dependency mosaik-text - The Text element.
 *
 * @example
 * Toast at the top with progress:
 * ```html
 * <mosaik-toast position="top" show-progress timeout="3000"></mosaik-toast>
 * ```
 *
 * @example
 * Toast at bottom-center:
 * ```html
 * <mosaik-toast position="bottom"></mosaik-toast>
 * ```
 *
 * @public
 */
let ToastElement = class ToastElement extends Themeable(Animatable(Variantable(TextFormattable(Busyable(OverlayElement))))) {
    // #region Fields
    _timer;
    _header;
    _content;
    _timeout;
    _position;
    _showProgress;
    _fillElement;
    _fillAnimation;
    _mouseOverSubscription;
    _mouseOutSubscription;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._header = '';
        this._content = '';
        this._timeout = 0;
        this._position = ToastPosition.Middle;
        this._showProgress = false;
        this._fillElement = null;
        this._fillAnimation = null;
        this._timer = Timer.empty(() => this.close());
        this._mouseOverSubscription = null;
        this._mouseOutSubscription = null;
        this.enter = fadeSlideIn;
        this.exit = fadeOut;
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `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 the animation target element.
     * For the toast, animations are applied to the root template part instead of the host element.
     *
     * @public
     * @override
     * @readonly
     */
    get animationTarget() {
        return this.getTemplatePart('root');
    }
    // #endregion
    // #region Methods
    /**
     * Handles the `isOpen` property change.
     * Starts or stops the timer and fill animation accordingly.
     *
     * @protected
     */
    onIsOpenPropertyChanged(prev, next) {
        if (next) {
            void this.play(this.enter ?? { steps: [] });
            this._timer.start();
            if (this._showProgress && this._timeout > 0) {
                this.startFillAnimation();
            }
        }
        else {
            this._timer.destroy();
            this.cancelFillAnimation();
        }
    }
    /**
     * Closes the `ToastElement`.
     *
     * @public
     * @override
     */
    async close() {
        return this.play(this.exit ?? { steps: [] }).then(() => super.close());
    }
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        // Note: Animation is now handled in open() method, not here
    }
    /**
     * @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.pauseFillAnimation();
        });
        this._mouseOutSubscription = on(root, 'mouseout', () => {
            this._timer.resume();
            this.resumeFillAnimation();
        });
    }
    /**
     * Handles the backdrop click event.
     * Only closes the toast if `clickOutsideToClose` is enabled.
     *
     * @private
     * @template
     */
    onBackdrop() {
        if (this.clickOutsideToClose) {
            void this.close();
        }
    }
    /**
     * @protected
     */
    onShowProgressPropertyChanged() {
        if (this._showProgress && this.isOpen && this._timeout > 0) {
            this.startFillAnimation();
        }
        else {
            this.cancelFillAnimation();
        }
    }
    /**
     * Starts the progress fill animation that shrinks from right to left.
     *
     * @private
     */
    startFillAnimation() {
        this.cancelFillAnimation();
        if (!this._fillElement) {
            this._fillElement = this.getTemplatePart('progressFill');
        }
        if (!this._fillElement) {
            return;
        }
        this._fillAnimation = this._fillElement.animate([
            { width: '100%' },
            { width: '0%' }
        ], {
            duration: this._timeout,
            easing: 'linear',
            fill: 'forwards'
        });
    }
    /**
     * Pauses the fill animation.
     *
     * @private
     */
    pauseFillAnimation() {
        this._fillAnimation?.pause();
    }
    /**
     * Resumes the fill animation.
     *
     * @private
     */
    resumeFillAnimation() {
        this._fillAnimation?.play();
    }
    /**
     * Cancels and resets the fill animation.
     *
     * @private
     */
    cancelFillAnimation() {
        if (this._fillAnimation) {
            this._fillAnimation.cancel();
            this._fillAnimation = null;
        }
    }
};
__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([
    Watch('isOpen', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], ToastElement.prototype, "onIsOpenPropertyChanged", 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,
            ProgressRingElement,
            StackElement,
            BackdropElement,
            ButtonElement
        ]
    }),
    __metadata("design:paramtypes", [])
], ToastElement);
export { ToastElement };
//# sourceMappingURL=ToastElement.js.map