/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// #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 { Attachable } from '../../../Behaviors/Attachable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { perspectiveElementTemplate } from './PerspectiveElementTemplate';
import { perspectiveElementJoyStyle } from './Themes/PerspectiveElement.Joy';
import { perspectiveElementRetroStyle } from './Themes/PerspectiveElement.Retro';
import { perspectiveElementCosmopolitanStyle } from './Themes/PerspectiveElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { waitForEvent, on } from '@breadstone/mosaik-elements';
import { CssTime } from '@breadstone/mosaik-themes';
// #endregion
/**
 * Perspective - A component that manages the perspective effect for 3D transformations.
 *
 * @description
 * The Perspective component allows you to apply a perspective effect to elements, giving them a 3D appearance by controlling the distance between the user and the object. This is useful for creating depth and enhancing visual presentations in web applications.
 *
 * @name Perspective
 * @element mosaik-perspective
 * @category Effects
 *
 * @example
 * ```html
 * <div is="perspective-element">
 *   <div class="box">3D Content</div>
 * </div>
 * ```
 *
 * @public
 */
let PerspectiveElement = class PerspectiveElement extends Themeable(Disableable(Attachable(CustomElement))) {
    // #region Fields
    _duration;
    _mouseMoveSubscription;
    _mouseLeaveSubscription;
    _factor;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._duration = 600;
        this._factor = 8;
        this._mouseMoveSubscription = null;
        this._mouseLeaveSubscription = null;
    }
    // #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-perspective';
    }
    /**
     * Gets or sets the `duration` property.
     *
     * @public
     * @attr
     */
    get duration() {
        return this._duration;
    }
    set duration(value) {
        if (this._duration !== value) {
            this._duration = value;
            this.requestUpdate('duration');
        }
    }
    /**
     * Gets or sets the `factor` property.
     *
     * @public
     * @attr
     */
    get factor() {
        return this._factor;
    }
    set factor(value) {
        if (this._factor !== value) {
            this._factor = value;
            this.requestUpdate('factor');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    disconnectedCallback() {
        super.disconnectedCallback();
        this._mouseMoveSubscription?.dispose();
        this._mouseLeaveSubscription?.dispose();
    }
    /**
     * @public
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        if (this.control) {
            this._mouseMoveSubscription = on(this.control, 'mousemove', (e) => this.onMouseMove(e));
            this._mouseLeaveSubscription = on(this.control, 'mouseleave', (e) => this.onMouseLeave(e));
        }
    }
    /**
     * @private
     */
    onMouseMove(event) {
        const rect = event.currentTarget.getBoundingClientRect();
        const clientWidth = event.currentTarget.clientWidth;
        const clientHeight = event.currentTarget.clientHeight;
        const clientX = event.clientX;
        const clientY = event.clientY;
        const horizontal = (clientX - rect.left) / clientWidth;
        const vertical = (clientY - rect.top) / clientHeight;
        const rotateX = ((this._factor / 2) - (horizontal * this._factor)).toFixed(2);
        const rotateY = ((vertical * this._factor) - (this._factor / 2)).toFixed(2);
        const animationProps = {
            transform: `perspective(${clientWidth}px) rotateX(${rotateY}deg) rotateY(${rotateX}deg) scale3d(1, 1, 1)`,
            curve: 'easeInOut'
        };
        const animation = this.control?.animate(animationProps, {
            duration: this.duration
        });
        if (animation) {
            void waitForEvent(animation, 'finish').then(() => {
                this.applyAnimation(animationProps);
            });
            animation.play();
        }
    }
    /**
     * @private
     */
    onMouseLeave(event) {
        const clientWidth = event.target.clientWidth;
        const animationProps = {
            transform: `perspective(${clientWidth}px) rotateX(0deg) rotateY(0deg)`,
            curve: 'easeInOut'
        };
        const animation = this.control?.animate(animationProps, {
            duration: this.duration
        });
        if (animation) {
            void waitForEvent(animation, 'finish').then(() => {
                this.applyAnimation(animationProps);
            });
            animation.play();
        }
    }
    /**
     * Applies the animation as style to the element.
     *
     * @private
     */
    applyAnimation(prop) {
        if (!this.control) {
            return;
        }
        Object.entries(prop).forEach(([k, v]) => {
            if (k in this.control.style) {
                this.control.style.setProperty(k, v?.toString() ?? '');
            }
        });
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [Object])
], PerspectiveElement.prototype, "duration", null);
__decorate([
    Attribute({ type: Number }),
    __metadata("design:type", Number),
    __metadata("design:paramtypes", [Number])
], PerspectiveElement.prototype, "factor", null);
PerspectiveElement = __decorate([
    Component({
        selector: 'mosaik-perspective',
        template: perspectiveElementTemplate,
        themes: {
            joy: perspectiveElementJoyStyle,
            retro: perspectiveElementRetroStyle,
            cosmopolitan: perspectiveElementCosmopolitanStyle
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], PerspectiveElement);
export { PerspectiveElement };
//# sourceMappingURL=PerspectiveElement.js.map