"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const User_1 = __importDefault(require("../../models/User"));
const AppError_1 = __importDefault(require("../../errors/AppError"));
const UpdateDeletedUserOpenTicketsStatus_1 = __importDefault(require("../../helpers/UpdateDeletedUserOpenTicketsStatus"));
const RedisClient_1 = require("../../helpers/RedisClient");
const DeleteUserService = (id, ignoreThrow = false) => __awaiter(void 0, void 0, void 0, function* () {
    var _a;
    const user = yield User_1.default.findOne({
        where: { id }
    });
    if (!user) {
        if (ignoreThrow)
            return {
                error: true,
                msg: `No user found with this id ${id}`,
                status: 404
            };
        throw new AppError_1.default("ERR_NO_USER_FOUND", 404);
    }
    if (((_a = user === null || user === void 0 ? void 0 : user.name) === null || _a === void 0 ? void 0 : _a.trim()) == "botqueue") {
        let botInfo = JSON.stringify({
            userId: 0,
            queueId: 0,
            botIsOnQueue: false
        });
        botInfo = JSON.parse(botInfo);
        yield RedisClient_1.set("botInfo", botInfo);
    }
    const userOpenTickets = yield user.$get("tickets", {
        where: { status: "open" }
    });
    if (userOpenTickets.length > 0) {
        UpdateDeletedUserOpenTicketsStatus_1.default(userOpenTickets);
    }
    yield user.destroy();
});
exports.default = DeleteUserService;
