import { UmbExtensionApiInitializer } from '../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../extension-registry/index.js';
import { UmbControllerBase } from '../../../../libs/class-api/index.js';
export class UmbGlobalSearchBase extends UmbControllerBase {
    constructor() {
        super(...arguments);
        this.#initialized = false;
        this._init = new Promise((resolve) => {
            if (this.#initialized) {
                resolve();
            }
            else {
                this.#initResolver = resolve;
            }
        });
    }
    #initResolver;
    #initialized;
    #checkIfInitialized() {
        if (this._searchProvider) {
            this.#initialized = true;
            this.#initResolver?.();
        }
    }
    set manifest(manifest) {
        if (this._manifest === manifest)
            return;
        this._manifest = manifest;
        this.#observeSearchProvider(this._manifest?.meta.searchProviderAlias);
    }
    get manifest() {
        return this._manifest;
    }
    #observeSearchProvider(alias) {
        if (!alias)
            throw new Error('Search provider alias is required');
        new UmbExtensionApiInitializer(this, umbExtensionsRegistry, alias, [this], (permitted, ctrl) => {
            this._searchProvider = permitted ? ctrl.api : undefined;
            this.#checkIfInitialized();
        });
    }
    async search(args) {
        await this._init;
        if (!this._searchProvider) {
            throw new Error('Search provider is not available');
        }
        return await this._searchProvider.search({ query: args.query });
    }
}
