// #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 { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Watch } from '../../../Decorators/WatchDecorator';
import { ToolbarElement } from '../../Grouping/Toolbar/ToolbarElement';
import { SelectorElement } from '../Abstracts/Selector/SelectorElement';
import { wizardElementTemplate } from './WizardElementTemplate';
import { WizardStepElement } from './WizardStepElement';
import { wizardElementJoyStyle } from './Themes/WizardElement.Joy';
import { wizardElementRetroStyle } from './Themes/WizardElement.Retro';
import { wizardElementCosmopolitanStyle } from './Themes/WizardElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
import { TitleLayoutElement } from '../../Layouts/Title/TitleLayoutElement';
// #endregion
/**
 * Wizard - A component that allows users to navigate through steps to complete a task.
 *
 * @element mosaik-wizard
 * @category Selector
 *
 * @slot header - The header slot.
 * @slot header-before - The header before slot.
 * @slot header-after - The header after slot.
 * @slot steps - The steps slot.
 * @slot footer - The footer slot.
 *
 * @fires activeStepChanged - Fired when the active step has changed.
 *
 * @public
 */
let WizardElement = class WizardElement extends Themeable(Slottable(Fitable(Disableable(SelectorElement)))) {
    // #region Fields
    _activeStepChanged;
    _name;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._name = '';
        this._activeStepChanged = new HTMLElementEventEmitter(this, 'activeStepChanged');
    }
    // #endregion
    // #region Properties
    /**
     * Returns the `{@link is}` property.
     * The `{@link is}` property represents natural name of this element.
     *
     * @public
     * @static
     * @override
     * @readonly
     */
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    static get is() {
        return 'mosaik-wizard';
    }
    /**
     * 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');
        }
    }
    /**
     * Returns the `items` property.
     *
     * @public
     * @override
     * @readonly
     */
    get items() {
        return getSlotElements(this.shadowRoot, 'steps');
    }
    /**
     * Gets the active index of the wizard.
     *
     * @public
     * @readonly
     */
    get activeIndex() {
        return this.items.findIndex((x) => x.isActive);
    }
    /**
     * Gets the first step of the wizard.
     *
     * @public
     * @readonly
     */
    get isFirstStep() {
        return this.activeIndex === 0;
    }
    /**
     * Gets the last step of the wizard.
     *
     * @public
     * @readonly
     */
    get isLastStep() {
        return this.activeIndex === this.items.length - 1;
    }
    /**
     * Called when the active step has changed.
     * Provides reference to `{@link IEventDetail}` as event detail.
     *
     * @public
     * @readonly
     * @eventProperty
     */
    get activeStepChanged() {
        return this._activeStepChanged;
    }
    // #endregion
    // #region Methods
    /**
     * @protected
     * @override
     */
    onSelectionChanged(oldIndex, newIndex) {
        if (oldIndex === newIndex) {
            return;
        }
        const { items } = this;
        const args = {
            cancel: false,
            oldStep: items[oldIndex],
            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;
        }
    }
    /**
     * @protected
     * @template
     */
    onSlotChange() {
        this.requestUpdate('items');
    }
};
__decorate([
    Attribute({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], WizardElement.prototype, "name", null);
__decorate([
    Emit({ eventName: 'activeStepChanged' }),
    __metadata("design:type", Object),
    __metadata("design:paramtypes", [])
], WizardElement.prototype, "activeStepChanged", null);
__decorate([
    Watch('selectedIndex'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Number]),
    __metadata("design:returntype", void 0)
], WizardElement.prototype, "onSelectedIndexPropertyChanged", null);
WizardElement = __decorate([
    Component({
        selector: 'mosaik-wizard',
        template: wizardElementTemplate,
        themes: {
            joy: wizardElementJoyStyle,
            retro: wizardElementRetroStyle,
            cosmopolitan: wizardElementCosmopolitanStyle
        },
        options: {
            mode: 'open',
            delegatesFocus: true
        },
        host: {
            tabIndex: '0',
            role: 'tablist'
        },
        imports: [
            WizardStepElement,
            ToolbarElement,
            TitleLayoutElement
        ]
    }),
    __metadata("design:paramtypes", [])
], WizardElement);
export { WizardElement };
//# sourceMappingURL=WizardElement.js.map