// #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 SegmentElement_1;
import { Listen } from '../../../../events/Decorators/ListenDecorator';
import { Appearanceable } from '../../../Behaviors/Appearanceable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Slottable } from '../../../Behaviors/Slottable';
import { Themeable } from '../../../Behaviors/Themeable';
import { Variantable } from '../../../Behaviors/Variantable';
import { addInheritanceProviderController } from '../../../Controllers/InheritanceProviderController';
import { KeyboardController } from '../../../Controllers/KeyboardController';
import { ResizeController } from '../../../Controllers/ResizeController';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { FlowDirection } from '../../../Types/FlowDirection';
import { Key } from '../../../Types/Key';
import { CustomElement } from '../../Abstracts/CustomElement';
import { InkBarElement } from '../../Primitives/InkBar/InkBarElement';
import { segmentElementTemplate } from './SegmentElementTemplate';
import { SEGMENT_INHERITANCE_CONTEXT } from './SegmentInheritanceContext';
import { SegmentItemElement } from './SegmentItemElement';
import { segmentElementCosmopolitanStyle } from './Themes/SegmentElement.Cosmopolitan';
import { segmentElementJoyStyle } from './Themes/SegmentElement.Joy';
import { segmentElementMemphisStyle } from './Themes/SegmentElement.Memphis';
// #endregion
/**
 * Segment - A mutually exclusive button group for toggling between different views or options.
 *
 * @description
 * The Segment component (also known as a segmented control) displays a group of related buttons
 * arranged in a horizontal row where only one option can be selected at a time. It provides a
 * visual indicator that highlights the selected segment and supports keyboard navigation for
 * accessibility. Segments are ideal for switching between different views, filters, or modes
 * within the same context without navigating to a new page. The component automatically manages
 * selection state and includes an animated ink bar indicator to show the active segment.
 *
 * @name Segment
 * @element mosaik-segment
 * @category Selectors
 *
 * @slot - The default slot for segment items (mosaik-segment-item elements)
 *
 * @csspart indicator - The animated ink bar indicator element
 *
 * @cssprop {String} --segment-background-color - The background fill color of the segment container
 * @cssprop {String} --segment-border-color - The border color
 * @cssprop {String} --segment-border-width - The border thickness
 * @cssprop {String} --segment-border-radius - The corner rounding radius
 * @cssprop {String} --segment-border-style - The border line style
 * @cssprop {String} --segment-foreground-color - The text and icon color
 * @cssprop {String} --segment-gap - The spacing between segment items
 * @cssprop {String} --segment-padding-top - The top padding inside the segment container
 * @cssprop {String} --segment-padding-right - The right padding inside the segment container
 * @cssprop {String} --segment-padding-bottom - The bottom padding inside the segment container
 * @cssprop {String} --segment-padding-left - The left padding inside the segment container
 * @cssprop {String} --segment-indicator-height - The height of the selection indicator
 *
 * @dependency {InkBarElement} - Animated indicator bar for the selected segment
 *
 * @fires connected {ConnectedEvent} - Fired when connected to the DOM
 * @fires changed {PropertyChangedEvent} - Fired when properties change
 *
 * @example
 * Basic segment with three options:
 * ```html
 * <mosaik-segment>
 *   <mosaik-segment-item label="Day"></mosaik-segment-item>
 *   <mosaik-segment-item label="Week" checked></mosaik-segment-item>
 *   <mosaik-segment-item label="Month"></mosaik-segment-item>
 * </mosaik-segment>
 * ```
 *
 * @example
 * Segment with icons:
 * ```html
 * <mosaik-segment variant="primary">
 *   <mosaik-segment-item label="List" icon="list-icon"></mosaik-segment-item>
 *   <mosaik-segment-item label="Grid" icon="grid-icon" checked></mosaik-segment-item>
 *   <mosaik-segment-item label="Table" icon="table-icon"></mosaik-segment-item>
 * </mosaik-segment>
 * ```
 *
 * @example
 * Disabled segment:
 * ```html
 * <mosaik-segment disabled>
 *   <mosaik-segment-item label="Option 1"></mosaik-segment-item>
 *   <mosaik-segment-item label="Option 2"></mosaik-segment-item>
 * </mosaik-segment>
 * ```
 *
 * @public
 */
let SegmentElement = SegmentElement_1 = class SegmentElement extends Themeable(Slottable(Disableable(Variantable(Appearanceable(CustomElement))))) {
    // #region Fields
    _provider = addInheritanceProviderController(this, {
        context: SEGMENT_INHERITANCE_CONTEXT,
        values: () => ({
            disabled: this.disabled,
            variant: this.variant,
            dir: this.dir
        })
    });
    _indicatorElement;
    _resizeController;
    _keyboardController;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._indicatorElement = null;
        this._keyboardController = new KeyboardController(this, { capture: true });
        this._resizeController = new ResizeController(this, {
            callback: () => {
                const current = this.getCurrent();
                if (current) {
                    this.alignIndicatorTo(current);
                }
            }
        });
    }
    // #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-segment';
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    connectedCallback() {
        super.connectedCallback();
        this._keyboardController
            .add(Key.RightArrow, () => {
            const current = this.dir === FlowDirection.RightToLeft
                ? this.getItem('previous')
                : this.getItem('next');
            current?.check();
        }, (e) => this.getEnabledItems().includes(e?.target), { preventDefault: true })
            .add(Key.LeftArrow, () => {
            const current = this.dir === FlowDirection.RightToLeft
                ? this.getItem('next')
                : this.getItem('previous');
            current?.check();
        }, (e) => this.getEnabledItems().includes(e?.target), { preventDefault: true })
            .add(Key.Home, () => {
            const current = this.getItem('first');
            current?.check();
        }, (e) => this.getEnabledItems().includes(e?.target), { preventDefault: true })
            .add(Key.End, () => {
            const current = this.getItem('last');
            current?.check();
        }, (e) => this.getEnabledItems().includes(e?.target), { preventDefault: true })
            .add(Key.Enter, () => {
            const current = document.activeElement;
            current.check();
        }, (e) => this.getEnabledItems().includes(e?.target), { preventDefault: true });
    }
    /**
     * @public
     * @override
     */
    onSlotChanges(slotName) {
        super.onSlotChanges(slotName);
        const elements = this.getItems();
        for (const [i, e] of elements.entries()) {
            e.classList.toggle('first', i === 0);
            e.classList.toggle('inner', i > 0 && i < elements.length - 1);
            e.classList.toggle('last', i === elements.length - 1);
            if (e.isChecked) {
                this.alignIndicatorTo(e);
            }
        }
    }
    /**
     * @protected
     * @override
     */
    onApplyTemplate() {
        super.onApplyTemplate();
        this._indicatorElement = this.getTemplatePart('indicator');
    }
    /**
     * @protected
     */
    onClick(event) {
        if (event.target instanceof SegmentElement_1) {
            return;
        }
        const current = event.target;
        const previous = this.getCurrent();
        const others = this.getItems()
            .filter((x) => x !== current && x !== previous)
            .filter((x) => !x.isChecked);
        // this.value = current.value;
        if (previous && current !== previous) {
            previous.isChecked = false;
            others.forEach((x) => x.isChecked = false);
            current.isChecked = true;
            // this.checkButton(previous, current);
            this.alignIndicatorTo(current);
        }
        // if (this.scrollable || !this.swipeGesture) {
        //     if (previous) {
        //         this.checkButton(previous, current);
        //     } else {
        //         this.setCheckedClasses();
        //     }
        // }
    }
    /**
     * @protected
     */
    onChecked(event) {
        const current = event.target;
        // const others = this.getSlotAssigmments('')
        //     .filter((x): x is SegmentItemElement => x instanceof SegmentItemElement)
        //     .filter(x => x !== current)
        //     .find((x) => !x.isChecked);
        this.alignIndicatorTo(current);
    }
    onResize(event) {
        const current = this.getCurrent();
        if (current) {
            this.alignIndicatorTo(current);
        }
    }
    /**
     * @protected
     */
    onDisabledPropertyChange() {
        // Trigger context update for child elements
        this.requestUpdate();
    }
    /**
     * @protected
     */
    onVariantPropertyChange() {
        // Trigger context update for child elements
        this.requestUpdate();
    }
    /**
     * @protected
     */
    onDirPropertyChange() {
        // Trigger context update for child elements
        this.requestUpdate();
    }
    /**
     * @private
     */
    alignIndicatorTo(element) {
        if (this._indicatorElement) {
            this._indicatorElement.target = element;
            this._indicatorElement.align();
        }
    }
    /**
     * @private
     */
    getItems() {
        return this.getSlotAssignmentsOf('', SegmentItemElement);
    }
    /**
     * @private
     */
    getEnabledItems() {
        return this.getItems().filter((x) => !x.disabled);
    }
    /**
     * @private
     */
    getCurrent() {
        return this.getItems().find((x) => x.isChecked) ?? null;
    }
    /**
     * @private
     */
    getItem(selector) {
        const items = this.getItems().filter((item) => !item.disabled);
        const currIndex = items.findIndex((item) => item === document.activeElement);
        switch (selector) {
            case 'first':
                return items[0];
            case 'last':
                return items[items.length - 1];
            case 'next':
                return items[currIndex + 1] ?? items[0];
            case 'previous':
                return items[currIndex - 1] ?? items[items.length - 1];
            default:
                return null;
        }
    }
};
__decorate([
    Listen('click'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Event]),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onClick", null);
__decorate([
    Listen('checked'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CustomEvent]),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onChecked", null);
__decorate([
    Listen('resize', 'window'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [CustomEvent]),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onResize", null);
__decorate([
    Watch('disabled'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onDisabledPropertyChange", null);
__decorate([
    Watch('variant'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onVariantPropertyChange", null);
__decorate([
    Watch('dir'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SegmentElement.prototype, "onDirPropertyChange", null);
SegmentElement = SegmentElement_1 = __decorate([
    Component({
        selector: 'mosaik-segment',
        template: segmentElementTemplate,
        themes: {
            joy: segmentElementJoyStyle,
            memphis: segmentElementMemphisStyle,
            cosmopolitan: segmentElementCosmopolitanStyle
        },
        imports: [
            InkBarElement
        ]
    }),
    __metadata("design:paramtypes", [])
], SegmentElement);
export { SegmentElement };
//# sourceMappingURL=SegmentElement.js.map