"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = CalendarHighlightLegend;
const dayjs_1 = __importDefault(require("dayjs"));
const react_1 = __importStar(require("react"));
const Text_1 = __importStar(require("../../Text"));
const Tooltip_1 = __importDefault(require("../../Tooltip/Tooltip"));
const contexts_1 = require("../../../../contexts");
const hooks_1 = require("../../../../hooks");
const CalendarHighlightLegend_module_scss_1 = __importDefault(require("./CalendarHighlightLegend.module.scss"));
function CalendarHighlightLegend({ showMultipleMonths }) {
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const datePickerContext = (0, react_1.useContext)(contexts_1.DatePickerContext);
    const highlights = (0, react_1.useMemo)(() => {
        if (!(hotel === null || hotel === void 0 ? void 0 : hotel.calendarHighlights) || !(datePickerContext === null || datePickerContext === void 0 ? void 0 : datePickerContext.currentMonth)) {
            return [];
        }
        const rangeStart = datePickerContext.currentMonth.startOf('month');
        const rangeEnd = datePickerContext.currentMonth.add(showMultipleMonths ? 1 : 0, 'month').endOf('month');
        const legendFlags = new Map();
        const result = [];
        hotel.calendarHighlights.forEach((highlight) => {
            const highlightStart = (0, dayjs_1.default)(highlight.startDate);
            const highlightEnd = (0, dayjs_1.default)(highlight.endDate);
            const isOverlapping = highlightStart.isSameOrBefore(rangeEnd) && highlightEnd.isSameOrAfter(rangeStart);
            if (!isOverlapping)
                return;
            const key = `${highlight.name}-${highlight.color}`.toLowerCase();
            if (!legendFlags.get(key)) {
                legendFlags.set(key, true);
                result.push(highlight);
            }
        });
        return result;
    }, [hotel === null || hotel === void 0 ? void 0 : hotel.calendarHighlights, datePickerContext === null || datePickerContext === void 0 ? void 0 : datePickerContext.currentMonth, showMultipleMonths]);
    return (react_1.default.createElement("div", { className: CalendarHighlightLegend_module_scss_1.default.legend },
        react_1.default.createElement("ul", { className: CalendarHighlightLegend_module_scss_1.default.list }, highlights.map((highlight) => {
            const content = (react_1.default.createElement("div", { className: CalendarHighlightLegend_module_scss_1.default.content },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, inline: true }, highlight.name),
                react_1.default.createElement("div", { className: CalendarHighlightLegend_module_scss_1.default.underline, style: { backgroundColor: highlight.color } })));
            return (react_1.default.createElement("li", { key: highlight.id, className: CalendarHighlightLegend_module_scss_1.default.item }, highlight.tooltip ? (react_1.default.createElement(Tooltip_1.default, { usePortal: true, title: highlight.tooltip }, content)) : (content)));
        }))));
}
//# sourceMappingURL=CalendarHighlightLegend.js.map