import { UMB_COLLECTION_MENU_ITEM_CONTEXT } from '../collection-menu-item.context.token.js';
import { UMB_COLLECTION_MENU_CONTEXT } from '../../default/default-collection-menu.context.token.js';
import { UmbBooleanState, UmbObjectState } from '../../../../../../libs/observable-api/index.js';
import { UmbContextBase } from '../../../../../../libs/class-api/index.js';
import { map } from '../../../../../../external/rxjs/index.js';
export class UmbDefaultCollectionMenuItemContext extends UmbContextBase {
    #manifest;
    #isSelectable;
    #isSelectableContext;
    #isSelected;
    #collectionMenuContext;
    constructor(host) {
        super(host, UMB_COLLECTION_MENU_ITEM_CONTEXT);
        this._item = new UmbObjectState(undefined);
        this.item = this._item.asObservable();
        this.#isSelectable = new UmbBooleanState(false);
        this.isSelectable = this.#isSelectable.asObservable();
        this.#isSelectableContext = new UmbBooleanState(false);
        this.isSelectableContext = this.#isSelectableContext.asObservable();
        this.#isSelected = new UmbBooleanState(false);
        this.isSelected = this.#isSelected.asObservable();
        this.#consumeContexts();
    }
    async #consumeContexts() {
        this.consumeContext(UMB_COLLECTION_MENU_CONTEXT, (context) => {
            this.#collectionMenuContext = context;
            this.#observeIsSelectable();
            this.#observeIsSelected();
        });
    }
    set manifest(manifest) {
        if (this.#manifest === manifest)
            return;
        this.#manifest = manifest;
    }
    get manifest() {
        return this.#manifest;
    }
    setItem(item) {
        this._item.setValue(item);
        if (item) {
            this.#observeIsSelectable();
            this.#observeIsSelected();
        }
    }
    select() {
        const unique = this.getItem()?.unique;
        if (!unique)
            throw new Error('Could not select. Unique is missing');
        this.#collectionMenuContext?.selection.select(unique);
    }
    deselect() {
        const unique = this.getItem()?.unique;
        if (!unique)
            throw new Error('Could not deselect. Unique is missing');
        this.#collectionMenuContext?.selection.deselect(unique);
    }
    getItem() {
        return this._item.getValue();
    }
    #observeIsSelectable() {
        if (!this.#collectionMenuContext)
            return;
        const item = this.getItem();
        if (!item)
            return;
        this.observe(this.#collectionMenuContext.selection.selectable, (value) => {
            this.#isSelectableContext.setValue(value);
            // If the collection menu is selectable, check if this item is selectable
            if (value === true) {
                const isSelectable = this.#collectionMenuContext?.selectableFilter?.(item) ?? true;
                this.#isSelectable.setValue(isSelectable);
            }
        }, 'observeIsSelectable');
    }
    #observeIsSelected() {
        if (!this.#collectionMenuContext)
            return;
        const unique = this.getItem()?.unique;
        if (!unique)
            return;
        this.observe(this.#collectionMenuContext.selection.selection.pipe(map((selection) => selection.includes(unique))), (isSelected) => {
            this.#isSelected.setValue(isSelected);
        }, 'observeIsSelected');
    }
}
export { UmbDefaultCollectionMenuItemContext as api };
