// #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);
};
var CheckBoxGroupElement_1;
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Emit } from '../../../../events/Decorators/EmitDecorator';
import { EID } from '../../../../Utilities/EId';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Invalidable } from '../../../Behaviors/Invalidable';
import { Orientable } from '../../../Behaviors/Orientable';
import { Slottable } from '../../../Behaviors/Slottable';
import { Themeable } from '../../../Behaviors/Themeable';
import { Valueable } from '../../../Behaviors/Valueable';
import { Variantable } from '../../../Behaviors/Variantable';
import { addInheritanceProviderController } from '../../../Controllers/InheritanceProviderController';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { CustomElement } from '../../Abstracts/CustomElement';
import { CheckboxElement } from './CheckBoxElement';
import { checkBoxGroupElementTemplate } from './CheckBoxGroupElementTemplate';
import { CHECKBOX_GROUP_INHERITANCE_CONTEXT } from './CheckBoxGroupInheritanceContext';
import { checkBoxGroupElementCosmopolitanStyle } from './Themes/CheckBoxGroupElement.Cosmopolitan';
import { checkBoxGroupElementJoyStyle } from './Themes/CheckBoxGroupElement.Joy';
import { checkBoxGroupElementMemphisStyle } from './Themes/CheckBoxGroupElement.Memphis';
// #endregion
/**
 * Checkbox Group - A container for grouping multiple checkboxes.
 *
 * @description
 * The Checkbox Group component is used to organize and manage a set of checkboxes, allowing users to select multiple options from the group.
 * It typically contains multiple Checkbox components, each representing an individual option.
 * Checkbox Groups are commonly used when users need to select multiple items from a list or set of choices.
 *
 * @name CheckBoxGroup
 * @element mosaik-checkbox-group
 * @category Forms
 *
 * @slot  - The default slot.
 *
 * @cssprop {String} --check-box-group-background-color - The box group background color CSS custom property.
 * @cssprop {String} --check-box-group-border-color - The box group border color CSS custom property.
 * @cssprop {String} --check-box-group-border-radius - The box group border radius CSS custom property.
 * @cssprop {String} --check-box-group-border-style - The box group border style CSS custom property.
 * @cssprop {String} --check-box-group-border-width - The box group border width CSS custom property.
 * @cssprop {String} --check-box-group-font-family - The box group font family CSS custom property.
 * @cssprop {String} --check-box-group-font-letter-spacing - The box group font letter spacing CSS custom property.
 * @cssprop {String} --check-box-group-font-line-height - The box group font line height CSS custom property.
 * @cssprop {String} --check-box-group-font-size - The box group font size CSS custom property.
 * @cssprop {String} --check-box-group-font-text-decoration - The box group font text decoration CSS custom property.
 * @cssprop {String} --check-box-group-font-text-transform - The box group font text transform CSS custom property.
 * @cssprop {String} --check-box-group-font-weight - The box group font weight CSS custom property.
 * @cssprop {String} --check-box-group-foreground-color - The box group foreground color CSS custom property.
 * @cssprop {String} --check-box-group-gap - The box group gap CSS custom property.
 * @cssprop {String} --check-box-group-padding-bottom - The box group padding bottom CSS custom property.
 * @cssprop {String} --check-box-group-padding-left - The box group padding left CSS custom property.
 * @cssprop {String} --check-box-group-padding-right - The box group padding right CSS custom property.
 * @cssprop {String} --check-box-group-padding-top - The box group padding top CSS custom property.
 * @cssprop {String} --check-box-group-shadow - The box group shadow CSS custom property.
 * @cssprop {String} --check-box-group-shadow-blur - The box group shadow blur CSS custom property.
 * @cssprop {String} --check-box-group-shadow-color - The box group shadow color CSS custom property.
 * @cssprop {String} --check-box-group-shadow-offset-x - The box group shadow offset x CSS custom property.
 * @cssprop {String} --check-box-group-shadow-offset-y - The box group shadow offset y CSS custom property.
 * @cssprop {String} --check-box-group-shadow-spread - The box group shadow spread CSS custom property.
 * @cssprop {String} --check-box-group-transition-duration - The box group transition duration CSS custom property.
 * @cssprop {String} --check-box-group-transition-mode - The box group transition mode CSS custom property.
 * @cssprop {String} --check-box-group-transition-property - The box group transition property CSS custom property.
 * @cssprop {String} --check-box-group-translate - The box group translate CSS custom property.
 *
 * @fires checkChanged {GroupChangedEvent} - Called when the selection in the group changes.
 *
 * @example
 * Basic checkbox group:
 * ```html
 * <mosaik-checkbox-group name="interests">
 *   <mosaik-checkbox label="Sports" value="sports"></mosaik-checkbox>
 *   <mosaik-checkbox label="Music" value="music"></mosaik-checkbox>
 *   <mosaik-checkbox label="Technology" value="technology" is-checked></mosaik-checkbox>
 * </mosaik-checkbox-group>
 * ```
 *
 * @example
 * Horizontal checkbox group with variant:
 * ```html
 * <mosaik-checkbox-group name="options" orientation="horizontal" variant="primary">
 *   <mosaik-checkbox label="Option A" value="a"></mosaik-checkbox>
 *   <mosaik-checkbox label="Option B" value="b"></mosaik-checkbox>
 * </mosaik-checkbox-group>
 * ```
 *
 * @public
 */
let CheckBoxGroupElement = CheckBoxGroupElement_1 = class CheckBoxGroupElement extends Themeable(Invalidable(Disableable(Orientable(Valueable(Appearanceable(Variantable(Slottable(CustomElement)))))))) {
    // #region Fields
    _provider = addInheritanceProviderController(this, {
        context: CHECKBOX_GROUP_INHERITANCE_CONTEXT,
        values: () => ({
            disabled: this.disabled,
            appearance: this.appearance,
            variant: this.variant,
            name: this.name,
            dir: this.dir
        })
    });
    _checkChanged;
    _name;
    _required;
    _eventSubscriptions;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._name = EID.next(CheckBoxGroupElement_1.is);
        this._required = false;
        this._checkChanged = new HTMLElementEventEmitter(this, 'checkChanged');
        this._eventSubscriptions = new Array();
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `is` property.
     * The `is` property represents natural name of this element.
     *
     * @public
     * @static
     * @readonly
     */
    static get is() {
        return 'mosaik-checkbox-group';
    }
    /**
     * Gets or sets the `name` property.
     *
     * @public
     * @attr
     */
    get name() {
        return this._name;
    }
    set name(value) {
        if (this._name !== value) {
            this._name = value;
            this.requestUpdate('name');
        }
    }
    /**
     * Gets or sets the `required` property.
     *
     * @public
     * @attr
     */
    get required() {
        return this._required;
    }
    set required(value) {
        if (this._required !== value) {
            this._required = value;
            this.requestUpdate('required');
        }
    }
    /**
     * Called when the selection in the group changes.
     * Provides reference to `IGroupChangedEventDetail` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get checkChanged() {
        return this._checkChanged;
    }
    // #endregion
    // #region Methods
    /**
     * Checks all checkboxes in the group.
     *
     * @public
     */
    checkAll() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof CheckboxElement);
        elements.forEach((x) => x.check());
    }
    /**
     * Unchecks all checkboxes in the group.
     *
     * @public
     */
    uncheckAll() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof CheckboxElement);
        elements.forEach((x) => x.uncheck());
    }
    /**
     * 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 CheckboxElement)
            .filter((x) => x.isChecked ?? false);
        return elements.length > 0;
    }
    /**
     * @public
     * @override
     */
    onSlotChanges() {
        this._eventSubscriptions.forEach((x) => x.dispose());
        this._eventSubscriptions = [];
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof CheckboxElement);
        for (const [i, e] of elements.entries()) {
            e.isChecked = this.value === e.value;
            e.classList.add('checkbox-group-checkbox');
            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', () => this.onCheckBoxCheckChanged(), { passive: true }), e.on('unchecked', () => this.onCheckBoxCheckChanged(), { passive: true }));
        }
    }
    /**
     * Resets the element to its initial state.
     *
     * @public
     */
    reset() {
        this.uncheckAll();
        this.invalid = false;
    }
    /**
     * Emits the `checkChanged` event.
     *
     * @protected
     */
    onCheckChanged(args) {
        this._checkChanged.emit(args);
    }
    /**
     * @protected
     */
    onValuePropertyChange(_prev, next) {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof CheckboxElement);
        for (const e of elements) {
            e.isChecked = next?.includes(e.value) ?? false;
        }
    }
    /**
     * @private
     */
    onCheckBoxCheckChanged() {
        const elements = this.getSlotAssignments('')
            .filter((x) => x instanceof CheckboxElement);
        const checkedElements = elements.filter((x) => x.isChecked ?? false);
        const checkedValues = checkedElements.map((x) => x.value);
        this.value = checkedValues;
        this.onCheckChanged({ value: checkedValues });
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], CheckBoxGroupElement.prototype, "name", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], CheckBoxGroupElement.prototype, "required", null);
__decorate([
    Emit({ eventName: 'checkChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], CheckBoxGroupElement.prototype, "checkChanged", null);
__decorate([
    Watch('value'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Array, Array]),
    __metadata("design:returntype", void 0)
], CheckBoxGroupElement.prototype, "onValuePropertyChange", null);
CheckBoxGroupElement = CheckBoxGroupElement_1 = __decorate([
    Component({
        selector: 'mosaik-checkbox-group',
        template: checkBoxGroupElementTemplate,
        themes: {
            joy: checkBoxGroupElementJoyStyle,
            memphis: checkBoxGroupElementMemphisStyle,
            cosmopolitan: checkBoxGroupElementCosmopolitanStyle
        },
        host: {
            role: 'group'
        },
        imports: [
            CheckboxElement
        ]
    }),
    __metadata("design:paramtypes", [])
], CheckBoxGroupElement);
export { CheckBoxGroupElement };
//# sourceMappingURL=CheckBoxGroupElement.js.map