"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Addon = exports.ADDON_PER_PERSON_PER_NIGHT_TYPES = exports.ADDON_PER_NIGHT_TYPES = void 0;
const core_1 = require("@roomstay/core");
const dayjs_1 = __importDefault(require("dayjs"));
exports.ADDON_PER_NIGHT_TYPES = [
    core_1.EAddonPricingType.PER_NIGHT,
    core_1.EAddonPricingType.PER_PERSON_PER_NIGHT,
    core_1.EAddonPricingType.PER_QUANTITY_NAME_PER_NIGHT,
    core_1.EAddonPricingType.PER_ROOM_QUANTITY_PER_NIGHT,
    core_1.EAddonPricingType.PER_PERSON_OCCUPANCY_PER_NIGHT,
    core_1.EAddonPricingType.PER_ADULT_OCCUPANCY_PER_NIGHT,
    core_1.EAddonPricingType.PER_CHILD_OCCUPANCY_PER_NIGHT,
    core_1.EAddonPricingType.PER_ALL_PERSONS_PER_NIGHT,
];
exports.ADDON_PER_PERSON_PER_NIGHT_TYPES = [
    core_1.EAddonPricingType.PER_PERSON_PER_NIGHT,
    core_1.EAddonPricingType.PER_PERSON_OCCUPANCY_PER_NIGHT,
    core_1.EAddonPricingType.PER_ADULT_OCCUPANCY_PER_NIGHT,
    core_1.EAddonPricingType.PER_CHILD_OCCUPANCY_PER_NIGHT,
];
class Addon {
    constructor(props) {
        /**
         * Whether the price for this package is included in the room rate.
         * (Always `false` unless the price is zero)
         */
        this.inclusive = false;
        this.availability = {};
        this.selectorDates = [];
        if (props) {
            this.applyData(props);
        }
    }
    static fromData(data) {
        const addon = new this();
        addon.applyData(data);
        return addon;
    }
    applyData(data) {
        var _a;
        Object.keys(data).map((key) => {
            this[key] = data[key];
        }, this);
        // The image property comes as an array so we need to handle this
        if (Array.isArray(this.image) && this.image.length > 0) {
            this.image = this.image[0];
        }
        let first = true;
        for (const date in this.availability) {
            if (!Object.prototype.hasOwnProperty.call(this.availability, date)) {
                continue;
            }
            const day = (0, dayjs_1.default)(date, 'YYYY-MM-DD');
            this.selectorDates.push({ text: day.format('D MMM'), value: date });
            if (first) {
                this.firstDate = date;
                first = false;
            }
            const priceInfo = this.availability[date];
            priceInfo.selectorTimes = [];
            (_a = priceInfo.timeSlots) === null || _a === void 0 ? void 0 : _a.forEach((slot) => {
                var _a, _b;
                const timeRegExp = /T(\d{2}):(\d{2}):(\d{2})$/;
                const startTime = (_a = slot.startTime) === null || _a === void 0 ? void 0 : _a.match(timeRegExp);
                const endTime = (_b = slot.endTime) === null || _b === void 0 ? void 0 : _b.match(timeRegExp);
                if (startTime && endTime) {
                    const start = (0, dayjs_1.default)().hour(parseInt(startTime[1], 10)).minute(parseInt(startTime[2], 10)).second(parseInt(startTime[3], 10));
                    const end = (0, dayjs_1.default)().hour(parseInt(endTime[1], 10)).minute(parseInt(endTime[2], 10)).second(parseInt(endTime[3], 10));
                    const [hours, minutes, seconds] = slot.increment.split(':').map((it) => parseInt(it, 10));
                    const increment = (time) => time.add(hours, 'hour').add(minutes, 'minute').add(seconds, 'second');
                    for (let time = start; time.isBefore(end, 'second'); time = increment(time)) {
                        priceInfo.selectorTimes.push({ text: time.format('h:mma'), value: time.format('THH:mm:ss') });
                    }
                }
            });
        }
        if (data.checkInDate && data.checkOutDate) {
            this.nights = data.checkOutDate.diff(data.checkInDate, 'day');
        }
    }
}
exports.Addon = Addon;
//# sourceMappingURL=Addon.js.map