import { map } from '../../../../external/rxjs/index.js';
import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UmbBooleanState } from '../../../../libs/observable-api/index.js';
import { umbExtensionsRegistry } from '../../extension-registry/index.js';
export class UmbTreeItemEntityActionManager extends UmbControllerBase {
    constructor() {
        super(...arguments);
        this.#hasActions = new UmbBooleanState(false);
        this.hasActions = this.#hasActions.asObservable();
    }
    #hasActions;
    #treeItem;
    #observerController;
    /**
     * Set the parent for which to load children.
     * @param {(TreeItemType | TreeRootType | undefined)} treeItem - The tree item model
     * @memberof UmbTreeItemChildrenManager
     */
    setTreeItem(treeItem) {
        // If we don't get a tree item stop the observation
        if (!treeItem) {
            this.#observerController?.destroy();
            return;
        }
        // If we get the same tree item again, continue with the same observation
        if (treeItem.entityType === this.#treeItem?.entityType && treeItem.unique === this.#treeItem?.unique) {
            return;
        }
        this.#treeItem = treeItem;
        this.#observeActionsForTreeItem(this.#treeItem);
    }
    /**
     * Gets the tree item for which to load children.
     * @returns {TreeItemType | TreeRootType | undefined} - The tree item for the children
     * @memberof UmbTreeItemChildrenManager
     */
    getTreeItem() {
        return this.#treeItem;
    }
    #observeActionsForTreeItem(treeItem) {
        this.#observerController = this.observe(umbExtensionsRegistry
            .byType('entityAction')
            .pipe(map((actions) => actions.filter((action) => action.forEntityTypes.includes(treeItem.entityType)))), (actions) => {
            this.#hasActions.setValue(actions.length > 0);
        }, 'observeActions');
    }
}
