import { UmbDataSourceDataMappingResolver } from './mapping/data-mapping-resolver.js';
import { UmbControllerBase } from '../../../../libs/class-api/index.js';
export class UmbDataSourceDataMapper extends UmbControllerBase {
    #dataMappingResolver = new UmbDataSourceDataMappingResolver(this);
    async map(args) {
        if (!args.forDataSource) {
            const message = 'data source identifier is required';
            console.error(message);
            throw new Error(message);
        }
        if (!args.data) {
            const message = 'data is required';
            console.error(message);
            throw new Error(message);
        }
        if (!args.forDataModel && !args.fallback) {
            const message = 'forDataModel is missing and no fallback provided.';
            console.error(message);
            throw new Error(message);
        }
        if (!args.forDataModel && args.fallback) {
            return args.fallback(args.data);
        }
        const dataMapping = await this.#dataMappingResolver.resolve(args.forDataSource, args.forDataModel);
        if (!dataMapping && !args.fallback) {
            const message = 'Data mapping not found and no fallback provided.';
            console.error(message);
            throw new Error(message);
        }
        if (!dataMapping && args.fallback) {
            return args.fallback(args.data);
        }
        if (!dataMapping?.map) {
            const message = 'Data mapping does not have a map method.';
            console.error(message);
            throw new Error(message);
        }
        return dataMapping.map(args.data);
    }
}
