// #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 { Themeable } from '../../../../Controls/Behaviors/Themeable';
import { fadeSlideIn, fadeSlideOut } from '../../../../Animations/Presets/Fade';
import { Animatable } from '../../../Behaviors/Animatable';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Dimensionable } from '../../../Behaviors/Dimensionable';
import { Elevatable } from '../../../Behaviors/Elevatable';
import { Slottable } from '../../../Behaviors/Slottable';
import { FocusTrapController } from '../../../Controllers/FocusTrapController';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { StackElement } from '../../Layouts/Stack/StackElement';
import { BackdropElement } from '../../Primitives/Backdrop/BackdropElement';
import { ElevationElement } from '../../Primitives/Elevation/ElevationElement';
import { OverlayElement } from '../Abstracts/OverlayElement';
import { DialogActionsElement } from './DialogActionsElement';
import { DialogContentElement } from './DialogContentElement';
import { dialogElementTemplate } from './DialogElementTemplate';
import { DialogFooterElement } from './DialogFooterElement';
import { DialogHeaderElement } from './DialogHeaderElement';
import { dialogElementMemphisStyle } from './Themes/DialogElement.Memphis';
import { dialogElementJoyStyle } from './Themes/DialogElement.Joy';
import { dialogElementCosmopolitanStyle } from './Themes/DialogElement.Cosmopolitan';
// #endregion
/**
 * Default dimensions for offsets and heights used for drag bounding
 */
export const DEFAULT_DIMENSIONS = {
    RIGHT_OFFSET: 8,
    TOP_OFFSET: 8,
    DRAG_BAR_HEIGHT: 32
};
/**
 * Dialog - A modal window component for displaying focused content and user interactions.
 *
 * Creates modal dialogs that overlay the main content with focus management and backdrop interaction.
 * Supports draggable positioning, fullscreen mode, and structured content areas for headers, content, and actions.
 * Provides essential modal patterns for confirmations, forms, and detailed content presentation.
 *
 * @name DialogElement
 * @element mosaik-dialog
 * @category Overlays
 *
 * @slot - Default content area for main dialog body
 * @slot header - Dialog header section for titles and controls
 * @slot footer - Dialog footer section for additional information
 * @slot content - Main content area for dialog body content
 * @slot actions - Action buttons and controls area
 *
 * @csspart dialog - The native HTML dialog container element
 * @csspart backdrop - The semi-transparent overlay backdrop
 * @csspart container - Main dialog content container
 * @csspart header - Header section container
 * @csspart content - Main content section container
 * @csspart footer - Footer section container
 * @csspart actions - Action buttons container
 *
 * @cssprop --dialog-width - Width of the dialog container
 * @cssprop --dialog-height - Height of the dialog container
 * @cssprop --dialog-max-width - Maximum width constraint for the dialog
 * @cssprop --dialog-max-height - Maximum height constraint for the dialog
 * @cssprop --dialog-background-color - Background color of the dialog
 * @cssprop --dialog-border-radius - Border radius for dialog corners
 * @cssprop --dialog-padding - Internal padding for dialog content
 * @cssprop --dialog-elevation - Elevation level for shadow effects
 * @cssprop --dialog-backdrop-color - Color of the backdrop overlay
 *
 * @dependency BackdropElement - Provides backdrop overlay functionality
 * @dependency StackElement - Layout container for dialog content
 * @dependency ElevationElement - Elevation and shadow effects
 * @dependency DialogHeaderElement - Structured header layout
 * @dependency DialogContentElement - Main content area layout
 * @dependency DialogFooterElement - Footer section layout
 * @dependency DialogActionsElement - Action buttons layout
 *
 * @example
 * ```html
 * <!-- Confirmation dialog -->
 * <mosaik-dialog id="confirmDialog">
 *   <mosaik-dialog-header slot="header">
 *     <h2>Confirm Delete</h2>
 *   </mosaik-dialog-header>
 *   <mosaik-dialog-content slot="content">
 *     <p>Are you sure you want to delete this item? This action cannot be undone.</p>
 *   </mosaik-dialog-content>
 *   <mosaik-dialog-actions slot="actions">
 *     <mosaik-button onclick="closeDialog()">Cancel</mosaik-button>
 *     <mosaik-button appearance="danger" onclick="confirmDelete()">Delete</mosaik-button>
 *   </mosaik-dialog-actions>
 * </mosaik-dialog>
 *
 * <!-- Form dialog -->
 * <mosaik-dialog draggable>
 *   <div slot="header">
 *     <h3>Add New User</h3>
 *   </div>
 *   <form slot="content">
 *     <mosaik-text-field label="Name" required></mosaik-text-field>
 *     <mosaik-text-field label="Email" type="email" required></mosaik-text-field>
 *     <mosaik-select label="Role" required>
 *       <option>User</option>
 *       <option>Admin</option>
 *     </mosaik-select>
 *   </form>
 *   <div slot="actions">
 *     <mosaik-button type="submit">Save</mosaik-button>
 *   </div>
 * </mosaik-dialog>
 *
 * <!-- Fullscreen dialog -->
 * <mosaik-dialog fullscreen>
 *   <header slot="header">
 *     <mosaik-toolbar>
 *       <mosaik-button slot="start" onclick="closeDialog()">Close</mosaik-button>
 *       <h1 slot="title">Settings</h1>
 *       <mosaik-button slot="end">Save</mosaik-button>
 *     </mosaik-toolbar>
 *   </header>
 *   <main slot="content">
 *     <!-- Full settings content -->
 *   </main>
 * </mosaik-dialog>
 * ```
 *
 * @public
 */
let DialogElement = class DialogElement extends Themeable(Animatable(Dimensionable(Elevatable(Appearanceable(Slottable(OverlayElement)))))) {
    // #region Fields
    _dialogElement;
    _headerElement;
    _isDraggable;
    _isFullScreen;
    _dragging;
    _offsetX;
    _offsetY;
    _dragStartPosition;
    _currentOffsetX;
    _previousOffsetX;
    _currentOffsetY;
    _previousOffsetY;
    _containerWidth;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        // this.height = '320px';
        // this.width = '320px';
        this._dragging = false;
        this._offsetX = 0;
        this._offsetY = 0;
        this._isDraggable = false;
        this._isFullScreen = false;
        this._dragStartPosition = {
            x: 0,
            y: 0
        };
        this._currentOffsetX = 0;
        this._previousOffsetX = 0;
        this._currentOffsetY = 0;
        this._previousOffsetY = 0;
        this._containerWidth = 0;
        this.enter = fadeSlideIn;
        this.exit = fadeSlideOut;
        new FocusTrapController(this);
    }
    // #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-dialog';
    }
    /**
     * Gets or sets the `isDraggable` property.
     *
     * @public
     * @attr
     */
    get isDraggable() {
        return this._isDraggable;
    }
    set isDraggable(value) {
        if (this._isDraggable !== value) {
            this._isDraggable = value;
            this.requestUpdate('isDraggable');
        }
    }
    /**
     * Gets or sets the `isFullScreen` property.
     *
     * @public
     * @attr
     */
    get isFullScreen() {
        return this._isFullScreen;
    }
    set isFullScreen(value) {
        if (this._isFullScreen !== value) {
            this._isFullScreen = value;
            this.requestUpdate('isFullScreen');
        }
    }
    /**
     * Gets or sets the `dragging` property.
     *
     * @public
     * @readonly
     * @attr
     */
    get dragging() {
        return this._dragging;
    }
    set dragging(value) {
        if (this._dragging !== value) {
            this._dragging = value;
            this.requestUpdate('dragging');
        }
    }
    /**
     * Gets or sets the `offsetX` property.
     *
     * @public
     * @readonly
     * @attr
     */
    get offsetX() {
        return this._offsetX;
    }
    set offsetX(value) {
        if (this._offsetX !== value) {
            this._offsetX = value;
            this.requestUpdate('offsetX');
        }
    }
    /**
     * Gets or sets the `offsetY` property.
     *
     * @public
     * @readonly
     * @attr
     */
    get offsetY() {
        return this._offsetY;
    }
    set offsetY(value) {
        if (this._offsetY !== value) {
            this._offsetY = value;
            this.requestUpdate('offsetY');
        }
    }
    /**
     * Gets the animation target element.
     * For the dialog, animations are applied to the root (dialog) template part instead of the host element.
     *
     * @public
     * @override
     * @readonly
     */
    get animationTarget() {
        return this.getTemplatePart('root');
    }
    // #endregion
    // #region Methods
    /**
     * Opens the `DialogElement`.
     *
     * @public
     * @override
     */
    async open() {
        return super.open().then(() => {
            void this.play(this.enter ?? { steps: [] });
        });
    }
    /**
     * Closes the `DialogElement`.
     *
     * @public
     * @override
     */
    async close() {
        return this.play(this.exit ?? { steps: [] }).then(() => super.close());
    }
    /**
     * @private
     * @template
     */
    onBackdrop() {
        if (this.clickOutsideToClose) {
            void this.close();
        }
    }
    /**
     * @private
     * @template
     */
    onDragStart(event) {
        if (!this._isDraggable) {
            return;
        }
        this._dragStartPosition = {
            x: event.x,
            y: event.y
        };
        this.dragging = true;
        this._headerElement.setPointerCapture(event.pointerId);
    }
    /**
     * @private
     * @template
     */
    onDrag(event) {
        if (!this._isDraggable || !this._dragging) {
            return;
        }
        const newX = this._previousOffsetX + event.x - this._dragStartPosition.x;
        const newY = this._previousOffsetY + event.y - this._dragStartPosition.y;
        // this.translatePos(newX, newY);
    }
    /**
     * @private
     * @template
     */
    onDragEnd(event) {
        if (!this._isDraggable) {
            return;
        }
        this.dragging = false;
        this._previousOffsetX = this._currentOffsetX;
        this._previousOffsetY = this._currentOffsetY;
        this._headerElement.releasePointerCapture(event.pointerId);
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._dialogElement = this.getTemplatePart('root');
        this._headerElement = this.getTemplatePart('header');
    }
    /**
     * @protected
     */
    onIsOpenPropertyChanged(_prev, next) {
        if (next) {
            this._dialogElement.show();
        }
        else {
            this._dialogElement.close();
        }
    }
    /**
     * @protected
     */
    onIsDraggablePropertyChanged(_prev, newValue) {
        if (newValue) {
            this._containerWidth = this.parentElement?.getBoundingClientRect().width ?? 0;
        }
        else {
            // TODO: 0 is wrong, `-50%` is correct
            // this.translatePos(0, 0);
            // this._previousOffsetX = 0;
            // this._previousOffsetY = 0;
        }
    }
    /**
     * @private
     */
    translatePos(x, y) {
        if (x === 0 && y === 0) {
            this.style.transform = '';
            return;
        }
        const rightBound = DEFAULT_DIMENSIONS.RIGHT_OFFSET;
        const leftBound = DEFAULT_DIMENSIONS.RIGHT_OFFSET + this._containerWidth - window.innerWidth;
        const topBound = -DEFAULT_DIMENSIONS.TOP_OFFSET;
        const bottomBound = window.innerHeight - (DEFAULT_DIMENSIONS.DRAG_BAR_HEIGHT + DEFAULT_DIMENSIONS.TOP_OFFSET);
        // do not allow drag outside right bound
        if (x > rightBound) {
            x = rightBound;
        }
        // do not allow drag outside left bound
        if (x < leftBound) {
            x = leftBound;
        }
        // do not allow drag outside top bound
        if (y < topBound) {
            y = topBound;
        }
        // do not allow drag outside bottom bound
        if (y > bottomBound) {
            y = bottomBound;
        }
        this._currentOffsetX = x;
        this._currentOffsetY = y;
        // this._dialogElement.style.transform = `translate(${x}px, ${y}px)`;
    }
};
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], DialogElement.prototype, "isDraggable", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], DialogElement.prototype, "isFullScreen", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], DialogElement.prototype, "dragging", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], DialogElement.prototype, "offsetX", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], DialogElement.prototype, "offsetY", null);
__decorate([
    Watch('isOpen', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], DialogElement.prototype, "onIsOpenPropertyChanged", null);
__decorate([
    Watch('isDraggable', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean, Boolean]),
    __metadata("design:returntype", void 0)
], DialogElement.prototype, "onIsDraggablePropertyChanged", null);
DialogElement = __decorate([
    Component({
        selector: 'mosaik-dialog',
        template: dialogElementTemplate,
        themes: {
            joy: dialogElementJoyStyle,
            memphis: dialogElementMemphisStyle,
            cosmopolitan: dialogElementCosmopolitanStyle
        },
        imports: [
            BackdropElement,
            StackElement,
            ElevationElement,
            DialogHeaderElement,
            DialogContentElement,
            DialogFooterElement,
            DialogActionsElement
        ]
    }),
    __metadata("design:paramtypes", [])
], DialogElement);
export { DialogElement };
//# sourceMappingURL=DialogElement.js.map