"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 SchedulingNotifyCache_1 = require("../../helpers/SchedulingNotifyCache");
const SchedulingNotify_1 = __importDefault(require("../../models/SchedulingNotify"));
const ListSchedulingNotifyService = ({ searchParam = "", pageNumber = "1" }) => __awaiter(void 0, void 0, void 0, function* () {
    // const whereCondition = {
    //   message: Sequelize.where( 
    //      Sequelize.fn("date", Sequelize.col("schedulingDate")), `${searchParam.toLowerCase().trim()}`,
    //      ),
    // };
    let date = searchParam.split(' ')[0];
    let hour = searchParam.split(' ')[1].split(':')[0];
    let minute = searchParam.split(' ')[1].split(':')[1];
    let fromCache = null;
    fromCache = yield SchedulingNotifyCache_1.searchScheduleCache(date, hour, minute);
    if (fromCache) {
        return {
            schedulingNotifies: fromCache,
            count: 0,
            hasMore: false,
        };
    }
    const whereCondition = {
        [sequelize_1.Op.and]: [
            {
                "$schedulingTime$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("date", sequelize_1.Sequelize.col("schedulingTime")), `${date.trim()}`)
            },
            {
                "$schedulingTime$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("hour", sequelize_1.Sequelize.col("schedulingTime")), `${hour.trim()}`)
            },
            {
                "$schedulingTime$": sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("minute", sequelize_1.Sequelize.col("schedulingTime")), `${minute.trim()}`)
            }
        ]
    };
    const limit = 1;
    const offset = limit * (+pageNumber - 1);
    const { count, rows: schedulingNotifies } = yield SchedulingNotify_1.default.findAndCountAll({
        raw: true,
        where: whereCondition,
        limit,
        offset,
        order: [["id", "ASC"]]
    });
    const hasMore = count > offset + schedulingNotifies.length;
    return {
        schedulingNotifies,
        count,
        hasMore
    };
});
exports.default = ListSchedulingNotifyService;
