import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { createExtensionApi } from '../../../../libs/extension-api/index.js';
import { umbExtensionsRegistry } from '../../extension-registry/index.js';
export class UmbPropertyValueCloneController extends UmbControllerBase {
    /**
     * Clones the property data.
     * @param {UmbPropertyValueDataPotentiallyWithEditorAlias} property - The property data.
     * @returns {Promise<UmbPropertyValueDataPotentiallyWithEditorAlias>} - A promise that resolves to the cloned property data.
     */
    async clone(property) {
        const result = await this.#cloneProperty(property);
        this.destroy();
        return result ?? property;
    }
    async #cloneProperty(property) {
        const clonedProperty = await this.#cloneValue(property);
        return await this.#cloneInnerValues(clonedProperty);
    }
    async #cloneValue(incomingProperty) {
        const editorAlias = incomingProperty.editorAlias;
        if (!editorAlias) {
            console.error(`Editor alias not found for ${incomingProperty.alias}`);
            return incomingProperty;
        }
        // Find the cloner for this editor alias:
        const manifest = umbExtensionsRegistry.getByTypeAndFilter('propertyValueCloner', (x) => x.forEditorAlias === editorAlias)[0];
        if (!manifest) {
            return incomingProperty;
        }
        const api = await createExtensionApi(this, manifest);
        if (!api) {
            return incomingProperty;
        }
        api.manifest = manifest;
        let clonedProperty = incomingProperty;
        if (api.cloneValue) {
            const clonedValue = await api.cloneValue(incomingProperty.value);
            if (clonedValue) {
                clonedProperty = { ...incomingProperty, value: clonedValue };
            }
        }
        return clonedProperty;
    }
    async #cloneInnerValues(incomingProperty) {
        const editorAlias = incomingProperty.editorAlias;
        if (!editorAlias) {
            return incomingProperty;
        }
        // Find the resolver for this editor alias:
        const manifest = umbExtensionsRegistry.getByTypeAndFilter('propertyValueResolver', (x) => x.forEditorAlias === editorAlias)[0];
        if (!manifest) {
            return incomingProperty;
        }
        const api = await createExtensionApi(this, manifest);
        if (!api) {
            return incomingProperty;
        }
        api.manifest = manifest;
        if (api.processValues) {
            return ((await api.processValues(incomingProperty, async (properties) => {
                // Transform the values:
                const clonedValues = await Promise.all(properties.map(async (value) => {
                    return (await this.#cloneProperty(value)) ?? value;
                }));
                return clonedValues;
            })) ?? incomingProperty);
        }
        // the api did not provide a value processor, so we will return the incoming property:
        return incomingProperty;
    }
}
