import { UmbExtensionCollectionRepository } from '../collection/repository/extension-collection.repository.js';
import { UmbExtensionItemRepository } from '../item/data/item.repository.js';
import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UmbObjectState } from '../../../../libs/observable-api/index.js';
import { getConfigValue } from '../../utils/index.js';
export class UmbExtensionPickerDataSource extends UmbControllerBase {
    constructor() {
        super(...arguments);
        this.#collectionRepository = new UmbExtensionCollectionRepository(this);
        this.#itemRepository = new UmbExtensionItemRepository(this);
        this.#supportsTextFilter = new UmbObjectState({ enabled: true });
        this.features = {
            supportsTextFilter: this.#supportsTextFilter.asObservable(),
        };
    }
    #collectionRepository;
    #itemRepository;
    #config;
    #supportsTextFilter;
    setConfig(config) {
        this.#config = config;
    }
    getConfig() {
        return this.#config;
    }
    async requestCollection(args) {
        const configAllowedExtensionTypes = getConfigValue(this.#config, 'allowedExtensionTypes');
        const requestedTypes = args.extensionTypes;
        let extensionTypes;
        if (configAllowedExtensionTypes?.length) {
            if (requestedTypes?.length) {
                // If the UI requests specific types, only use those that are in the allowed list
                extensionTypes = requestedTypes.filter((t) => configAllowedExtensionTypes.includes(t));
            }
            else {
                // No specific types requested — use all allowed types
                extensionTypes = configAllowedExtensionTypes;
            }
        }
        else {
            // No config restriction — pass through whatever the caller requested
            extensionTypes = requestedTypes;
        }
        const extendedArgs = {
            ...args,
            extensionTypes,
        };
        return this.#collectionRepository.requestCollection(extendedArgs);
    }
    async requestItems(uniques) {
        return this.#itemRepository.requestItems(uniques);
    }
}
export { UmbExtensionPickerDataSource as api };
