"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const MockAPI_1 = __importStar(require("mockApi/MockAPI"));
class ReservationAPI extends MockAPI_1.default {
    constructor() {
        super(...arguments);
        this.storedReservations = {};
        this.bookNow = (basketRows_1, profiles_1, card_1, addonRows_1, comments_1, ...args_1) => __awaiter(this, [basketRows_1, profiles_1, card_1, addonRows_1, comments_1, ...args_1], void 0, function* (basketRows, profiles, card, addonRows, comments, specialReq = undefined) {
            const rooms = [];
            basketRows.forEach((row) => {
                var _a;
                const room = {
                    RoomType: row.getRoom().code,
                    RatePlan: (_a = row.getRate()) === null || _a === void 0 ? void 0 : _a.code,
                    PromoCode: row.getPromoCode(),
                    Adults: row.getAdults(),
                    Children: row.getChildren(),
                    CheckInDate: row.getStartDate().format('YYYY-MM-DD'),
                    CheckOutDate: row.getEndDate().format('YYYY-MM-DD'),
                };
                rooms.push(room);
            });
            const addons = addonRows.map((row) => {
                const addon = row.getAddon();
                const priceInfo = addon.availability[row.getSelectedDate()];
                const response = {
                    Code: addon.code,
                    StartDate: row.getSelectedDate(),
                };
                if (row.getAddon().ratePlanCode) {
                    response.RatePlanCode = row.getAddon().ratePlanCode;
                }
                if (row.getSelectedTime()) {
                    response.StartTime = row.getSelectedTime();
                }
                if (priceInfo.price) {
                    response.Quantity = row.getQuantity();
                }
                if (priceInfo.adultPrice) {
                    response.Adults = row.getAdultQuantity();
                }
                if (priceInfo.childPrice) {
                    response.Children = row.getChildQuantity();
                }
                return response;
            });
            return this.post(MockAPI_1.APIEndpoint.Reservations, {
                Rooms: rooms,
                Profiles: profiles,
                PaymentCard: card,
                Services: addons,
                SpecialRequest: specialReq,
                Comments: comments,
            });
        });
        this.commit = (reservations) => __awaiter(this, void 0, void 0, function* () {
            return this.post(MockAPI_1.APIEndpoint.Reservations, {
                reservationIds: reservations,
            }, {}, 'commit');
        });
        this.rollback = (reservations) => __awaiter(this, void 0, void 0, function* () {
            yield this.post(MockAPI_1.APIEndpoint.Reservations, {
                reservationIds: reservations,
            }, {}, 'rollback');
            return true;
        });
        this.cancelReservation = (id, email) => new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            try {
                const response = yield this.delete(MockAPI_1.APIEndpoint.Reservations, {}, { email }, id);
                (_b = (_a = this.storedReservations[id]) === null || _a === void 0 ? void 0 : _a.reservations) === null || _b === void 0 ? void 0 : _b.forEach((reservation) => {
                    if (reservation.reservationNumber === id) {
                        reservation.status = 'Cancelled';
                    }
                });
                resolve(response);
            }
            catch (ex) {
                reject(ex);
            }
        }));
        this.findReservation = (id, email, hotelId) => new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            /*
      resolve with locally cached data, so we don't have to re-query this again
  */
            if (this.storedReservations[id]) {
                return resolve(this.storedReservations[id]);
            }
            try {
                const response = yield this.get(MockAPI_1.APIEndpoint.Reservations, { email }, id, hotelId);
                this.storedReservations[id] = response;
                resolve(response);
            }
            catch (ex) {
                reject(ex);
            }
        }));
    }
}
exports.default = ReservationAPI;
//# sourceMappingURL=ReservationAPI.js.map