// #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 { DomServiceLocator } from '../../../../Dom/Dom';
import { getSlotElements } from '../../../../Dom/Slot';
import { Listen } from '../../../../Events/Decorators/ListenDecorator';
import { Busyable } from '../../../Behaviors/Busyable';
import { Disableable } from '../../../Behaviors/Disableable';
import { Focusable } from '../../../Behaviors/Focusable';
import { Slottable } from '../../../Behaviors/Slottable';
import { Valueable } from '../../../Behaviors/Valueable';
import { Variantable } from '../../../Behaviors/Variantable';
import { Attribute } from '../../../Decorators/AttributeDecorator';
import { Component } from '../../../Decorators/ComponentDecorator';
import { Property } from '../../../Decorators/PropertyDecorator';
import { SelectionMode } from '../../../Types/SelectionMode';
import { ButtonElement } from '../../Buttons/Button/ButtonElement';
import { CheckmarkElement } from '../../Primitives/Checkmark/CheckmarkElement';
import { ExpandableElement } from '../../Primitives/Expandable/ExpandableElement';
import { SelectorItemElement } from '../Abstracts/Selector/SelectorItemElement';
import { TreeElement } from './TreeElement';
import { treeItemElementTemplate } from './TreeItemElementTemplate';
import { treeItemElementJoyStyle } from './Themes/TreeItemElement.Joy';
import { treeItemElementMemphisStyle } from './Themes/TreeItemElement.Memphis';
import { treeItemElementCosmopolitanStyle } from './Themes/TreeItemElement.Cosmopolitan';
import { Themeable } from '../../../Behaviors/Themeable';
// #endregion
/**
 * The `{@link TreeItemElement}` element.
 *
 * @public
 */
let TreeItemElement = class TreeItemElement extends Themeable(Disableable(Valueable(Variantable(Focusable(Slottable(Busyable(SelectorItemElement))))))) {
    // #region Fields
    _text;
    _isExpanded;
    _hasChildren;
    _isChecked;
    // #endregion
    // #region Ctor
    /**
     * @public
     */
    constructor() {
        super();
        this._text = '';
        this._hasChildren = false;
        this._isExpanded = false;
        this._isChecked = false;
    }
    // #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-tree-item';
    }
    /**
     * Gets or sets the `text` property.
     *
     * @public
     */
    get text() {
        return this._text;
    }
    set text(value) {
        if (this._text !== value) {
            this._text = value;
            this.requestUpdate('text');
        }
    }
    /**
     * Gets or sets the `isExpanded` property.
     *
     * @public
     * @attr
     */
    get isExpanded() {
        return this._isExpanded;
    }
    set isExpanded(value) {
        if (this._isExpanded !== value) {
            this._isExpanded = value;
            this.requestUpdate('isExpanded');
        }
    }
    /**
     * Gets or sets the `isChecked` property.
     *
     * @public
     * @attr
     */
    get isChecked() {
        return this._isChecked;
    }
    set isChecked(value) {
        if (this._isChecked !== value) {
            this._isChecked = value;
            this.requestUpdate('isChecked');
        }
    }
    /**
     * The `hasChildren` property represents whether the element has children or not.
     *
     * @public
     * @readonly
     * @attr
     */
    get hasChildren() {
        return this._hasChildren;
    }
    set hasChildren(value) {
        if (this._hasChildren !== value) {
            this._hasChildren = value;
            this.requestUpdate('hasChildren');
        }
    }
    /**
     * Get the item parent.
     *
     * @protected
     * @readonly
     */
    get parent() {
        return DomServiceLocator.current.findAncestor(this, TreeElement.is, { strict: true });
    }
    /**
     * Gets all the list items in the list.
     *
     * @public
     */
    get items() {
        const slotElements = getSlotElements(this.shadowRoot);
        return slotElements.flatMap((x) => x.hasChildren ? x.items : x);
    }
    // #endregion
    // #region Methods
    /**
     * Expands the tree item.
     *
     * @public
     */
    expand() {
        if (this.disabled) {
            return;
        }
        if (!this.hasChildren) {
            return;
        }
        this.isExpanded = true;
    }
    /**
     * Collapses the tree item.
     *
     * @public
     */
    collapse() {
        if (this.disabled) {
            return;
        }
        if (!this.hasChildren) {
            return;
        }
        this.isExpanded = false;
    }
    /**
     * Toggles the expanded state of the tree item.
     *
     * @public
     */
    toggle() {
        if (this.disabled) {
            return;
        }
        if (!this.hasChildren) {
            return;
        }
        this.isExpanded = !this.isExpanded;
    }
    /**
     * @public
     * @overload
     */
    onSlotChanges(slotName) {
        // this is the slot to react on child menu changes
        if (slotName === '') {
            this.hasChildren = this.hasSlotContent('');
        }
    }
    /**
     * @protected
     */
    onClick(event) {
        if (this.disabled) {
            return;
        }
        if (this === event.currentTarget) {
            if (!(this.parent.selectionMode === SelectionMode.Single || this.parent.selectionMode === SelectionMode.None)) {
                this.isChecked = !this._isChecked;
            }
        }
    }
};
__decorate([
    Property({ type: String }),
    __metadata("design:type", String),
    __metadata("design:paramtypes", [String])
], TreeItemElement.prototype, "text", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], TreeItemElement.prototype, "isExpanded", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], TreeItemElement.prototype, "isChecked", null);
__decorate([
    Attribute({
        type: Boolean,
        useDefault: true
    }),
    __metadata("design:type", Boolean),
    __metadata("design:paramtypes", [Boolean])
], TreeItemElement.prototype, "hasChildren", null);
__decorate([
    Listen('click'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Event]),
    __metadata("design:returntype", void 0)
], TreeItemElement.prototype, "onClick", null);
TreeItemElement = __decorate([
    Component({
        selector: 'mosaik-tree-item',
        template: treeItemElementTemplate,
        themes: {
            joy: treeItemElementJoyStyle,
            memphis: treeItemElementMemphisStyle,
            cosmopolitan: treeItemElementCosmopolitanStyle
        },
        imports: [
            ButtonElement,
            ExpandableElement,
            CheckmarkElement
        ]
    }),
    __metadata("design:paramtypes", [])
], TreeItemElement);
export { TreeItemElement };
//# sourceMappingURL=TreeItemElement.js.map