import { umbQueryMapperForJsonPaths } from '../../utils/query-mapper-json-paths.function.js';
import { UmbControllerBase } from '../../../../../libs/class-api/index.js';
import { createExtensionApi } from '../../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../../extension-registry/index.js';
// Write interface that can be handed to the API for the Host, so each path Translator can communicate back to the host here. For translating inner values.
export class UmbValidationPropertyPathTranslationController extends UmbControllerBase {
    /**
     * translates the property data.
     * @param {UmbPropertyValueDataPotentiallyWithEditorAlias} property - The property data.
     * @param paths
     * @param data
     * @param queryConstructor
     * @returns {Promise<UmbPropertyValueDataPotentiallyWithEditorAlias>} - A promise that resolves to the cloned property data.
     */
    async translateProperties(paths, data, queryConstructor) {
        return await umbQueryMapperForJsonPaths(paths, data, queryConstructor, async (scopedPaths, data) => {
            return data ? await this.translateProperty(scopedPaths, data) : scopedPaths;
        });
    }
    /**
     * translates the property data.
     * @param {Array<string>} propertyPaths - The paths to be translated.
     * @param {UmbPropertyValueDataPotentiallyWithEditorAlias} propertyData - The property data.
     * @returns {Promise<UmbPropertyValueDataPotentiallyWithEditorAlias>} - A promise that resolves to the cloned property data.
     */
    async translateProperty(propertyPaths, propertyData) {
        const editorAlias = propertyData.editorAlias;
        if (!editorAlias) {
            console.error(`Editor alias not found for ${propertyData.alias}`);
            return propertyPaths;
        }
        // Find the cloner for this editor alias:
        const manifest = umbExtensionsRegistry.getByTypeAndFilter('propertyValidationPathTranslator', (x) => x.forEditorAlias === editorAlias)[0];
        if (!manifest) {
            return propertyPaths;
        }
        const api = await createExtensionApi(this, manifest);
        if (!api) {
            return propertyPaths;
        }
        api.manifest = manifest;
        propertyPaths = (await api.translate(propertyPaths, propertyData)) ?? propertyPaths;
        return propertyPaths;
    }
}
