"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 });
exports.getEventsFromHotelId = exports.getEventsForCity = void 0;
const BaseAPI_1 = __importDefault(require("../BaseAPI"));
const config_1 = require("../../config");
const cachedCityEvents = {};
const getEventsForCity = (city) => __awaiter(void 0, void 0, void 0, function* () {
    const headers = yield BaseAPI_1.default.getBaseHeaders();
    if (cachedCityEvents[city]) {
        return cachedCityEvents[city];
    }
    cachedCityEvents[city] = (() => __awaiter(void 0, void 0, void 0, function* () {
        try {
            const response = yield fetch(`${config_1.RoomstayGlobalConfig.RoomstayEventsApiUrl}?city=${city}`, { headers });
            const responseJson = yield response.json();
            return responseJson.data;
        }
        catch (ex) {
            console.log(`Error fetching events for ${city}: `, ex);
        }
        return [];
    }))();
    return cachedCityEvents[city];
});
exports.getEventsForCity = getEventsForCity;
const cachedHotelEvents = {};
const getEventsFromHotelId = (hotelId, unixStartTime) => __awaiter(void 0, void 0, void 0, function* () {
    const hotelEventKey = hotelId + unixStartTime;
    const headers = yield BaseAPI_1.default.getBaseHeaders();
    if (cachedHotelEvents[hotelEventKey]) {
        return cachedHotelEvents[hotelEventKey];
    }
    cachedHotelEvents[hotelEventKey] = (() => __awaiter(void 0, void 0, void 0, function* () {
        try {
            const response = yield fetch(`${config_1.RoomstayGlobalConfig.RoomstayEventsApiUrl}hotel/${hotelId}?${unixStartTime
                ? new URLSearchParams({
                    unixStartTime: unixStartTime === null || unixStartTime === void 0 ? void 0 : unixStartTime.toString(),
                })
                : ''}`, { headers });
            const responseJson = yield response.json();
            return responseJson.data;
        }
        catch (ex) {
            console.log(`Error fetching events for hotelID ${hotelId}: `, ex);
            throw new Error();
        }
    }))();
    return cachedHotelEvents[hotelEventKey];
});
exports.getEventsFromHotelId = getEventsFromHotelId;
//# sourceMappingURL=EventsServiceAPI.js.map