"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 = RoomFilterOverlay;
const contexts_1 = require("../../../../contexts/index.js");
const hooks_1 = require("../../../../hooks/index.js");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const Headline_1 = __importDefault(require("../../../generic/Headline"));
const LineBreak_1 = __importDefault(require("../../../generic/LineBreak"));
const SimpleModal_1 = __importDefault(require("../../../generic/modal/SimpleModal"));
const Overlay_1 = require("../../../generic/Overlay/Overlay");
const Text_1 = __importDefault(require("../../../generic/Text"));
const RoomFilterAmenities_1 = __importDefault(require("./RoomFilterAmenities"));
const RoomFilterPriceSlider_1 = __importDefault(require("./RoomFilterPriceSlider"));
const RoomFilterRateType_1 = __importDefault(require("./RoomFilterRateType"));
const RoomFilterRoomType_1 = __importDefault(require("./RoomFilterRoomType"));
const FilterProvider_1 = __importDefault(require("../../../../providers/FilterProvider"));
const Color_1 = require("../../../../util/Color");
const DataLayer_1 = __importStar(require("../../../../util/DataLayer"));
const ScreenSize_1 = __importDefault(require("../../../../util/ScreenSize"));
function RoomFilterOverlay(props) {
    const context = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const [filters, setFilters] = (0, react_1.useState)(context.currentFilters);
    const [roomList, setRoomList] = (0, react_1.useState)([]);
    (0, react_1.useEffect)(() => {
        var _a;
        setFilters(Object.assign({}, context.currentFilters));
        if (props.open) {
            const allRows = basketContext.getAllValidRows();
            FilterProvider_1.default.getUnfilteredRoomList((_a = basketContext.selectedBasketRow) !== null && _a !== void 0 ? _a : allRows[allRows.length - 1], hotel === null || hotel === void 0 ? void 0 : hotel.hotelID).then((rooms) => {
                rooms === null || rooms === void 0 ? void 0 : rooms.forEach((room) => {
                    var _a;
                    room.extraDetails = (_a = hotel === null || hotel === void 0 ? void 0 : hotel.rooms) === null || _a === void 0 ? void 0 : _a[room.code];
                });
                rooms = rooms === null || rooms === void 0 ? void 0 : rooms.filter((room) => room.isAvailable());
                setRoomList(rooms);
            });
        }
    }, [props.open]);
    const applyChangesOnClickHandler = () => {
        var _a;
        DataLayer_1.default.instance.sendInteraction('Apply Filters', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.ROOMS);
        context.setCurrentFilters(filters);
        (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props);
    };
    const stickyFooter = (react_1.default.createElement("div", { className: "u-flex align-items-center justify-content-between justify-content-md-end" },
        react_1.default.createElement("div", { className: "u-marg-right--heavy" },
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.Accent },
                react_1.default.createElement("strong", null,
                    FilterProvider_1.default.filterRooms(roomList, Object.values(filters)).length,
                    " ",
                    t(Translation_1.Translation.Step.Room.Filter.MatchingRoomsRates)))),
        react_1.default.createElement(BEButton_1.default, { primary: true, filled: true, onClick: applyChangesOnClickHandler }, t(Translation_1.Translation.Misc.ApplyChanges))));
    const updateFilters = (name, filter) => {
        setFilters((oldFilters) => (Object.assign(Object.assign({}, oldFilters), { [name]: filter })));
    };
    const onPriceSliderChangeHandler = (filter) => {
        return updateFilters('Budget', filter);
    };
    const onFilterTypeChangeHandler = (filter) => {
        return updateFilters('RoomType', filter);
    };
    const onFilterRateChangeHandler = (filter) => {
        return updateFilters('RateType', filter);
    };
    const onFilterAmenitiesChangeHandler = (filter) => {
        return updateFilters('Amenities', filter);
    };
    const filterInside = (react_1.default.createElement("div", { className: "room-filter" },
        react_1.default.createElement("div", { className: "u-pad--heavy u-pad-bottom--none" },
            context.screenSize > ScreenSize_1.default.Medium && (react_1.default.createElement("div", { className: "u-marg-bottom" },
                react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Step.Room.FilterBy)))),
            react_1.default.createElement("div", { className: "row" },
                react_1.default.createElement("div", { className: "col-md-6" },
                    react_1.default.createElement(RoomFilterPriceSlider_1.default, { filter: filters.Budget, onChange: onPriceSliderChangeHandler, roomList: roomList }),
                    react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                        react_1.default.createElement(RoomFilterRoomType_1.default, { roomList: roomList, filter: filters.RoomType, onChange: onFilterTypeChangeHandler }))),
                react_1.default.createElement("div", { className: "col-md-6" },
                    react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                        react_1.default.createElement(RoomFilterRateType_1.default, { roomList: roomList, filter: filters.RateType, onChange: onFilterRateChangeHandler }))),
                react_1.default.createElement("div", { className: "col-md-6" },
                    react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                        react_1.default.createElement(RoomFilterAmenities_1.default, { roomList: roomList, filter: filters.Amenities, onChange: onFilterAmenitiesChangeHandler })))))));
    if (context.screenSize > ScreenSize_1.default.Medium) {
        return (react_1.default.createElement(Overlay_1.Overlay, { followElement: props.anchorEl, onClose: props.onClose, open: props.open, placement: "bottom-start" },
            react_1.default.createElement(react_1.default.Fragment, null,
                filterInside,
                react_1.default.createElement("div", { className: "u-pad--heavy u-pad-top--none" },
                    react_1.default.createElement(LineBreak_1.default, null),
                    stickyFooter))));
    }
    else {
        return (react_1.default.createElement(SimpleModal_1.default, { open: props.open, onClose: props.onClose, title: t(Translation_1.Translation.Step.Room.FilterBy), mobileFooter: stickyFooter }, filterInside));
    }
}
//# sourceMappingURL=RoomFilterOverlay.js.map