/* eslint-disable @stylistic/padded-blocks */
/* eslint-disable @typescript-eslint/naming-convention */
// #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 { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { RendererServiceLocator } from '../../Dom/Renderer';
import { Emit } from '../../Events/Decorators/EmitDecorator';
import { Attribute } from '../Decorators/AttributeDecorator';
import { Watch } from '../Decorators/WatchDecorator';
/**
 * A mixin that describes a selector behavior.
 *
 * @public
 */
export const Selectable = (base) => {
    /**
     * @fires selectionChanged - Fired when the selection has changed.
     */
    class SelectableElement extends base {
        // #region Fields
        _selectionChanged;
        _isSelected;
        // #endregion
        // #region Ctor
        /**
         * Constructs a new instance of the `SelectableElement` class.
         *
         * @public
         */
        constructor(...args) {
            super(args);
            this._isSelected = false;
            this._selectionChanged = new HTMLElementEventEmitter(this, 'selectionChanged');
        }
        // #endregion
        // #region Properties
        /**
         * Gets or sets the `isSelected` property.
         * The default value is `false`, which means the element is not selected.
         *
         * @public
         * @attr
         */
        get isSelected() {
            return this._isSelected;
        }
        set isSelected(value) {
            if (this._isSelected !== value) {
                this._isSelected = value;
                this.requestUpdate('isSelected');
            }
        }
        /**
         * Called when the `isSelected` property has changed.
         * Provides reference to `IValueEventDetail` as event argument.
         *
         * @public
         * @readonly
         * @eventProperty
         */
        get selectionChanged() {
            return this._selectionChanged;
        }
        // #endregion
        // #region Methods
        /**
         * Selects the element.
         *
         * @public
         */
        select() {
            if (!this._isSelected) {
                this.isSelected = true;
            }
        }
        /**
         * Deselects the element.
         */
        deselect() {
            if (this._isSelected) {
                this.isSelected = false;
            }
        }
        /**
         * Called when the selection changes.
         * This method is a hook that can be overridden.
         *
         * @public
         */
        onSelectionChanges() {
            // This method is a hook that can be overridden.
        }
        /**
         * Emits the {@link selectionChanged} event.
         *
         * @protected
         */
        onSelectionChanged(args) {
            this._selectionChanged.emit(args);
        }
        /**
         * @protected
         */
        onIsSelectedPropertyChanged(prev, next) {
            if (next !== prev) {
                if (next) {
                    RendererServiceLocator.current.setAttribute(this, 'aria-selected', 'true');
                }
                else {
                    RendererServiceLocator.current.removeAttribute(this, 'aria-selected');
                }
                this.onSelectionChanged({ isSelected: next ?? false });
                this.onSelectionChanges();
            }
        }
    }
    __decorate([
        Attribute({
            type: Boolean,
            useDefault: true
        }),
        __metadata("design:type", Boolean),
        __metadata("design:paramtypes", [Boolean])
    ], SelectableElement.prototype, "isSelected", null);
    __decorate([
        Emit(),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], SelectableElement.prototype, "selectionChanged", null);
    __decorate([
        Watch('isSelected'),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Boolean, Boolean]),
        __metadata("design:returntype", void 0)
    ], SelectableElement.prototype, "onIsSelectedPropertyChanged", null);
    return SelectableElement;
};
// /**
//  * @public
//  */
// Declare global {
//     Interface HTMLElementEventMap {
//         SelectionChanged: CustomEvent<IValueEventDetail>;
//     }
// }
//# sourceMappingURL=Selectable.js.map