"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 BaseAPI_1 = __importDefault(require("./BaseAPI"));
const APIError_1 = require("../errors/APIError");
class HotelAPI extends BaseAPI_1.default {
    constructor() {
        super(...arguments);
        this.pendingQueries = {};
        this.hotelOverrideQueries = {};
        this.fetchData = (lang, hotel) => __awaiter(this, void 0, void 0, function* () {
            const hotelID = hotel.hotelID;
            const queryID = this.getQueryID([lang, hotelID]);
            if (this.pendingQueries[queryID] !== undefined) {
                return this.pendingQueries[queryID];
            }
            const promiseFetch = () => __awaiter(this, void 0, void 0, function* () {
                try {
                    const response = yield fetch(this.getApiPath() + `hotel/${hotelID}?language=${lang}`, {
                        headers: yield BaseAPI_1.default.getBaseHeaders(),
                    });
                    if (!response.ok) {
                        return new APIError_1.APIError(`${response.status} ${response.statusText}`);
                    }
                    const responseObject = yield response.json();
                    return responseObject;
                }
                catch (error) {
                    throw new APIError_1.APIError('Failed to fetch hotel data');
                }
            });
            this.pendingQueries[queryID] = promiseFetch();
            return this.pendingQueries[queryID];
        });
        this.fetchOverrides = (frontendId, companyId) => __awaiter(this, void 0, void 0, function* () {
            const queryID = this.getQueryID([frontendId, companyId]);
            if (this.hotelOverrideQueries[queryID] !== undefined) {
                return this.hotelOverrideQueries[queryID];
            }
            const promiseFetch = () => __awaiter(this, void 0, void 0, function* () {
                try {
                    const response = yield fetch(this.getApiPath() + `hotel/${frontendId}/overrides${companyId ? `?companyId=${companyId}` : ''}`, {
                        headers: yield BaseAPI_1.default.getBaseHeaders(),
                    });
                    const json = yield response.json();
                    return json;
                }
                catch (error) {
                    throw new APIError_1.APIError('Failed to fetch hotel data');
                }
            });
            this.hotelOverrideQueries[queryID] = promiseFetch();
            return this.hotelOverrideQueries[queryID];
        });
        this.fetchBookingWizardData = (companyId) => __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(this.getApiPath() + `company/${companyId}/booking-wizard-data`, {
                headers: yield BaseAPI_1.default.getBaseHeaders(),
            });
            const json = yield response.json();
            return json;
        });
        this.getQueryID = (params) => {
            return params.join('-');
        };
    }
}
exports.default = HotelAPI;
//# sourceMappingURL=HotelAPI.js.map