// #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 { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Invalidable } from '../../../Behaviors/Invalidable';
import { Orientable } from '../../../Behaviors/Orientable';
import { Valueable } from '../../../Behaviors/Valueable';
import { Variantable } from '../../../Behaviors/Variantable';
import { addInheritanceProviderController } from '../../../Controllers/InheritanceProviderController';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { Slottable } from '../../../Behaviors/Slottable';
import { Themeable } from '../../../Behaviors/Themeable';
import { CustomElement } from '../../Abstracts/CustomElement';
import { buttonGroupElementCosmopolitanStyle } from '../Button/Themes/ButtonGroupElement.Cosmopolitan';
import { buttonGroupElementJoyStyle } from '../Button/Themes/ButtonGroupElement.Joy';
import { buttonGroupElementMemphisStyle } from '../Button/Themes/ButtonGroupElement.Memphis';
import { ToggleButtonElement } from './ToggleButtonElement';
import { toggleButtonGroupElementTemplate } from './ToggleButtonGroupElementTemplate';
import { TOGGLE_BUTTON_GROUP_INHERITANCE_CONTEXT } from './ToggleButtonGroupInheritanceContext';
// #endregion
/**
 * Toggle Button Group - A container for grouping multiple toggle buttons with single-selection behavior.
 *
 * @description
 * Groups multiple Toggle Buttons to behave like a segmented control. Only one button can be selected at a time.
 * Inside a group, a toggle button cannot be deselected by clicking it again; clicking a selected button keeps it selected.
 * This component is useful for creating segmented controls, option selectors, and toggle-based navigation.
 *
 * @name Toggle Button Group
 * @element mosaik-toggle-button-group
 * @category Buttons
 *
 * @fires groupChanged {GroupChangedEvent} - Called when the selection in the group changes.
 *
 * @slot - The default slot for toggle buttons.
 *
 * @cssprop {String} --button-group-font-family - The font family.
 * @cssprop {String} --button-group-font-size - The font size.
 * @cssprop {String} --button-group-font-line-height - The font line height.
 * @cssprop {String} --button-group-font-weight - The font weight.
 * @cssprop {String} --button-group-font-letter-spacing - The font letter spacing.
 * @cssprop {String} --button-group-font-text-decoration - The font text decoration.
 * @cssprop {String} --button-group-font-text-transform - The font text transform.
 * @cssprop {String} --button-group-padding-top - The padding top.
 * @cssprop {String} --button-group-padding-right - The padding right.
 * @cssprop {String} --button-group-padding-bottom - The padding bottom.
 * @cssprop {String} --button-group-padding-left - The padding left.
 * @cssprop {String} --button-group-gap - The gap between buttons.
 * @cssprop {String} --button-group-transition-duration - The transition duration.
 * @cssprop {String} --button-group-transition-mode - The transition mode.
 * @cssprop {String} --button-group-transition-property - The transition property.
 * @cssprop {Color} --button-group-background-color - The background color.
 * @cssprop {Color} --button-group-border-color - The border color.
 * @cssprop {Color} --button-group-foreground-color - The foreground color.
 * @cssprop {String} --button-group-border-width - The border width.
 * @cssprop {String} --button-group-border-radius - The border radius.
 * @cssprop {String} --button-group-border-style - The border style.
 * @cssprop {String} --button-group-shadow - The shadow.
 *
 * @dependency {ToggleButtonElement} mosaik-toggle-button - The toggle button element.
 *
 * @example
 * ```html
 * <mosaik-toggle-button-group>
 *   <mosaik-toggle-button label="Option 1" value="1"></mosaik-toggle-button>
 *   <mosaik-toggle-button label="Option 2" value="2"></mosaik-toggle-button>
 *   <mosaik-toggle-button label="Option 3" value="3"></mosaik-toggle-button>
 * </mosaik-toggle-button-group>
 * ```
 *
 * @public
 */
let ToggleButtonGroupElement = class ToggleButtonGroupElement extends Themeable(Invalidable(Disableable(Orientable(Valueable(Appearanceable(Variantable(Slottable(CustomElement)))))))) {
    // #region Fields
    _provider = addInheritanceProviderController(this, {
        context: TOGGLE_BUTTON_GROUP_INHERITANCE_CONTEXT,
        values: () => ({
            disabled: this.disabled,
            appearance: this.appearance,
            variant: this.variant,
            dir: this.dir
        })
    });
    _toggleChanged;
    _eventSubscriptions;
    _isSwitching;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._eventSubscriptions = [];
        this._toggleChanged = new HTMLElementEventEmitter(this, 'groupChanged');
        this._isSwitching = 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-toggle-button-group';
    }
    /**
     * Called when the selection in the group changes.
     * Provides reference to `{@link IGroupChangedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get toggleChanged() {
        return this._toggleChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    onSlotChanges() {
        this.clearEvents();
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof ToggleButtonElement);
        for (const [i, e] of elements.entries()) {
            e.isChecked = this.value === e.value;
            e.classList.add('button-group-button');
            e.classList.toggle('first', i === 0);
            e.classList.toggle('inner', i > 0 && i < elements.length - 1);
            e.classList.toggle('last', i === elements.length - 1);
            this._eventSubscriptions.push(e.on('checked', (evt) => this.onToggleChecked(evt), { passive: true }));
            this._eventSubscriptions.push(e.on('unchecked', (evt) => this.onToggleUnchecked(evt), { passive: true }));
        }
    }
    /**
     * Returns whether a form will validate when it is submitted, without having to submit it.
     *
     * @public
     * @override
     */
    checkValidity() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof ToggleButtonElement)
            .filter((x) => x.isChecked);
        return elements.length > 0;
    }
    /**
     * Resets the element to its initial state.
     *
     * @public
     */
    reset() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof ToggleButtonElement);
        elements.forEach((x) => x.uncheck());
        this.invalid = false;
    }
    /**
     * Emits the {@link toggleChanged} event.
     *
     * @protected
     */
    onToggleChanged(args) {
        const value = args.target.value;
        this._toggleChanged.emit({ value });
    }
    /**
     * @protected
     */
    onValuePropertyChange(_prev, next) {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof ToggleButtonElement);
        elements.forEach((x) => x.isChecked = next === x.value);
    }
    /**
     * @private
     */
    onToggleChecked(event) {
        const target = event.target;
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof ToggleButtonElement);
        this._isSwitching = true;
        elements.filter((x) => x !== target).forEach((x) => x.uncheck());
        this._isSwitching = false;
        this.value = target.value;
        this.onToggleChanged(event);
    }
    /**
     * Prevent deselection of the currently selected button inside the group.
     *
     * @private
     */
    onToggleUnchecked(event) {
        if (this._isSwitching) {
            return;
        }
        const target = event.target;
        // If the unchecked target was the current selection, re-check it to enforce single-selection.
        if (this.value === target.value) {
            target.check();
        }
    }
    /**
     * @private
     */
    clearEvents() {
        this._eventSubscriptions.forEach((x) => x.dispose());
        this._eventSubscriptions = [];
    }
};
__decorate([
    Emit({ eventName: 'groupChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], ToggleButtonGroupElement.prototype, "toggleChanged", null);
__decorate([
    Watch('value'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", void 0)
], ToggleButtonGroupElement.prototype, "onValuePropertyChange", null);
ToggleButtonGroupElement = __decorate([
    Component({
        selector: 'mosaik-toggle-button-group',
        template: toggleButtonGroupElementTemplate,
        themes: {
            joy: buttonGroupElementJoyStyle,
            memphis: buttonGroupElementMemphisStyle,
            cosmopolitan: buttonGroupElementCosmopolitanStyle
        },
        host: {
            role: 'group'
        },
        imports: [ToggleButtonElement]
    }),
    __metadata("design:paramtypes", [])
], ToggleButtonGroupElement);
export { ToggleButtonGroupElement };
//# sourceMappingURL=ToggleButtonGroupElement.js.map