"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleAggregateRule = void 0;
const policy_1 = require("./policy");
const database_1 = require("./database");
async function hasAggregatePermission(domain, databaseAdapter, context, options) {
    const allowed = await (0, policy_1.ruleHasPermission)(`aggregate.${domain}`, context, databaseAdapter, options);
    if (allowed !== true)
        throw { message: 'You have insufficient permission to this resource' };
}
async function handleAggregateRule(domain, ruleData, ruleResponse, databaseAdapter, context, options) {
    await hasAggregatePermission(domain, databaseAdapter, context, options);
    const wOptions = { bypassDomainVerification: true, transaction: null };
    if (ruleData && ruleData.pipelines && Array.isArray(ruleData.pipelines)) {
        ruleResponse[`aggregate${domain}`] =
            await (0, database_1.aggregateDataInStore)(domain, ruleData.pipelines, databaseAdapter, wOptions, options);
    }
    else if (ruleData && Array.isArray(ruleData)) {
        ruleResponse[`aggregate${domain}`] =
            await (0, database_1.aggregateDataInStore)(domain, ruleData, databaseAdapter, wOptions, options);
    }
    else
        throw { message: 'A pipeline must be of any[] or {hashes:string[],pipelines: any[]}' };
    return ruleResponse;
}
exports.handleAggregateRule = handleAggregateRule;
