// #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 { getSlotElements } from '../../../../Dom/Slot';
import { Emit } from '../../../../Events/Decorators/EmitDecorator';
import { HTMLElementEventEmitter } from '@breadstone/mosaik-elements';
import { Disableable } from '../../../Behaviors/Disableable';
import { Fitable } from '../../../Behaviors/Fitable';
import { Slottable } from '../../../Behaviors/Slottable';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { SelectorElement } from '../Abstracts/Selector/SelectorElement';
import { stepperElementTemplate } from './StepperElementTemplate';
import { StepperItemElement } from './StepperItemElement';
import { Themeable } from '../../../Behaviors/Themeable';
import { stepperElementJoyStyle } from './Themes/StepperElement.Joy';
import { stepperElementCosmopolitanStyle } from './Themes/StepperElement.Cosmopolitan';
import { stepperElementMemphisStyle } from './Themes/StepperElement.Memphis';
import { Orientable } from '../../../Behaviors/Orientable';
import { IconElement } from '../../Media/Icon/IconElement';
import { TextElement } from '../../Primitives/Text/TextElement';
// #endregion
/**
 * Stepper - A control that guides users through a workflow.
 *
 * @element mosaik-stepper
 * @category Selector
 *
 * @slot header - The header slot.
 * @slot steps - The steps slot.
 * @slot footer - The footer slot.
 *
 * @fires activeStepChanged - Fired when the active step has changed.
 *
 * @public
 */
let StepperElement = class StepperElement extends Themeable(Slottable(Fitable(Disableable(Orientable(SelectorElement))))) {
    // #region Fields
    _activeStepChanged;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._activeStepChanged = new HTMLElementEventEmitter(this, 'activeStepChanged');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     *
     * @public
     * @static
     * @override
     * @readonly
     */
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    static get is() {
        return 'mosaik-stepper';
    }
    /**
     * Returns the `items` property.
     *
     * @public
     * @override
     * @readonly
     */
    get items() {
        return getSlotElements(this.shadowRoot, 'steps');
    }
    /**
     * Gets the active index of the stepper.
     *
     * @public
     * @readonly
     */
    get activeIndex() {
        return this.items.findIndex((x) => x.isActive);
    }
    /**
     * Gets the first step indicator.
     *
     * @public
     * @readonly
     */
    get isFirstStep() {
        return this.activeIndex === 0;
    }
    /**
     * Gets the last step indicator.
     *
     * @public
     * @readonly
     */
    get isLastStep() {
        return this.activeIndex === this.items.length - 1;
    }
    /**
     * Called when the active step has changed.
     * Provides reference to `{@link IStepperActiveStepChangedEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get activeStepChanged() {
        return this._activeStepChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @protected
     * @template
     */
    onSlotChange() {
        this.requestUpdate('items');
        this.ensureActiveStep();
    }
    /**
     * Ensures that at least one step is active.
     * If no step is active, the first step will be selected.
     *
     * @protected
     */
    ensureActiveStep() {
        const { items } = this;
        if (items.length === 0) {
            return;
        }
        const hasActiveStep = items.some((x) => x.isActive);
        if (!hasActiveStep) {
            this.select(0);
        }
    }
    /**
     * Handles a step invocation from the header.
     *
     * @public
     * @param step - The step that has been invoked.
     */
    onStepInvoked(step) {
        if (this.disabled || step.disabled) {
            return;
        }
        const index = this.items.indexOf(step);
        if (index >= 0) {
            this.select(index);
        }
    }
    /**
     * @protected
     * @override
     */
    onSelectionChanged(oldIndex, newIndex) {
        if (oldIndex === newIndex) {
            return;
        }
        const items = this.items;
        const args = {
            cancel: false,
            oldStep: items[oldIndex] ?? null,
            oldStepIndex: oldIndex,
            newStep: items[newIndex],
            newStepIndex: newIndex,
            direction: newIndex > oldIndex ? 'forward' : 'backward',
            totalSteps: items.length
        };
        this.onActiveStepChanged(args);
        if (!args.cancel) {
            super.onSelectionChanged(oldIndex, newIndex);
        }
    }
    /**
     * Emits the {@link activeStepChanged} event.
     *
     * @protected
     */
    onActiveStepChanged(args) {
        this._activeStepChanged.emit(args);
    }
    /**
     * @protected
     */
    onSelectedIndexPropertyChanged(_prev, next) {
        const { items } = this;
        const { length } = items;
        for (let i = 0; i < length; i++) {
            const item = items[i];
            item.isActive = i === next;
        }
    }
};
__decorate([
    Emit({ eventName: 'activeStepChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], StepperElement.prototype, "activeStepChanged", null);
__decorate([
    Watch('selectedIndex'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Number]),
    __metadata("design:returntype", void 0)
], StepperElement.prototype, "onSelectedIndexPropertyChanged", null);
StepperElement = __decorate([
    Component({
        selector: 'mosaik-stepper',
        template: stepperElementTemplate,
        themes: {
            joy: stepperElementJoyStyle,
            memphis: stepperElementMemphisStyle,
            cosmopolitan: stepperElementCosmopolitanStyle
        },
        options: {
            mode: 'open',
            delegatesFocus: true
        },
        host: {
            tabIndex: '0',
            role: 'tablist'
        },
        imports: [
            StepperItemElement,
            IconElement,
            TextElement
        ]
    }),
    __metadata("design:paramtypes", [])
], StepperElement);
export { StepperElement };
//# sourceMappingURL=StepperElement.js.map