// #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 { Attachable } from '../../../Behaviors/Attachable';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { focusRingElementTemplate } from './FocusRingElementTemplate';
import { focusRingElementJoyStyle } from './Themes/FocusRingElement.Joy';
import { focusRingElementRetroStyle } from './Themes/FocusRingElement.Retro';
import { focusRingElementCosmopolitanStyle } from './Themes/FocusRingElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
/**
 * Focus Ring - A visible indicator that highlights the currently focused element, aiding accessibility.
 *
 * @description
 * The Focus Ring element is a visible indicator that highlights the currently focused element, aiding accessibility.
 *
 * @name Focus Ring
 * @element mosaik-focus-ring
 * @category Primitives
 *
 * @cssprop {String} --focus-ring-inward-offset - The inward offset.
 * @cssprop {String} --focus-ring-outward-offset - The outward offset.
 * @cssprop {String} --focus-ring-width - The width.
 * @cssprop {String} --focus-ring-active-width - The active width.
 * @cssprop {String} --focus-ring-shape - The shape.
 * @cssprop {String} --focus-ring-x - The x.
 * @cssprop {String} --focus-ring-y - The y.
 * @cssprop {String} --focus-ring-blur - The blur.
 * @cssprop {String} --focus-ring-width - The width.
 * @cssprop {String} --focus-ring-color - The color.
 *
 * @public
 */
let FocusRingElement = class FocusRingElement extends Themeable(Attachable(Variantable(CustomElement))) {
    // #region Fields
    _focusInSubscription;
    _focusOutSubscription;
    _pointerDownSubscription;
    _visible;
    _inward;
    _controlled;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._visible = false;
        this._inward = false;
        this._controlled = 'auto';
        this._focusInSubscription = null;
        this._focusOutSubscription = null;
        this._pointerDownSubscription = 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-focus-ring';
    }
    /**
     * Gets or sets the `visible` property.
     *
     * @public
     * @attr
     */
    get visible() {
        return this._visible;
    }
    set visible(value) {
        if (this._visible !== value) {
            this._visible = value;
            this.requestUpdate('visible');
        }
    }
    /**
     * Gets or sets the `inward` property.
     *
     * @public
     * @attr
     */
    get inward() {
        return this._inward;
    }
    set inward(value) {
        if (this._inward !== value) {
            this._inward = value;
            this.requestUpdate('inward');
        }
    }
    /**
     * Gets or sets the `controlled` property.
     *
     * @public
     * @attr
     */
    get controlled() {
        return this._controlled;
    }
    set controlled(value) {
        if (this._controlled !== value) {
            this._controlled = value;
            this.requestUpdate('controlled');
        }
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @overload
     */
    connectedCallback() {
        super.connectedCallback();
        if (this.control) {
            this._focusInSubscription = on(this.control, 'focusin', (e) => this.handleEvent(e));
            this._focusOutSubscription = on(this.control, 'focusout', (e) => this.handleEvent(e));
            this._pointerDownSubscription = on(this.control, 'pointerdown', (e) => this.handleEvent(e));
        }
    }
    /**
     * @protected
     */
    onForPropertyChanged(prev, next) {
        if (prev && prev !== next) {
            this._focusInSubscription?.dispose();
            this._focusOutSubscription?.dispose();
            this._pointerDownSubscription?.dispose();
            if (next) {
                this._focusInSubscription = on(next, 'focusin', (e) => this.handleEvent(e));
                this._focusOutSubscription = on(next, 'focusout', (e) => this.handleEvent(e));
                this._pointerDownSubscription = on(next, 'pointerdown', (e) => this.handleEvent(e));
            }
        }
    }
    /**
     * @private
     */
    handleEvent(event) {
        if (this._controlled === 'manual') {
            return;
        }
        switch (event.type) {
            case 'focusin':
                this.visible = this.control?.matches(':focus-visible') ?? true;
                break;
            case 'focusout':
            case 'pointerdown':
                this.visible = false;
                break;
            default:
        }
    }
};
__decorate([
    Attribute({ type: Boolean, useDefault: true }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], FocusRingElement.prototype, "visible", null);
__decorate([
    Attribute({ type: Boolean, useDefault: true }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], FocusRingElement.prototype, "inward", null);
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], FocusRingElement.prototype, "controlled", null);
__decorate([
    Watch('for', { waitUntilFirstUpdate: true }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], FocusRingElement.prototype, "onForPropertyChanged", null);
FocusRingElement = __decorate([
    Component({
        selector: 'mosaik-focus-ring',
        template: focusRingElementTemplate,
        themes: {
            joy: focusRingElementJoyStyle,
            retro: focusRingElementRetroStyle,
            cosmopolitan: focusRingElementCosmopolitanStyle
        },
        imports: []
    }),
    __metadata("design:paramtypes", [])
], FocusRingElement);
export { FocusRingElement };
//# sourceMappingURL=FocusRingElement.js.map