"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 sequelize_1 = require("sequelize");
exports.default = {
    dispute: ({ allocationID }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const dispute = yield models.POIDispute.findOne({
            where: { allocationID },
        });
        return (dispute === null || dispute === void 0 ? void 0 : dispute.toGraphQL()) || dispute;
    }),
    disputes: ({ status, minClosedEpoch }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const disputes = yield models.POIDispute.findAll({
            where: {
                [sequelize_1.Op.and]: [
                    { status },
                    {
                        closedEpoch: {
                            [sequelize_1.Op.gte]: minClosedEpoch,
                        },
                    },
                ],
            },
            order: [['allocationAmount', 'DESC']],
        });
        return disputes.map((dispute) => dispute.toGraphQL());
    }),
    storeDisputes: ({ disputes }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const createdDisputes = yield models.POIDispute.bulkCreate(disputes, {
            returning: true,
            validate: true,
            updateOnDuplicate: [
                'closedEpochReferenceProof',
                'previousEpochReferenceProof',
                'status',
            ],
        });
        return createdDisputes.map((dispute) => dispute.toGraphQL());
    }),
    deleteDisputes: ({ allocationIDs }, { models }) => __awaiter(void 0, void 0, void 0, function* () {
        const numDeleted = yield models.POIDispute.destroy({
            where: {
                allocationID: allocationIDs,
            },
            force: true,
        });
        return numDeleted;
    }),
};
//# sourceMappingURL=poi-disputes.js.map