import { __esDecorate, __runInitializers } from "tslib";
import { html } from 'lit';
import { property, queryAssignedElements } from 'lit/decorators.js';
import { UiElement } from '../../UiElement.js';
let SegmentedButtonsSet = (() => {
    let _classSuper = UiElement;
    let _multiselect_decorators;
    let _multiselect_initializers = [];
    let _multiselect_extraInitializers = [];
    let _buttons_decorators;
    let _buttons_initializers = [];
    let _buttons_extraInitializers = [];
    return class SegmentedButtonsSet extends _classSuper {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _multiselect_decorators = [property({ type: Boolean })];
            _buttons_decorators = [queryAssignedElements({ flatten: true })];
            __esDecorate(this, null, _multiselect_decorators, { kind: "accessor", name: "multiselect", static: false, private: false, access: { has: obj => "multiselect" in obj, get: obj => obj.multiselect, set: (obj, value) => { obj.multiselect = value; } }, metadata: _metadata }, _multiselect_initializers, _multiselect_extraInitializers);
            __esDecorate(this, null, _buttons_decorators, { kind: "accessor", name: "buttons", static: false, private: false, access: { has: obj => "buttons" in obj, get: obj => obj.buttons, set: (obj, value) => { obj.buttons = value; } }, metadata: _metadata }, _buttons_initializers, _buttons_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        disabled = false;
        #multiselect_accessor_storage = __runInitializers(this, _multiselect_initializers, false);
        /**
         * Whether multiple buttons can be selected at once.
         * @attribute
         */
        get multiselect() { return this.#multiselect_accessor_storage; }
        set multiselect(value) { this.#multiselect_accessor_storage = value; }
        #buttons_accessor_storage = (__runInitializers(this, _multiselect_extraInitializers), __runInitializers(this, _buttons_initializers, void 0));
        get buttons() { return this.#buttons_accessor_storage; }
        set buttons(value) { this.#buttons_accessor_storage = value; }
        constructor() {
            super();
            __runInitializers(this, _buttons_extraInitializers);
            this.addEventListener('trigger', this.handleButtonTrigger.bind(this));
        }
        connectedCallback() {
            super.connectedCallback();
            this.setAttribute('role', 'group');
        }
        isButtonDisabled(index) {
            if (this.isOutOfBounds(index))
                return false;
            return this.buttons[index].disabled;
        }
        setButtonDisabled(index, disabled) {
            if (this.isOutOfBounds(index))
                return;
            this.buttons[index].disabled = disabled;
        }
        isButtonSelected(index) {
            if (this.isOutOfBounds(index))
                return false;
            return this.buttons[index].selected;
        }
        setButtonSelected(index, selected) {
            // Ignore out-of-index values.
            if (this.isOutOfBounds(index))
                return;
            // Ignore disabled buttons.
            if (this.isButtonDisabled(index))
                return;
            if (this.multiselect) {
                this.buttons[index].selected = selected;
                this.dispatchSelected(index);
                return;
            }
            // Single-select segmented buttons are not unselectable.
            if (!selected)
                return;
            this.buttons[index].selected = true;
            this.dispatchSelected(index);
            // Deselect all other buttons for single-select.
            for (let i = 0; i < this.buttons.length; i++) {
                if (i === index)
                    continue;
                this.buttons[i].selected = false;
            }
        }
        handleButtonTrigger(e) {
            const index = this.buttons.indexOf(e.target);
            this.toggleSelection(index);
        }
        toggleSelection(index) {
            this.setButtonSelected(index, !this.buttons[index].selected);
        }
        isOutOfBounds(index) {
            return index < 0 || index >= this.buttons.length;
        }
        dispatchSelected(index) {
            this.dispatchEvent(new CustomEvent('select', {
                detail: {
                    button: this.buttons[index],
                    selected: this.buttons[index].selected,
                    index,
                },
                bubbles: true,
                composed: true,
            }));
        }
        render() {
            return html `<slot></slot>`;
        }
    };
})();
/**
 * @fires select - When button's selection change.
 */
export default SegmentedButtonsSet;
//# sourceMappingURL=SegmentedButtonsSet.js.map