import { UmbControllerBase } from '../../../../../libs/class-api/index.js';
import { createExtensionApi } from '../../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../../../core/extension-registry/index.js';
export class UmbClipboardPastePropertyValueTranslatorValueResolver extends UmbControllerBase {
    #apiCache = new Map();
    async resolve(clipboardEntryValues, propertyEditorUiAlias) {
        if (!clipboardEntryValues.length) {
            throw new Error('Clipboard entry values are required.');
        }
        if (!propertyEditorUiAlias) {
            throw new Error('Property editor UI alias is required.');
        }
        const manifest = this.#getManifestWithBestFit(clipboardEntryValues, propertyEditorUiAlias);
        const pasteTranslator = await this.getPasteTranslator(clipboardEntryValues, propertyEditorUiAlias);
        const valueToTranslate = clipboardEntryValues.find((x) => x.type === manifest.fromClipboardEntryValueType);
        if (!valueToTranslate) {
            throw new Error(`Value to translate is missing`);
        }
        return pasteTranslator.translate(valueToTranslate.value);
    }
    /**
     * Get the paste translator for the given clipboard entry values and property editor ui alias
     * @param {UmbClipboardEntryValuesType} clipboardEntryValues
     * @param {string} propertyEditorUiAlias
     * @returns {Promise<UmbClipboardPastePropertyValueTranslator>} - The paste translator
     * @memberof UmbClipboardPastePropertyValueTranslatorValueResolver
     */
    async getPasteTranslator(clipboardEntryValues, propertyEditorUiAlias) {
        const manifest = this.#getManifestWithBestFit(clipboardEntryValues, propertyEditorUiAlias);
        // Check the cache before creating a new instance
        if (this.#apiCache.has(manifest.alias)) {
            return this.#apiCache.get(manifest.alias);
        }
        const pasteTranslator = await createExtensionApi(this, manifest);
        if (!pasteTranslator) {
            throw new Error('Failed to create paste translator.');
        }
        if (!pasteTranslator.translate) {
            throw new Error('Paste translator does not have a translate method.');
        }
        // Cache the api instance for future use
        this.#apiCache.set(manifest.alias, pasteTranslator);
        return pasteTranslator;
    }
    #getManifestWithBestFit(clipboardEntryValues, propertyEditorUiAlias) {
        const supportedManifests = this.#getSupportedManifests(clipboardEntryValues, propertyEditorUiAlias);
        if (!supportedManifests.length) {
            throw new Error('No paste translator found for the given property editor ui and entry value type.');
        }
        // Pick the manifest with the highest priority
        // TODO: This should have been handled in the extension registry, but until then we do it here: [NL]
        return supportedManifests.sort((a, b) => (b.weight || 0) - (a.weight || 0))[0];
    }
    #getSupportedManifests(clipboardEntryValues, propertyEditorUiAlias) {
        const entryValueTypes = clipboardEntryValues.map((x) => x.type);
        const supportedManifests = umbExtensionsRegistry.getByTypeAndFilter('clipboardPastePropertyValueTranslator', (manifest) => {
            const canTranslateValue = entryValueTypes.includes(manifest.fromClipboardEntryValueType);
            const supportsPropertyEditorUi = manifest.toPropertyEditorUi === propertyEditorUiAlias;
            return canTranslateValue && supportsPropertyEditorUi;
        });
        return supportedManifests;
    }
}
