"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.write = exports.read = exports.isPastBooking = exports.isCancelledBooking = exports.isCachedEventValid = exports.getValidEvents = exports.getApplicableEvents = void 0;
const core_1 = require("@roomstay/core");
const dayjs_1 = __importDefault(require("dayjs"));
const isBetween_1 = __importDefault(require("dayjs/plugin/isBetween"));
const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
const StorageProvider_1 = require("../providers/storage/StorageProvider");
dayjs_1.default.extend(utc_1.default);
dayjs_1.default.extend(timezone_1.default);
dayjs_1.default.extend(isBetween_1.default);
const CACHED_EVENTS_KEY = 'events';
const provider = new StorageProvider_1.StorageProvider();
const createEventKey = (hotelId, unixStartDate) => [CACHED_EVENTS_KEY, hotelId, unixStartDate].join('-');
const write = (hotelId, unixStartDate, value) => provider.write(createEventKey(hotelId, unixStartDate), value);
exports.write = write;
const read = (hotelId, unixStartDate) => provider.read(createEventKey(hotelId, unixStartDate));
exports.read = read;
const getValidEvents = (events) => {
    if (!events || events.length === 0) {
        return [];
    }
    const today = (0, dayjs_1.default)();
    return events.filter(({ endDate, timezone, isPermanent }) => isPermanent || today.isBefore((0, dayjs_1.default)(endDate, timezone)));
};
exports.getValidEvents = getValidEvents;
const getApplicableEvents = (events, { checkInDate, checkInTime, checkOutDate, checkOutTime, }) => {
    if (!events || events.length === 0) {
        return [];
    }
    const itineraryStartDate = (0, dayjs_1.default)(`${checkInDate}${checkInTime}`, 'YYYY-MM-DDHHmm');
    const itineraryEndDate = (0, dayjs_1.default)(`${checkOutDate}${checkOutTime}`, 'YYYY-MM-DDHHmm');
    return events.filter(({ endDate: endDateString, timezone, startDate: startDateString }) => {
        // The maximum (and minimum) date in JavaScript. This is used to represent the far future and past.
        // https://stackoverflow.com/questions/11526504/minimum-and-maximum-date
        const JS_MAX_DATE_MS = 8640000000000000;
        const eventStartDate = startDateString ? dayjs_1.default.tz(startDateString, timezone) : (0, dayjs_1.default)(-JS_MAX_DATE_MS);
        const eventEndDate = endDateString ? dayjs_1.default.tz(endDateString, timezone) : (0, dayjs_1.default)(JS_MAX_DATE_MS);
        return eventStartDate.isSameOrBefore(itineraryEndDate) && eventEndDate.isSameOrAfter(itineraryStartDate);
    });
};
exports.getApplicableEvents = getApplicableEvents;
const isCancelledBooking = (booking) => {
    return booking.status === core_1.EBookingStatus.Cancelled;
};
exports.isCancelledBooking = isCancelledBooking;
const isPastBooking = (booking) => {
    return (0, dayjs_1.default)(booking.checkOutDate).isBefore((0, dayjs_1.default)());
};
exports.isPastBooking = isPastBooking;
const isCachedEventValid = (cachedEvents) => {
    var _a;
    const today = (0, dayjs_1.default)();
    const expiredDate = (0, dayjs_1.default)(cachedEvents.expired);
    return today.isBefore(expiredDate) && ((_a = cachedEvents.events) === null || _a === void 0 ? void 0 : _a.length) > 0;
};
exports.isCachedEventValid = isCachedEventValid;
//# sourceMappingURL=EventsHelper.js.map