// #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, waitForEvent } from '@breadstone/mosaik-elements';
import { Labelable } from '../Controls/Behaviors/Labelable';
import { CustomElement } from '../Controls/Components/Abstracts/CustomElement';
import { Attribute } from '../Controls/Decorators/AttributeDecorator';
import { Component } from '../Controls/Decorators/ComponentDecorator';
import { ScrollerServiceLocator } from '../Dom/Scroller';
import { Listen } from '../events/Decorators/ListenDecorator';
import { RouterServiceLocator } from './Router';
import { routerAnchorElementStyle } from './RouterAnchorElementStyle';
import { routerAnchorElementTemplate } from './RouterAnchorElementTemplate';
// #endregion
/**
 * RouterAnchor - Represents an anchor element for jump links within a page.
 *
 * @element mosaik-router-anchor
 *
 * @description
 * The RouterAnchor element is used to create jump links within a page. When clicked, it allows the page to scroll
 * smoothly to the specified anchor point. This is useful for single-page applications or long pages where users
 * need to navigate to different sections quickly.
 *
 * @category Routing
 *
 * @example
 * ```html
 * <mosaik-router-anchor ref="section1">Go to Section 1</mosaik-router-anchor>
 * ```
 *
 * @public
 */
let RouterAnchorElement = class RouterAnchorElement extends Labelable(CustomElement) {
    // #region Fields
    _anchorElement;
    _ref;
    _hasActivated;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._ref = '';
        this._hasActivated = false;
    }
    // #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-router-anchor';
    }
    /**
     * Gets or sets the `ref` property.
     *
     * @public
     * @attr
     */
    get ref() {
        return this._ref;
    }
    set ref(value) {
        if (this._ref !== value) {
            this._ref = value;
            this.requestUpdate('ref');
        }
    }
    /**
     * Gets or sets the `hasActivated` property.
     *
     * @public
     * @readonly
     * @attr
     */
    get hasActivated() {
        return this._hasActivated;
    }
    set hasActivated(value) {
        if (this._hasActivated !== value) {
            this._hasActivated = value;
            this.requestUpdate('hasActivated');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._anchorElement = this.getTemplatePart('anchor');
        on(this._anchorElement, 'click', (event) => {
            event.preventDefault();
            if (event.currentTarget) {
                const href = event.currentTarget.getAttribute('href') ?? '';
                const element = this.shadowRoot?.querySelector(href);
                void waitForEvent(RouterServiceLocator.current, 'navigated').then(() => {
                    ScrollerServiceLocator.current.scrollToElement(element, {
                        behavior: 'smooth',
                        block: 'start',
                        inline: 'nearest'
                    });
                });
                location.hash = href;
            }
        });
        void this.updateComplete.then(() => {
            const isCurrentHash = window.location.hash === `#${this.ref}`;
            if (isCurrentHash) {
                const element = this.shadowRoot?.querySelector(location.hash);
                setTimeout(() => {
                    ScrollerServiceLocator.current.scrollToElement(element, {
                        behavior: 'smooth',
                        block: 'start',
                        inline: 'nearest'
                    });
                }, 800);
                this.hasActivated = true;
            }
        });
        on(RouterServiceLocator.current, 'navigated', () => {
            const isCurrentHash = window.location.hash === `#${this.ref}`;
            this.hasActivated = isCurrentHash;
        });
    }
    /**
     * Click event listener.
     *
     * @protected
     */
    onClick() {
        // We need this click listener around the anchor element to make the anchor element clickable.
        // Afterwards we click logically the anchor element to use the default browser behavior.
        this._anchorElement.click();
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], RouterAnchorElement.prototype, "ref", null);
__decorate([
    Attribute({ type: Boolean,
        useDefault: true }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], RouterAnchorElement.prototype, "hasActivated", null);
__decorate([
    Listen('click'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], RouterAnchorElement.prototype, "onClick", null);
RouterAnchorElement = __decorate([
    Component({
        selector: 'mosaik-router-anchor',
        template: routerAnchorElementTemplate,
        styles: routerAnchorElementStyle,
        imports: []
    }),
    __metadata("design:paramtypes", [])
], RouterAnchorElement);
export { RouterAnchorElement };
//# sourceMappingURL=RouterAnchorElement.js.map