"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRule = void 0;
const database_1 = require("./database");
const policy_1 = require("./policy");
async function hasCreatePermission(domain, context, databaseAdapter, options) {
    const allowed = await (0, policy_1.ruleHasPermission)(`create.${domain}`, context, databaseAdapter, options);
    if (allowed !== true)
        throw { message: 'You have insufficient permission to this resource' };
}
async function createMany(domain, ruleData, databaseAdapter, context, options) {
    const wOptions = { bypassDomainVerification: context?.useMasterKey === true, transaction: null };
    return (0, database_1.writeManyDataInStore)(domain, ruleData, context, databaseAdapter, wOptions, options);
}
async function createOne(domain, ruleData, databaseAdapter, context, options) {
    const wOptions = { bypassDomainVerification: context?.useMasterKey === true, transaction: null };
    return (0, database_1.writeOneDataInStore)(domain, ruleData, context, databaseAdapter, wOptions, options);
}
async function createRule(domain, ruleData, ruleResponse, databaseAdapter, context, options) {
    await hasCreatePermission(domain, context, databaseAdapter, options);
    let result;
    if (ruleData && Array.isArray(ruleData)) {
        result = await createMany(domain, ruleData, databaseAdapter, context, options);
    }
    else {
        result = await createOne(domain, ruleData, databaseAdapter, context, options);
    }
    ruleResponse[`create${domain}`] = result;
    return ruleResponse;
}
exports.createRule = createRule;
