import { UMB_MEDIA_ITEM_REPOSITORY_ALIAS } from '../../constants.js';
import { UMB_MEDIA_PICKER_MODAL } from '../../modals/index.js';
import { UMB_MEDIA_SEARCH_PROVIDER_ALIAS } from '../../search/constants.js';
import { isMediaTreeItem } from '../../tree/utils.js';
import { UmbPickerInputContext } from '../../../../core/picker-input/index.js';
import { UmbMediaTypeStructureRepository } from '../../../media-types/index.js';
import { UMB_VARIANT_CONTEXT } from '../../../../core/variant/index.js';
export var UmbMediaPickerFolderFilter;
(function (UmbMediaPickerFolderFilter) {
    UmbMediaPickerFolderFilter["FILES_ONLY"] = "filesOnly";
    UmbMediaPickerFolderFilter["FOLDERS_ONLY"] = "foldersOnly";
    UmbMediaPickerFolderFilter["FILES_AND_FOLDERS"] = "filesAndFolders";
})(UmbMediaPickerFolderFilter || (UmbMediaPickerFolderFilter = {}));
export class UmbMediaPickerInputContext extends UmbPickerInputContext {
    #mediaTypeStructureRepository;
    #folderTypeUniques = new Set();
    #folderTypesPromise = null;
    constructor(host) {
        super(host, UMB_MEDIA_ITEM_REPOSITORY_ALIAS, UMB_MEDIA_PICKER_MODAL);
        this.#mediaTypeStructureRepository = new UmbMediaTypeStructureRepository(host);
    }
    async openPicker(pickerData, args) {
        // Load folder types before opening the picker so the filter is ready
        await this.#loadFolderTypes();
        const combinedPickerData = {
            ...pickerData,
        };
        // combine internal allowedContentTypes filter with user-supplied pickableFilter
        combinedPickerData.pickableFilter = this._combinePickableFilters((item) => this.#pickableFilter(item, args?.allowedContentTypes, args?.folderFilter), pickerData?.pickableFilter);
        // set default search data
        if (!pickerData?.search) {
            combinedPickerData.search = {
                providerAlias: UMB_MEDIA_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 #loadFolderTypes() {
        if (!this.#folderTypesPromise) {
            this.#folderTypesPromise = this.#mediaTypeStructureRepository.requestMediaTypesOfFolders().then((folderTypes) => {
                this.#folderTypeUniques = new Set(folderTypes.map((ft) => ft.unique).filter((u) => u != null));
            });
        }
        return this.#folderTypesPromise;
    }
    #pickableFilter = (item, allowedContentTypes, folderFilter = UmbMediaPickerFolderFilter.FILES_AND_FOLDERS) => {
        // Check if the user has no access to this item (tree items only)
        if (isMediaTreeItem(item) && item.noAccess) {
            return false;
        }
        const isFolder = this.#folderTypeUniques.has(item.mediaType.unique);
        // Apply folder filter
        if (folderFilter === UmbMediaPickerFolderFilter.FILES_ONLY && isFolder) {
            return false;
        }
        if (folderFilter === UmbMediaPickerFolderFilter.FOLDERS_ONLY && !isFolder) {
            return false;
        }
        // FILES_AND_FOLDERS — no folder-level filtering
        if (allowedContentTypes && allowedContentTypes.length > 0) {
            return allowedContentTypes
                .map((contentTypeReference) => contentTypeReference.unique)
                .includes(item.mediaType.unique);
        }
        return true;
    };
}
