import { UMB_DOCUMENT_PICKER_MODAL, UMB_DOCUMENT_SEARCH_PROVIDER_ALIAS } from '../../constants.js';
import { UMB_DOCUMENT_ITEM_REPOSITORY_ALIAS } from '../../item/constants.js';
import { isDocumentTreeItem } from '../../tree/utils.js';
import { UmbDocumentItemDataResolver } from '../../item/index.js';
import { UmbPickerInputContext } from '../../../../core/picker-input/index.js';
import { UMB_VARIANT_CONTEXT } from '../../../../core/variant/index.js';
export class UmbDocumentPickerInputContext extends UmbPickerInputContext {
    constructor(host) {
        super(host, UMB_DOCUMENT_ITEM_REPOSITORY_ALIAS, UMB_DOCUMENT_PICKER_MODAL);
    }
    async openPicker(pickerData, args) {
        const combinedPickerData = {
            ...pickerData,
        };
        // combine internal allowedContentTypes filter with user-supplied pickableFilter
        combinedPickerData.pickableFilter = this._combinePickableFilters((item) => this.#pickableFilter(item, args?.allowedContentTypes), pickerData?.pickableFilter);
        // set default search data
        if (!pickerData?.search) {
            combinedPickerData.search = {
                providerAlias: UMB_DOCUMENT_SEARCH_PROVIDER_ALIAS,
                ...pickerData?.search,
            };
        }
        const variantContext = await this.getContext(UMB_VARIANT_CONTEXT);
        const culture = await variantContext?.getDisplayCulture();
        // pass allowedContentTypes to the search request args
        combinedPickerData.search.queryParams = {
            allowedContentTypes: args?.allowedContentTypes,
            includeTrashed: args?.includeTrashed,
            culture,
            ...pickerData?.search?.queryParams,
        };
        await super.openPicker(combinedPickerData);
    }
    async _requestItemName(unique) {
        const item = this.getSelectedItemByUnique(unique);
        const resolver = new UmbDocumentItemDataResolver(this);
        resolver.setData(item);
        const name = await resolver.getName();
        this.removeUmbController(resolver);
        return name ?? '#general_notFound';
    }
    #pickableFilter = (item, allowedContentTypes) => {
        // Check if the user has no access to this item (tree items only)
        if (isDocumentTreeItem(item) && item.noAccess) {
            return false;
        }
        if (allowedContentTypes && allowedContentTypes.length > 0) {
            return allowedContentTypes
                .map((contentTypeReference) => contentTypeReference.unique)
                .includes(item.documentType.unique);
        }
        return true;
    };
}
