"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class RatePillProvider {
    constructor() {
        this.registeredPills = [];
    }
    registerRatePill(pill, criteria, isExclusive = false) {
        if (this.doesPillExist(pill, 'Rate') === false) {
            this.registeredPills.push({ pill, criteria, location: 'Rate', isExclusive });
        }
    }
    registerRoomPill(pill, criteria) {
        const pillIndex = this.doesPillExist(pill, 'Room');
        const newPill = { pill, criteria, location: 'Room', isExclusive: true };
        if (pillIndex === false) {
            this.registeredPills.push(newPill);
        }
        else {
            this.registeredPills[pillIndex] = newPill;
        }
    }
    getPillsForRate(match) {
        const matches = [];
        this.registeredPills.forEach((registeredPill) => {
            var _a, _b;
            // Rates will only ever show Rate Pills
            if (registeredPill.location === 'Rate' && registeredPill.criteria(match)) {
                const pill = Object.assign({}, registeredPill.pill);
                if (match.rate && registeredPill.pill.setDescription) {
                    pill.description = (_b = (_a = registeredPill.pill).setDescription) === null || _b === void 0 ? void 0 : _b.call(_a, match.rate);
                }
                matches.push(pill);
            }
        });
        return matches;
    }
    getPillsForRoom(match) {
        const roomMatches = [];
        const rateMatches = [];
        // Rooms will show both Room and Rate pills
        this.registeredPills.forEach((registeredPill) => {
            if (registeredPill.location === 'Room') {
                if (registeredPill.criteria(match)) {
                    roomMatches.push(registeredPill.pill);
                }
            }
            if (match.room) {
                for (const rate of match.room.getRates()) {
                    if (registeredPill.location === 'Rate' && !registeredPill.isExclusive && registeredPill.criteria(Object.assign(Object.assign({}, match), { rate }))) {
                        rateMatches.push(registeredPill.pill);
                        // No need to duplicate check because as soon as one Pill is matched, no more rates are checked.
                        break;
                    }
                }
            }
        });
        return [...roomMatches, ...rateMatches];
    }
    doesPillExist(pill, location) {
        for (let i = 0, end = this.registeredPills.length; i < end; i++) {
            const registeredPill = this.registeredPills[i];
            if (registeredPill.location !== location)
                continue;
            // Preferred: match by id if both have one
            if (pill.id && registeredPill.pill.id) {
                if (registeredPill.pill.id === pill.id) {
                    return i;
                }
                continue;
            }
            // Fallback: legacy comparison
            if (registeredPill.pill.title === pill.title && registeredPill.pill.tooltipText === pill.tooltipText && registeredPill.pill.translation === pill.translation) {
                return i;
            }
        }
        return false;
    }
}
exports.default = new RatePillProvider();
//# sourceMappingURL=RatePillProvider.js.map