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;
};
import { css, customElement, html, nothing, property, repeat, state } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../lit-element/index.js';
let UmbDefaultTreeElement = class UmbDefaultTreeElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this._selectionConfiguration = {
            multiple: false,
            selectable: true,
            selection: [],
        };
        this.selectionConfiguration = this._selectionConfiguration;
        this.hideTreeItemActions = false;
        this.hideTreeRoot = false;
        this.expandTreeRoot = false;
        this.foldersOnly = false;
        this.isMenu = false;
        this.selectableFilter = () => true;
        this.filter = () => true;
        this.expansion = [];
        this._rootItems = [];
        this._currentPage = 1;
        this._hasPreviousItems = false;
        this._hasNextItems = false;
        this._isLoadingPrevChildren = false;
        this._isLoadingNextChildren = false;
    }
    get api() {
        return this._api;
    }
    set api(value) {
        this._api = value;
        this.#observeData();
    }
    #observeData() {
        this.observe(this._api?.treeRoot, (treeRoot) => (this._treeRoot = treeRoot));
        this.observe(this._api?.rootItems, (rootItems) => (this._rootItems = rootItems ?? []));
        this.observe(this._api?.pagination.currentPage, (value) => (this._currentPage = value ?? 1));
        this.observe(this._api?.isLoadingPrevChildren, (value) => (this._isLoadingPrevChildren = value ?? false));
        this.observe(this._api?.isLoadingNextChildren, (value) => (this._isLoadingNextChildren = value ?? false));
        this.observe(this._api?.targetPagination?.totalPrevItems, (value) => (this._hasPreviousItems = value ? value > 0 : false));
        this.observe(this._api?.targetPagination?.totalNextItems, (value) => (this._hasNextItems = value ? value > 0 : false));
    }
    async updated(_changedProperties) {
        super.updated(_changedProperties);
        if (this._api === undefined)
            return;
        if (_changedProperties.has('selectionConfiguration')) {
            this._selectionConfiguration = this.selectionConfiguration;
            this._api.selection.setMultiple(this._selectionConfiguration.multiple ?? false);
            this._api.selection.setSelectable(this._selectionConfiguration.selectable ?? true);
            this._api.selection.setSelection(this._selectionConfiguration.selection ?? []);
        }
        if (_changedProperties.has('startNode')) {
            this._api.setStartNode(this.startNode);
        }
        if (_changedProperties.has('hideTreeRoot')) {
            this._api.setHideTreeRoot(this.hideTreeRoot);
        }
        if (_changedProperties.has('expandTreeRoot')) {
            this._api.setExpandTreeRoot(this.expandTreeRoot);
        }
        if (_changedProperties.has('foldersOnly')) {
            this._api.setFoldersOnly(this.foldersOnly ?? false);
        }
        if (_changedProperties.has('selectableFilter')) {
            this._api.selectableFilter = this.selectableFilter;
        }
        if (_changedProperties.has('filter')) {
            this._api.filter = this.filter;
        }
        if (_changedProperties.has('expansion')) {
            this._api.setExpansion(this.expansion);
        }
    }
    getSelection() {
        return this._api?.selection.getSelection();
    }
    getExpansion() {
        return this._api?.expansion.getExpansion();
    }
    #onLoadPrev(event) {
        event.stopPropagation();
        this._api?.loadPrevItems?.();
    }
    #onLoadNext(event) {
        event.stopPropagation();
        const next = (this._currentPage = this._currentPage + 1);
        this._api?.pagination.setCurrentPageNumber(next);
    }
    render() {
        return html ` ${this.#renderTreeRoot()} ${this.#renderRootItems()}`;
    }
    #renderTreeRoot() {
        if (this.hideTreeRoot || this._treeRoot === undefined)
            return nothing;
        return html `
			<umb-tree-item
				.entityType=${this._treeRoot.entityType}
				.props=${{
            hideActions: this.hideTreeItemActions,
            item: this._treeRoot,
            isMenu: this.isMenu,
        }}></umb-tree-item>
		`;
    }
    #renderRootItems() {
        // only show the root items directly if the tree root is hidden
        if (this.hideTreeRoot === true) {
            return html `
				${this.#renderLoadPrevButton()}
				${repeat(this._rootItems, (item, index) => item.name + '___' + index, (item) => html `
						<umb-tree-item
							.entityType=${item.entityType}
							.props=${{ hideActions: this.hideTreeItemActions, item, isMenu: this.isMenu }}></umb-tree-item>
					`)}
				${this.#renderLoadNextButton()}
			`;
        }
        else {
            return nothing;
        }
    }
    #renderLoadPrevButton() {
        if (!this._hasPreviousItems)
            return nothing;
        return html `<umb-tree-load-prev-button
			@click=${this.#onLoadPrev}
			.loading=${this._isLoadingPrevChildren}></umb-tree-load-prev-button>`;
    }
    #renderLoadNextButton() {
        if (!this._hasNextItems)
            return nothing;
        return html `<umb-tree-load-more-button
			@click=${this.#onLoadNext}
			.loading=${this._isLoadingNextChildren}></umb-tree-load-more-button> `;
    }
    static { this.styles = css `
		#load-more {
			width: 100%;
		}
	`; }
};
__decorate([
    property({ type: Object, attribute: false })
], UmbDefaultTreeElement.prototype, "api", null);
__decorate([
    property({ type: Object, attribute: false })
], UmbDefaultTreeElement.prototype, "selectionConfiguration", void 0);
__decorate([
    property({ type: Boolean, attribute: false })
], UmbDefaultTreeElement.prototype, "hideTreeItemActions", void 0);
__decorate([
    property({ type: Boolean, attribute: false })
], UmbDefaultTreeElement.prototype, "hideTreeRoot", void 0);
__decorate([
    property({ type: Boolean, attribute: false })
], UmbDefaultTreeElement.prototype, "expandTreeRoot", void 0);
__decorate([
    property({ type: Object, attribute: false })
], UmbDefaultTreeElement.prototype, "startNode", void 0);
__decorate([
    property({ type: Boolean, attribute: false })
], UmbDefaultTreeElement.prototype, "foldersOnly", void 0);
__decorate([
    property({ type: Boolean, attribute: false })
], UmbDefaultTreeElement.prototype, "isMenu", void 0);
__decorate([
    property({ attribute: false })
], UmbDefaultTreeElement.prototype, "selectableFilter", void 0);
__decorate([
    property({ attribute: false })
], UmbDefaultTreeElement.prototype, "filter", void 0);
__decorate([
    property({ attribute: false })
], UmbDefaultTreeElement.prototype, "expansion", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_rootItems", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_treeRoot", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_currentPage", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_hasPreviousItems", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_hasNextItems", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_isLoadingPrevChildren", void 0);
__decorate([
    state()
], UmbDefaultTreeElement.prototype, "_isLoadingNextChildren", void 0);
UmbDefaultTreeElement = __decorate([
    customElement('umb-default-tree')
], UmbDefaultTreeElement);
export { UmbDefaultTreeElement };
export default UmbDefaultTreeElement;
