"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GTFSCalendarModel = void 0;
const const_1 = require("../../../schema-definitions/const");
const ropid_gtfs_1 = require("../../../schema-definitions/ropid-gtfs");
const CalendarDto_1 = require("../../../schema-definitions/ropid-gtfs/models/CalendarDto");
const output_gateway_1 = require("@golemio/core/dist/output-gateway");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const sequelize_1 = __importDefault(require("@golemio/core/dist/shared/sequelize"));
class GTFSCalendarModel extends output_gateway_1.SequelizeModel {
    constructor() {
        super(ropid_gtfs_1.RopidGTFS.calendar.name, ropid_gtfs_1.RopidGTFS.calendar.pgTableName, CalendarDto_1.CalendarDto.attributeModel, {
            schema: const_1.PG_SCHEMA,
            scopes: {
                forDate(date) {
                    if (!date) {
                        return {};
                    }
                    const day = (0, moment_timezone_1.default)(date).day();
                    const where = {
                        [sequelize_1.default.Op.and]: [
                            sequelize_1.default.literal(`DATE('${date}') ` + `BETWEEN to_date(start_date, 'YYYYMMDD') AND to_date(end_date, 'YYYYMMDD')`),
                            { [GTFSCalendarModel.weekDayMap[day]]: 1 },
                        ],
                    };
                    return { where };
                },
            },
        });
        /** Retrieves all gtfs services
         * @param {object} [options] Options object with params
         * @param {string} [options.date] Filter by specific date in the 'YYYY-MM-DD' format
         * @param {number} [options.limit] Limit
         * @param {number} [options.offset] Offset
         * @returns Array of the retrieved records
         */
        this.GetAll = async (options = {}) => {
            const { limit, offset, date } = options;
            try {
                const data = await this.sequelizeModel.scope({ method: ["forDate", date] }).findAll({
                    limit,
                    offset,
                    order: [["service_id", "asc"]],
                });
                return data;
            }
            catch (err) {
                throw new golemio_errors_1.GeneralError("Database error", "GTFSCalendar", err, 500);
            }
        };
        this.GetOne = (id) => {
            throw new golemio_errors_1.FatalError("Method not implemented");
        };
    }
}
exports.GTFSCalendarModel = GTFSCalendarModel;
GTFSCalendarModel.weekDayMap = {
    1: "monday",
    2: "tuesday",
    3: "wednesday",
    4: "thursday",
    5: "friday",
    6: "saturday",
    0: "sunday",
};
//# sourceMappingURL=GTFSCalendarModel.js.map