import { PlatformServiceName, } from "@limetech/lime-web-components";
export class InfoTileLoader {
    constructor(platform, filters = []) {
        this.platform = platform;
        this.filters = filters;
    }
    async load(aggregator, context) {
        let value;
        if (this.isObjectIdentifier(context)) {
            value = await this.loadLimeObjectInfoTile(aggregator, context);
        }
        else {
            value = await this.loadBasicInfoTile(aggregator);
        }
        return value;
    }
    async loadBasicInfoTile(aggregator) {
        const filter = this.getFilter(aggregator.filter);
        if (!filter) {
            throw new Error(`Filter "${aggregator.filter}" not found`);
        }
        const result = await this.query.execute({
            limetype: filter.limetype,
            responseFormat: {
                aggregates: {
                    infotiles: {
                        value: {
                            op: aggregator.operator,
                            key: aggregator.name,
                        },
                    },
                },
            },
            filter: filter.filter,
        });
        return result.aggregates.infotiles[0].value;
    }
    async loadLimeObjectInfoTile(aggregator, identifier) {
        await this.objectRepository.loadObject(identifier.limetype, identifier.id, {
            properties: [aggregator],
        });
        const limeobject = this.objectRepository.getObject(identifier.limetype, identifier.id);
        return limeobject === null || limeobject === void 0 ? void 0 : limeobject.getValue([aggregator.name, aggregator.key].join('.'));
    }
    isObjectIdentifier(context) {
        return Boolean((context === null || context === void 0 ? void 0 : context.limetype) && (context === null || context === void 0 ? void 0 : context.id));
    }
    getFilter(id) {
        return this.filters.find((filter) => filter.id === id);
    }
    get query() {
        return this.platform.get(PlatformServiceName.Query);
    }
    get objectRepository() {
        return this.platform.get(PlatformServiceName.LimeObjectRepository);
    }
}
