import { h, Host } from "@stencil/core";
/** Fallback when computed `--animation-duration` is unavailable (matches `--pine-motion-duration-slow`). */
const TOAST_DISMISS_ANIMATION_MS = 300;
/**
 * @part dismiss
 */
export class PdsToast {
    constructor() {
        /**
         * Whether the toast can be dismissed manually via the close button.
         * Note: This only controls manual dismissal. Auto-dismissal via duration still applies.
         * @default true
         */
        this.dismissible = true;
        /**
         * The duration in milliseconds to show the toast before auto-dismissing.
         * Set to 0 to disable auto-dismiss.
         * @default 4500
         */
        this.duration = 4500;
        /**
         * The type of toast to display.
         * - default: Grey background (default)
         * - danger: Red background
         * - loading: With spinner animation
         * @default 'default'
         */
        this.type = 'default';
        /**
         * Whether the toast is currently visible.
         */
        this.isVisible = true;
        /**
         * Whether the toast is animating out.
         */
        this.isAnimatingOut = false;
    }
    componentDidLoad() {
        if (this.duration > 0) {
            this.startDismissTimer();
        }
    }
    disconnectedCallback() {
        this.cleanup();
    }
    handleDurationChange(newDuration) {
        if (this.dismissTimer) {
            window.clearTimeout(this.dismissTimer);
            this.dismissTimer = undefined;
        }
        if (newDuration > 0) {
            this.startDismissTimer();
        }
    }
    async dismiss() {
        // Start the animation out
        this.isAnimatingOut = true;
        await this.waitForDismissAnimation();
        this.isVisible = false;
        this.cleanup();
        this.pdsToastDismissed.emit({ componentId: this.componentId });
    }
    // Private methods
    waitForDismissAnimation() {
        const durationMs = this.getDismissAnimationDurationMs();
        if (durationMs <= 0) {
            return Promise.resolve();
        }
        return new Promise((resolve) => window.setTimeout(resolve, durationMs));
    }
    getDismissAnimationDurationMs() {
        if (typeof window === 'undefined') {
            return TOAST_DISMISS_ANIMATION_MS;
        }
        if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
            return 0;
        }
        const fromCss = this.parseCssDurationToMs(getComputedStyle(this.el).getPropertyValue('--animation-duration').trim());
        return fromCss !== null && fromCss !== void 0 ? fromCss : TOAST_DISMISS_ANIMATION_MS;
    }
    parseCssDurationToMs(value) {
        if (!value) {
            return undefined;
        }
        if (value === '0' || value === '0ms' || value === '0s') {
            return 0;
        }
        if (value.endsWith('ms')) {
            const ms = Number.parseFloat(value);
            return Number.isFinite(ms) ? ms : undefined;
        }
        if (value.endsWith('s')) {
            const seconds = Number.parseFloat(value);
            return Number.isFinite(seconds) ? seconds * 1000 : undefined;
        }
        return undefined;
    }
    cleanup() {
        if (this.dismissTimer) {
            window.clearTimeout(this.dismissTimer);
            this.dismissTimer = undefined;
        }
    }
    startDismissTimer() {
        this.dismissTimer = window.setTimeout(() => {
            this.dismiss();
        }, this.duration);
    }
    renderIcon() {
        // Loading type takes priority
        if (this.type === 'loading') {
            return (h("div", { class: "pds-toast__loader" }, h("svg", { class: "pds-toast__loader-spinner", viewBox: "25 25 50 50", "aria-hidden": "true" }, h("circle", { class: "pds-toast__loader-path", cx: "50", cy: "50", r: "20", fill: "none", stroke: "currentColor", "stroke-width": "4", "stroke-linecap": "round" }))));
        }
        // Return icon if provided, otherwise undefined (which renders as nothing)
        return this.icon && h("pds-icon", { name: this.icon, class: "pds-toast__icon" });
    }
    render() {
        return (h(Host, { key: '7768cbf398d4592b774734abdf5e9f1f4d4fb7e8', hidden: !this.isVisible }, h("div", { key: 'c17b00c76bd42d1040b8ef08a354e10346a79aa4', class: {
                'pds-toast': true,
                [`pds-toast--${this.type}`]: this.type !== 'default',
                'pds-toast--animating-out': this.isAnimatingOut
            }, role: "alert", "aria-live": "polite" }, this.renderIcon(), h("span", { key: 'cb7066fd49ae8674d5de80ecde21e63eecbcdf29', class: "pds-toast__message" }, h("slot", { key: '5c08d8b5cc54e02bed2906c2d52b894301fdca5d' })), this.dismissible && (h("button", { key: '4edd83a8521f76a2abddfe64caaac932962745ff', type: "button", part: "dismiss", class: "pds-toast__button", onClick: () => {
                this.dismiss();
            }, "aria-label": "Dismiss message" }, h("pds-icon", { key: 'c5cf65f889bfcd85e3fdab437539e7ee3f6e0465', name: "remove" }))))));
    }
    static get is() { return "pds-toast"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["pds-toast.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pds-toast.css"]
        };
    }
    static get properties() {
        return {
            "componentId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "A unique identifier used for the underlying component `id` attribute."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "component-id"
            },
            "dismissible": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "true"
                        }],
                    "text": "Whether the toast can be dismissed manually via the close button.\nNote: This only controls manual dismissal. Auto-dismissal via duration still applies."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "dismissible",
                "defaultValue": "true"
            },
            "duration": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "4500"
                        }],
                    "text": "The duration in milliseconds to show the toast before auto-dismissing.\nSet to 0 to disable auto-dismiss."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "duration",
                "defaultValue": "4500"
            },
            "icon": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The name of the icon to display in the toast."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "icon"
            },
            "type": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'default' | 'danger' | 'loading'",
                    "resolved": "\"danger\" | \"default\" | \"loading\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "default",
                            "text": "'default'"
                        }],
                    "text": "The type of toast to display.\n- default: Grey background (default)\n- danger: Red background\n- loading: With spinner animation"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "type",
                "defaultValue": "'default'"
            }
        };
    }
    static get states() {
        return {
            "isVisible": {},
            "isAnimatingOut": {}
        };
    }
    static get events() {
        return [{
                "method": "pdsToastDismissed",
                "name": "pdsToastDismissed",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event emitted when the toast is dismissed, either manually or automatically."
                },
                "complexType": {
                    "original": "{ componentId?: string }",
                    "resolved": "{ componentId?: string; }",
                    "references": {}
                }
            }];
    }
    static get methods() {
        return {
            "dismiss": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "duration",
                "methodName": "handleDurationChange"
            }];
    }
}
//# sourceMappingURL=pds-toast.js.map
