import { UMB_BLOCK_ENTRIES_CONTEXT } from './block-entries.context-token.js';
import { UmbArrayState, UmbBasicState, UmbStringState } from '../../../../libs/observable-api/index.js';
import { UmbContextBase } from '../../../../libs/class-api/index.js';
export class UmbBlockEntriesContext extends UmbContextBase {
    getLength() {
        return this._layoutEntries.getValue().length;
    }
    constructor(host, blockManagerContextToken) {
        super(host, UMB_BLOCK_ENTRIES_CONTEXT.toString());
        this._catalogueRouteBuilderState = new UmbBasicState(undefined);
        this.catalogueRouteBuilder = this._catalogueRouteBuilderState.asObservable();
        this._workspacePath = new UmbStringState(undefined);
        this.workspacePath = this._workspacePath.asObservable();
        this._layoutEntries = new UmbArrayState([], (x) => x.contentKey);
        this.layoutEntries = this._layoutEntries.asObservable();
        this.layoutEntriesLength = this._layoutEntries.asObservablePart((x) => x.length);
        // TODO: Observe Blocks of the layout entries of this component.
        this._retrieveManager = this.consumeContext(blockManagerContextToken, (blockGridManager) => {
            this._manager = blockGridManager;
            this._gotBlockManager();
        }).asPromise({ preventTimeout: true });
    }
    async getManager() {
        await this._retrieveManager;
        return this._manager;
    }
    setDataPath(path) {
        this._dataPath = path;
    }
    // Public methods:
    layoutOf(contentKey) {
        return this._layoutEntries.asObservablePart((source) => source.find((x) => x.contentKey === contentKey));
    }
    getLayoutOf(contentKey) {
        return this._layoutEntries.getValue().find((x) => x.contentKey === contentKey);
    }
    setLayouts(layouts) {
        return this._layoutEntries.setValue(layouts);
    }
    setOneLayout(layoutData) {
        return this._layoutEntries.appendOne(layoutData);
    }
    async delete(contentKey) {
        await this._retrieveManager;
        const layout = this._layoutEntries.value.find((x) => x.contentKey === contentKey);
        if (!layout) {
            throw new Error(`Cannot delete block, missing layout for ${contentKey}`);
        }
        this._layoutEntries.removeOne(contentKey);
        this._manager.removeOneContent(contentKey);
        if (layout.settingsKey) {
            this._manager.removeOneSettings(layout.settingsKey);
        }
        this._manager.removeExposesOf(contentKey);
    }
    // insert/paste from property value methods:
    async _insertFromPropertyValues(values, originData) {
        await Promise.all(values.map(async (value) => {
            originData = await this._insertFromPropertyValue(value, originData);
        }));
    }
    async _insertBlockFromPropertyValue(layoutEntry, value, originData) {
        const content = value.contentData.find((x) => x.key === layoutEntry.contentKey);
        if (!content) {
            throw new Error('No content found for layout entry');
        }
        const settings = value.settingsData.find((x) => x.key === layoutEntry.settingsKey);
        await this.insert(layoutEntry, content, settings, originData);
    }
}
