"use strict";
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 });
exports.InputRequestDao = void 0;
const InputRequest_1 = require("../models/InputRequest");
const Run_1 = require("../models/Run");
const messageForm_1 = require("../../../shared/src/types/messageForm");
class InputRequestDao {
    static getInputRequestByRequestId(requestId) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                return yield InputRequest_1.InputRequestTable.findOne({
                    where: { requestId: requestId },
                });
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
    static saveInputRequest(inputRequest) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const newInputRequest = InputRequest_1.InputRequestTable.create(Object.assign({}, inputRequest));
                yield newInputRequest.save();
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
    static deleteInputRequest(inputRequestId) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const inputRequest = yield InputRequest_1.InputRequestTable.createQueryBuilder('input_request')
                    .where('input_request.requestId = :inputRequestId', {
                    inputRequestId,
                })
                    .getOne();
                if (inputRequest) {
                    yield inputRequest.remove();
                }
                else {
                    throw new Error(`InputRequest with id ${inputRequestId} not found`);
                }
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
    static deleteInputRequestsByRunId(runId) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const inputRequests = yield InputRequest_1.InputRequestTable.createQueryBuilder('input_request')
                    .where('input_request.runId = :runId', { runId })
                    .getMany();
                if (inputRequests.length > 0) {
                    yield InputRequest_1.InputRequestTable.remove(inputRequests);
                }
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
    static updateInputRequests() {
        return __awaiter(this, void 0, void 0, function* () {
            // 遍历所有的input requests，当对应的run处于Finished状态时，删除input request
            try {
                const inputRequests = yield InputRequest_1.InputRequestTable.createQueryBuilder('input_request').getMany();
                for (const inputRequest of inputRequests) {
                    const runId = inputRequest.runId;
                    const run = yield Run_1.RunTable.findOne({ where: { id: runId } });
                    if (run && run.status === messageForm_1.Status.DONE) {
                        yield InputRequestDao.deleteInputRequest(inputRequest.requestId);
                    }
                }
            }
            catch (error) {
                console.error(error);
                throw error;
            }
        });
    }
}
exports.InputRequestDao = InputRequestDao;
