"use strict";
/* eslint-disable @typescript-eslint/ban-types */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const models_1 = require("../models");
const resetGlobalRule = (deployment, defaults, models, transaction) => __awaiter(void 0, void 0, void 0, function* () {
    yield models.IndexingRule.upsert(Object.assign(Object.assign({}, defaults), { allocationAmount: defaults.allocationAmount.toString(), deployment }), { transaction });
});
exports.default = {
    indexingRule: ({ deployment, merged }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const rule = yield models.IndexingRule.findOne({
            where: { deployment },
        });
        if (rule && merged) {
            return rule.mergeToGraphQL(yield models.IndexingRule.findOne({
                where: { deployment: models_1.INDEXING_RULE_GLOBAL },
            }));
        }
        else {
            return (rule === null || rule === void 0 ? void 0 : rule.toGraphQL()) || null;
        }
    }),
    indexingRules: ({ merged }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const rules = yield models.IndexingRule.findAll({
            order: [['deployment', 'DESC']],
        });
        if (merged) {
            const global = yield models.IndexingRule.findOne({
                where: { deployment: models_1.INDEXING_RULE_GLOBAL },
            });
            return rules.map((rule) => rule.mergeToGraphQL(global));
        }
        else {
            return rules.map((rule) => rule.toGraphQL());
        }
    }),
    setIndexingRule: ({ rule }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        yield models.IndexingRule.upsert(rule);
        // Since upsert succeeded, we _must_ have a rule
        const updatedRule = yield models.IndexingRule.findOne({
            where: { deployment: rule.deployment },
        });
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return updatedRule.toGraphQL();
    }),
    deleteIndexingRule: ({ deployment }, { models, defaults }) => __awaiter(void 0, void 0, void 0, function* () {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return yield models.IndexingRule.sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
            const numDeleted = yield models.IndexingRule.destroy({
                where: {
                    deployment,
                },
                transaction,
            });
            // Reset the global rule
            if (deployment === 'global') {
                yield resetGlobalRule(deployment, defaults.globalIndexingRule, models, transaction);
            }
            return numDeleted > 0;
        }));
    }),
    deleteIndexingRules: ({ deployments }, { models, defaults }) => __awaiter(void 0, void 0, void 0, function* () {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return yield models.IndexingRule.sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
            const numDeleted = yield models.IndexingRule.destroy({
                where: {
                    deployment: deployments,
                },
                transaction,
            });
            if (deployments.includes('global')) {
                yield resetGlobalRule('global', defaults.globalIndexingRule, models, transaction);
            }
            return numDeleted > 0;
        }));
    }),
};
//# sourceMappingURL=indexing-rules.js.map