"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
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 dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importStar(require("react"));
const EventsServiceAPI_1 = require("../../api/events-service/EventsServiceAPI");
const EventCarousel_1 = __importDefault(require("../generic/Event/EventCarousel"));
const contexts_1 = require("../../contexts");
const EventsHelper = __importStar(require("../../util/EventsHelper"));
const ScreenSize_1 = __importDefault(require("../../util/ScreenSize"));
const EventCarouselContainer = ({ defaultExpanded, itineraryItem }) => {
    const { checkInDate, checkOutDate, hotel: { checkInTime, checkOutTime }, hotelId, } = itineraryItem;
    const [loadedEvents, setLoadedEvents] = (0, react_1.useState)([]);
    const beContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const isMobile = beContext.screenSize <= ScreenSize_1.default.Medium;
    const unixStartTime = (0, dayjs_1.default)(`${checkInDate}${checkInTime}`, 'YYYY-MM-DDHH:mm').valueOf();
    const getEventsFromStorage = () => __awaiter(void 0, void 0, void 0, function* () {
        let cachedEvent = undefined;
        try {
            cachedEvent = yield EventsHelper.read(hotelId, unixStartTime);
        }
        catch (e) {
            console.log(e);
        }
        return cachedEvent || undefined;
    });
    const getEventsFromApi = () => __awaiter(void 0, void 0, void 0, function* () {
        if (!hotelId) {
            return;
        }
        return (0, EventsServiceAPI_1.getEventsFromHotelId)(hotelId, unixStartTime).then(EventsHelper.getValidEvents);
    });
    const getEvents = () => __awaiter(void 0, void 0, void 0, function* () {
        // const cachedEvents = await getEventsFromStorage();
        // if (cachedEvents && EventsHelper.isCachedEventValid(cachedEvents)) {
        //     const validEvents = EventsHelper.getValidEvents(cachedEvents.events);
        //     EventsHelper.write({
        //         expired: cachedEvents.expired,
        //         events: validEvents,
        //     });
        //     setLoadedEvents(validEvents);
        // } else {
        // TODO: Commented out event caching temporarily
        const newEvents = yield getEventsFromApi();
        if (newEvents) {
            EventsHelper.write(hotelId, unixStartTime, {
                expired: (0, dayjs_1.default)().add(1, 'd').valueOf(),
                events: newEvents,
            });
            setLoadedEvents(EventsHelper.getApplicableEvents(newEvents, {
                checkInDate,
                checkInTime,
                checkOutDate,
                checkOutTime,
            }));
        }
        // }
    });
    (0, react_1.useEffect)(() => {
        if (hotelId) {
            getEvents();
        }
    }, [hotelId]);
    if (!loadedEvents.length)
        return null;
    return react_1.default.createElement(EventCarousel_1.default, { small: isMobile, data: loadedEvents, defaultExpanded: defaultExpanded });
};
exports.default = EventCarouselContainer;
//# sourceMappingURL=EventCarouselContainer.js.map