// #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 { Emit } from '../../../../../events/Decorators/EmitDecorator';
import { Valueable } from '../../../../Behaviors/Valueable';
import { Attribute } from '../../../../Decorators/AttributeDecorator';
import { CustomElement } from '../../../Abstracts/CustomElement';
// #endregion
/**
 * Selector Item - An abstract foundation for individual selectable items within selector components.
 *
 * @description
 * The SelectorItemElement serves as the base class for items that can be selected or deselected
 * within parent selector components (single or multi-select). This element manages its own
 * selection state, provides value representation, tracks its position index within the parent
 * collection, and emits selection/deselection events for reactive UI updates. It is designed
 * to be extended by concrete implementations such as dropdown options, listbox items, menu items,
 * tab headers, radio buttons, or any selectable child element within a selector container.
 * The element integrates seamlessly with SelectorElement and MultiSelectorElement parent components,
 * providing bidirectional selection synchronization and comprehensive accessibility support.
 *
 * @name Selector Item
 * @category Selectors
 *
 * @fires selected {SelectedEvent} - Fired when the item is selected, either programmatically or through user interaction
 * @fires deselected {DeselectedEvent} - Fired when the item is deselected, either programmatically or through user interaction
 * @fires changed {PropertyChangedEvent} - Fired when any property changes
 * @fires connected {ConnectedEvent} - Fired when the element is connected to the DOM
 *
 * @example
 * Extending for a dropdown option:
 * ```typescript
 * class OptionElement extends SelectorItemElement<DropdownElement> {
 *   protected get parent(): DropdownElement {
 *     return this.closest('mosaik-dropdown') as DropdownElement;
 *   }
 * }
 * ```
 *
 * @example
 * Using selection state:
 * ```typescript
 * const item = document.querySelector('my-item');
 * item.select();           // Programmatically select
 * item.deselect();         // Programmatically deselect
 * console.log(item.isSelected);  // Check selection state
 * console.log(item.index);       // Get position in parent
 * console.log(item.value);       // Get item value
 * ```
 *
 * @example
 * Listening to selection events:
 * ```typescript
 * const item = document.querySelector('my-item');
 * item.addEventListener('selected', () => {
 *   console.log('Item selected:', item.displayText);
 * });
 * item.addEventListener('deselected', () => {
 *   console.log('Item deselected:', item.displayText);
 * });
 * ```
 *
 * @example
 * Declarative usage in HTML:
 * ```html
 * <mosaik-selector>
 *   <mosaik-item value="small" is-selected>Small</mosaik-item>
 *   <mosaik-item value="medium">Medium</mosaik-item>
 *   <mosaik-item value="large">Large</mosaik-item>
 * </mosaik-selector>
 * ```
 *
 * @public
 * @abstract
 */
export class SelectorItemElement extends Valueable(CustomElement) {
    // #region Fields
    _selected;
    _deselected;
    _isSelected;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._isSelected = false;
        this._selected = new HTMLElementEventEmitter(this, 'selected');
        this._deselected = new HTMLElementEventEmitter(this, 'deselected');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `is` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-selector-item';
    }
    /**
     * Gets or sets the `isSelected` property.
     *
     * @public
     * @attr
     */
    get isSelected() {
        return this._isSelected;
    }
    set isSelected(value) {
        if (this._isSelected !== value) {
            this._isSelected = value;
            this.requestUpdate('isSelected');
        }
    }
    /**
     * Gets the index of the item.
     *
     * @public
     * @readonly
     */
    get index() {
        return this.parent.items.indexOf(this);
    }
    /**
     * Gets the `displayText` property.
     *
     * @public
     * @readonly
     * @hidden
     */
    get displayText() {
        return this.textContent.trim();
    }
    /**
     * Called when the item is selected.
     * Provides reference to `IEventDetail` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get selected() {
        return this._selected;
    }
    /**
     * Called when the item is deselected.
     * Provides reference to `IEventDetail` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get deselected() {
        return this._deselected;
    }
    // #endregion
    // #region Methods
    /**
     * This method is invoked when the `isSelected` property is changed to `true`.
     *
     * @internal
     * @virtual
     */
    select(forceUpdate = false) {
        if (!this._isSelected) {
            this._isSelected = true;
            this._selected.emit();
            if (forceUpdate) {
                this.parent.select(this);
            }
        }
    }
    /**
     * This method is invoked when the `isSelected` property is changed to `false`.
     *
     * @internal
     * @virtual
     */
    deselect(forceUpdate = false) {
        if (this._isSelected) {
            this._isSelected = false;
            this._deselected.emit();
            if (forceUpdate) {
                this.parent.deselect(this);
            }
        }
    }
}
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], SelectorItemElement.prototype, "isSelected", null);
__decorate([
    Emit(),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], SelectorItemElement.prototype, "selected", null);
__decorate([
    Emit(),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], SelectorItemElement.prototype, "deselected", null);
//# sourceMappingURL=SelectorItemElement.js.map