"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 });
const contexts_1 = require("../../../contexts/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 FloatingDatePicker_1 = __importDefault(require("../../generic/date/FloatingDatePicker"));
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const Overlay_1 = require("../../generic/Overlay/Overlay");
const PromoCodeInput_1 = __importDefault(require("../../generic/PromoCodeInput"));
const Select_1 = require("../../generic/Select/Select");
const Text_1 = __importStar(require("../../generic/Text"));
const Tooltip_1 = __importDefault(require("../../generic/Tooltip/Tooltip"));
const useSearchScopeHook_1 = require("../../../hooks/useSearchScopeHook");
const BasketRow_1 = require("../../../models/BasketRow");
const Color_1 = require("../../../util/Color");
const DataLayer_1 = __importStar(require("../../../util/DataLayer"));
const ScreenSize_1 = __importDefault(require("../../../util/ScreenSize"));
const StringHelper_1 = __importDefault(require("../../../util/StringHelper"));
const hooks_1 = require("../../../hooks");
const HotelSearchParameters = () => {
    var _a, _b, _c, _d, _e, _f, _g;
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const context = (0, react_1.useContext)(contexts_1.BasketContext);
    const row = context.currentBasketRows[0];
    const { t } = (0, react_i18next_1.useTranslation)();
    const engineContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { showChildren } = (0, useSearchScopeHook_1.useSearchScopeHook)();
    const promoCodeTooltip = (row === null || row === void 0 ? void 0 : row.getPromoCodeTooltip(t, Translation_1.Translation)) || '';
    const updateDateValues = (startDate, endDate) => {
        if (startDate) {
            row.setStartDate(startDate);
        }
        if (endDate) {
            row.setEndDate(endDate);
        }
        context.updateBasketRow(row);
    };
    const updatePeopleValues = ({ adults, children, infants, updatedPromoCode }) => {
        if (adults && adults !== row.getAdults()) {
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.ROOMS);
            row.setAdults(adults);
        }
        if ((children || children === 0) && children !== row.getChildren()) {
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.ROOMS);
            row.setChildren(children);
        }
        if ((infants || infants === 0) && infants !== row.getInfants()) {
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.ROOMS);
            row.setInfants(infants);
        }
        if ((updatedPromoCode || updatedPromoCode === '') && updatedPromoCode !== row.getPromoCode()) {
            DataLayer_1.default.instance.sendInteraction('Promo Code', DataLayer_1.InteractionType.FORM_FIELD, DataLayer_1.InteractionStep.ROOMS);
            row.setPromoCode(updatedPromoCode);
        }
        context.updateBasketRow(row);
    };
    const [isPromoModalOpen, setIsPromoModalOpen] = (0, react_1.useState)(false);
    const [promoCode, setPromoCode] = (0, react_1.useState)(row.getPromoCode());
    const updatePromoCode = (code) => {
        DataLayer_1.default.instance.sendInteraction('Promo Code', DataLayer_1.InteractionType.FORM_FIELD, DataLayer_1.InteractionStep.ROOMS);
        if (context.selectedBasketRow) {
            context.selectedBasketRow.setPromoCode(code);
            context.updateBasketRow(context.selectedBasketRow);
            if (code) {
                setIsPromoModalOpen(false);
            }
        }
    };
    const thisElement = (0, react_1.useRef)(null);
    const openPromoModalOnClick = () => {
        return setIsPromoModalOpen(true);
    };
    const setPromoCodeOnClose = () => {
        setPromoCode(row.getPromoCode());
        setIsPromoModalOpen(false);
    };
    const cancelPromoPopupOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Cancel Promo Popup', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.ROOMS);
        setPromoCode(row.getPromoCode());
        setIsPromoModalOpen(false);
    };
    const changePromoCode = (val) => {
        updatePromoCode(val);
    };
    const updateNumberOfAdultsOnChange = (value) => {
        updatePeopleValues({ adults: +value });
    };
    const updateNumberOfChildrenOnChange = (value) => {
        updatePeopleValues({ children: +value });
    };
    const updateNumberOfInfantsOnChange = (value) => {
        updatePeopleValues({ infants: +value });
    };
    return (react_1.default.createElement("div", { className: "u-pad u-rounded hotel-search-parameters u-marg-bottom--heavy" },
        react_1.default.createElement("div", null,
            react_1.default.createElement("div", { className: "u-flex w-100" },
                react_1.default.createElement("div", { className: "u-flex flex-wrap flex-xl-nowrap flex-row u-w-100@xl-" },
                    react_1.default.createElement("div", { className: engineContext.screenSize <= ScreenSize_1.default.Large ? 'u-marg-bottom--light' : '' },
                        react_1.default.createElement(Text_1.default, null,
                            react_1.default.createElement("strong", { className: "u-nowrap" }, t(Translation_1.Translation.Step.Date.SelectYourDate)))),
                    react_1.default.createElement(react_1.default.Fragment, null,
                        react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.UpdateAdults), wrapperClasses: "u-w-100@xl-" },
                            react_1.default.createElement("div", { className: "room-builder-progress--value" },
                                react_1.default.createElement(Select_1.Select, { nativeOnMobile: true, options: [...Array(15)].map((v, value) => {
                                        value += 1;
                                        return {
                                            text: StringHelper_1.default.pluralWithDictAndCount(value, Translation_1.Translation.Step.Date.Adult, t),
                                            value: value.toString(),
                                        };
                                    }), value: (_a = row.getAdults()) === null || _a === void 0 ? void 0 : _a.toString(), onChange: updateNumberOfAdultsOnChange, keyName: "value", labelName: "text" }))),
                        showChildren ? (react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.UpdateChildren), wrapperClasses: "u-w-100@xl-" },
                            react_1.default.createElement("div", { className: "room-builder-progress--value" },
                                react_1.default.createElement(Select_1.Select, { nativeOnMobile: true, options: [...Array(15)].map((v, value) => {
                                        return {
                                            text: StringHelper_1.default.pluralWithDictAndCount(value, Translation_1.Translation.Step.Date.Child, t),
                                            value: value.toString(),
                                        };
                                    }), value: (_b = row.getChildren()) === null || _b === void 0 ? void 0 : _b.toString(), onChange: updateNumberOfChildrenOnChange, keyName: "value", labelName: "text" })))) : null,
                        ((_c = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _c === void 0 ? void 0 : _c.supportInfants) ? (react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.UpdateInfants), wrapperClasses: "u-w-100@xl-" },
                            react_1.default.createElement("div", { className: "room-builder-progress--value" },
                                react_1.default.createElement(Select_1.Select, { nativeOnMobile: true, options: [...Array(15)].map((v, value) => {
                                        return {
                                            text: StringHelper_1.default.pluralWithDictAndCount(value, Translation_1.Translation.Step.Date.Infant, t),
                                            value: value.toString(),
                                        };
                                    }), value: (_d = row.getInfants()) === null || _d === void 0 ? void 0 : _d.toString(), onChange: updateNumberOfInfantsOnChange, keyName: "value", labelName: "text" })))) : null,
                        react_1.default.createElement(react_1.default.Fragment, null,
                            react_1.default.createElement("div", { className: "room-builder-progress--value" },
                                react_1.default.createElement(FloatingDatePicker_1.default, { startDate: row.getStartDate(), selectedDateChanged: updateDateValues, className: "u-w-100@xl- d-flex align-items-center justify-content-center" },
                                    react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.SelectArrivalAndDepartureDatesByClickingHere) },
                                        react_1.default.createElement("div", { className: "u-flex align-items-center justify-content-center u-w-100@xl- u-cursor-pointer" },
                                            react_1.default.createElement(Text_1.default, { color: Color_1.Color.Accent, bold: true }, row.getArrivalDate()),
                                            react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.ArrowRight2 }),
                                            react_1.default.createElement(Text_1.default, { color: Color_1.Color.Accent, bold: true }, row.getDepartureDate()))))),
                            react_1.default.createElement(Tooltip_1.default, { title: promoCodeTooltip, wrapperClasses: "u-w-100@xl-" },
                                react_1.default.createElement("div", { className: "room-builder-progress--value u-cursor-pointer", ref: thisElement, onClick: openPromoModalOnClick },
                                    react_1.default.createElement("div", { className: "u-flex align-items-center justify-content-center u-w-100@xl-" },
                                        react_1.default.createElement(Text_1.default, { className: "u-pad-right--light", type: Text_1.TextType.Body, color: row.getPromoCode() ? row.getPromoCodeColour() : Color_1.Color.Navy }, row.getPromoCode() ? row.getPromoCode() : 'Promo Code'),
                                        react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Dropdown, color: Color_1.Color.DarkGrey }))))),
                        react_1.default.createElement(Overlay_1.Overlay, { open: isPromoModalOpen, followElement: thisElement.current, onClose: setPromoCodeOnClose },
                            react_1.default.createElement("div", { className: "u-pad--heavy" },
                                react_1.default.createElement("div", null,
                                    react_1.default.createElement(PromoCodeInput_1.default, { value: (_e = context.selectedBasketRow) === null || _e === void 0 ? void 0 : _e.getPromoCode(), onChange: changePromoCode, valid: ((_f = context.selectedBasketRow) === null || _f === void 0 ? void 0 : _f.getPromoCodeStatus()) !== BasketRow_1.EPromoCodeStatus.New
                                            ? ((_g = context.selectedBasketRow) === null || _g === void 0 ? void 0 : _g.getPromoCodeStatus()) === BasketRow_1.EPromoCodeStatus.Valid
                                            : undefined, row: context.selectedBasketRow })),
                                react_1.default.createElement("div", { className: "u-flex justify-content-end align-items-center u-marg-top" },
                                    react_1.default.createElement(BEButton_1.default, { isText: true, size: "small", onClick: cancelPromoPopupOnClick }, t(Translation_1.Translation.Misc.Cancel)))))))))));
};
exports.default = HotelSearchParameters;
//# sourceMappingURL=HotelSearchParameters.js.map