"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 sequelize_1 = require("sequelize");
const QuickAnswer_1 = __importDefault(require("../../models/QuickAnswer"));
const Queue_1 = __importDefault(require("../../models/Queue"));
const ShowQueuesByUser_1 = __importDefault(require("../UserServices/ShowQueuesByUser"));
const QuickAnswearByqueueFiltered_1 = __importDefault(require("../../helpers/QuickAnswearByqueueFiltered"));
const WhaticketSettings_1 = require("../../helpers/WhaticketSettings");
const ListQuickAnswerService = ({ searchParam = "", pageNumber = "1", userId }) => __awaiter(void 0, void 0, void 0, function* () {
    var _a;
    console.log("searchParam: ", searchParam);
    const whereCondition = {
        message: sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("LOWER", sequelize_1.Sequelize.col("message")), "LIKE", `%${searchParam.toLowerCase().trim()}%`)
    };
    const limit = 20;
    const offset = limit * (+pageNumber - 1);
    let { count, rows: quickAnswers } = yield QuickAnswer_1.default.findAndCountAll({
        where: whereCondition,
        include: [
            {
                model: Queue_1.default,
                as: "queues",
                attributes: ["id", "name", "color", "greetingMessage"]
            }
        ],
        limit,
        offset,
        order: [["message", "ASC"]]
    });
    const hasMore = count > offset + quickAnswers.length;
    if (((_a = WhaticketSettings_1.getSettingValue("quickAnswerByQueue")) === null || _a === void 0 ? void 0 : _a.value) == "enabled") {
        const queueIds = yield ShowQueuesByUser_1.default({ userId });
        quickAnswers = QuickAnswearByqueueFiltered_1.default(queueIds, quickAnswers);
    }
    return {
        quickAnswers,
        count,
        hasMore
    };
});
exports.default = ListQuickAnswerService;
