"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 = RoomBuilderProgressRow;
const contexts_1 = require("../../../../contexts/index.js");
const hooks_1 = require("../../../../hooks/index.js");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const AutoAutoHeight_1 = __importDefault(require("../../../../animations/AutoAutoHeight"));
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 Pill_1 = __importStar(require("../../../generic/Pill"));
const PromoCodeInput_1 = __importDefault(require("../../../generic/PromoCodeInput"));
const Text_1 = __importStar(require("../../../generic/Text"));
const Tooltip_1 = __importDefault(require("../../../generic/Tooltip/Tooltip"));
const NumberOfAdultsPicker_1 = __importDefault(require("./NumberOfAdultsPicker"));
const NumberOfChildrenPicker_1 = __importDefault(require("./NumberOfChildrenPicker"));
const useSearchScopeHook_1 = require("../../../../hooks/useSearchScopeHook");
const WindowSize_1 = require("../../../../hooks/WindowSize");
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 NumberOfInfantsPicker_1 = __importDefault(require("./NumberOfInfantsPicker"));
function RoomBuilderProgressRow(props) {
    var _a, _b, _c, _d;
    const context = (0, react_1.useContext)(contexts_1.BasketContext);
    const engineContext = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { showChildren } = (0, useSearchScopeHook_1.useSearchScopeHook)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const { isMobile } = (0, WindowSize_1.useWindowSize)();
    const [isEditing, setIsEditing] = (0, react_1.useState)(false);
    const [collapsed, setCollapsed] = (0, react_1.useState)(true);
    const { row } = props;
    const isCurrent = (0, react_1.useMemo)(() => {
        var _a;
        return context.selectedBasketRow && ((_a = context.selectedBasketRow) === null || _a === void 0 ? void 0 : _a.getID()) === (row === null || row === void 0 ? void 0 : row.getID());
    }, [context.selectedBasketRow, (_a = context.selectedBasketRow) === null || _a === void 0 ? void 0 : _a.getID(), row === null || row === void 0 ? void 0 : row.getID()]);
    const classes = (0, classnames_1.default)('room-builder-progress--item', {
        '--current': isCurrent,
        '--complete': row.isValid(),
    });
    const thisElement = (0, react_1.useRef)(null);
    const [isPromoModalOpen, setIsPromoModalOpen] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        if (isEditing && !isCurrent) {
            setIsEditing(false);
        }
    }, [context.selectBasketRow]);
    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 updateDateValues = (startDate, endDate) => {
        if (startDate) {
            row.setStartDate(startDate);
        }
        if (endDate) {
            row.setEndDate(endDate);
        }
        context.updateBasketRow(row);
    };
    const onDatePickerOpenChange = (isOpen) => {
        context.onDatePickerOpenChange(isOpen);
    };
    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.selectedBasketRow.setManualInputPromoCode(!!code);
            context.updateBasketRow(context.selectedBasketRow);
            if (code) {
                setIsPromoModalOpen(false);
            }
        }
    };
    const isRemovable = context.currentBasketRows.length > 1;
    const removeRoomOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Remove Room', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.ROOMS);
        context.removeBasketRow(row);
    };
    const removeButton = (react_1.default.createElement("div", { className: "room-builder-progress--remove" },
        react_1.default.createElement(Tooltip_1.default, { title: isRemovable ? 'Remove this room' : 'Cannot remove this room as at least one must be selected.' },
            react_1.default.createElement(BEButton_1.default, { isText: true, icon: Icon_1.IconType.Close, iconPosition: "right", stopIconAnimation: true, disabled: !isRemovable, onClick: removeRoomOnClick }))));
    const toggleButton = (react_1.default.createElement("div", { className: "room-builder-progress--collapse" },
        react_1.default.createElement(BEButton_1.default, { isText: true, icon: collapsed ? Icon_1.IconType.ArrowDown3 : Icon_1.IconType.ArrowUp3, iconPosition: "right", stopIconAnimation: true, onClick: () => {
                setCollapsed((prev) => !prev);
            } })));
    const isLargerThanLarge = engineContext.screenSize > ScreenSize_1.default.ExtraLarge;
    const showEditSection = isLargerThanLarge || isCurrent;
    const roomLabel = (0, react_1.useMemo)(() => {
        return context.currentBasketRows.length !== 1 ? props.number + 1 : '';
    }, [context.currentBasketRows.length, props.number]);
    const promoCodeTooltip = (row === null || row === void 0 ? void 0 : row.getPromoCodeTooltip(t, Translation_1.Translation)) || '';
    const openPromoModalOnClick = () => {
        setIsPromoModalOpen(true);
    };
    const setPromoCodeOnClose = () => {
        setIsPromoModalOpen(false);
    };
    const cancelPromoPopupOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Cancel Promo Popup', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.ROOMS);
        setIsPromoModalOpen(false);
    };
    const saveRoomChangesOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Save Room Changes', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.ROOMS);
        context.selectBasketRow(null);
        setIsEditing(false);
    };
    const cancelRoomChangesOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Cancel Room Changes', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        context.selectBasketRow(null);
        setIsEditing(false);
    };
    const editRoomOnClick = () => {
        DataLayer_1.default.instance.sendInteraction('Edit Room', DataLayer_1.InteractionType.LINK, DataLayer_1.InteractionStep.ROOMS);
        context.selectBasketRow(row);
        setIsEditing(true);
    };
    const changePromoCode = (val) => {
        updatePromoCode(val);
    };
    const updateNumberOfAdultsOnChange = (value) => {
        updatePeopleValues({ adults: +value });
    };
    const updateNumberOfChildrenOnChange = (value) => {
        updatePeopleValues({ children: +value });
    };
    const updateNumberOfInfantsOnChange = (value) => {
        updatePeopleValues({ infants: +value });
    };
    const promocode = row.getPromoCode(hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyPromoCode);
    const DateRangePicker = (0, react_1.useMemo)(() => {
        return (react_1.default.createElement("div", { className: "room-builder-progress--value" },
            react_1.default.createElement(FloatingDatePicker_1.default, { startDate: row.getStartDate(), endDate: row.getEndDate(), selectedDateChanged: updateDateValues, className: "u-w-100@xl- d-flex align-items-center justify-content-center", isAboveNav: true, onDatePickerOpenChange: onDatePickerOpenChange },
                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, className: "u-nowrap" }, 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, className: "u-nowrap" }, row.getDepartureDate()))))));
    }, [row, onDatePickerOpenChange]);
    const RoomBuilderProgressSearchForm = (0, react_1.useMemo)(() => {
        var _a;
        return (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- d-flex" },
                react_1.default.createElement("div", { className: "room-builder-progress--value u-flex-justify-center u-flex" },
                    react_1.default.createElement(NumberOfAdultsPicker_1.default, { row: row, onChange: updateNumberOfAdultsOnChange }))),
            showChildren ? (react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.UpdateChildren), wrapperClasses: "u-w-100@xl- d-flex" },
                react_1.default.createElement("div", { className: "room-builder-progress--value u-flex-justify-center u-flex" },
                    react_1.default.createElement(NumberOfChildrenPicker_1.default, { row: row, onChange: updateNumberOfChildrenOnChange })))) : null,
            ((_a = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _a === void 0 ? void 0 : _a.supportInfants) ? (react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Date.UpdateInfants), wrapperClasses: "u-w-100@xl- d-flex" },
                react_1.default.createElement("div", { className: "room-builder-progress--value u-flex-justify-center u-flex" },
                    react_1.default.createElement(NumberOfInfantsPicker_1.default, { row: row, onChange: updateNumberOfInfantsOnChange })))) : null,
            react_1.default.createElement(react_1.default.Fragment, null,
                !isMobile ? DateRangePicker : null,
                react_1.default.createElement(Tooltip_1.default, { title: promoCodeTooltip, wrapperClasses: "u-w-100@xl- d-flex" },
                    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: promocode ? row.getPromoCodeColour() : Color_1.Color.Navy }, promocode ? promocode : t(Translation_1.Translation.Step.Date.PromoCode)),
                            react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Dropdown, color: Color_1.Color.DarkGrey })))))));
    }, [
        row,
        isMobile,
        promocode,
        promoCodeTooltip,
        showChildren,
        hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration,
        updateNumberOfAdultsOnChange,
        updateNumberOfChildrenOnChange,
        updateNumberOfInfantsOnChange,
    ]);
    return (react_1.default.createElement("div", { className: classes }, isCurrent || !row.isValid() ? (react_1.default.createElement("div", { className: "u-flex w-100 001" },
        react_1.default.createElement("div", { className: "u-flex flex-wrap flex-xl-nowrap flex-row u-w-100@xl- align-items-center" },
            react_1.default.createElement("div", null, row.isValid() ? (react_1.default.createElement(Text_1.default, null,
                react_1.default.createElement("strong", { className: "u-nowrap" },
                    t(Translation_1.Translation.Step.Room.EditingRoom),
                    " ",
                    roomLabel))) : (react_1.default.createElement(Text_1.default, null,
                react_1.default.createElement("strong", { className: "u-nowrap" },
                    t(Translation_1.Translation.Step.Room.SelectRoom),
                    " ",
                    roomLabel)))),
            showEditSection ? (react_1.default.createElement(react_1.default.Fragment, null,
                !isLargerThanLarge && (react_1.default.createElement("div", { className: 'w-100 d-flex' + (showEditSection ? ' u-marg-bottom@xl-' : '') },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey, inline: true }, t(Translation_1.Translation.Step.Room.ChooseRoomBelow)))),
                react_1.default.createElement(react_1.default.Fragment, null, isMobile ? DateRangePicker : null),
                isMobile ? (react_1.default.createElement("div", { className: "w-100" },
                    react_1.default.createElement(AutoAutoHeight_1.default, { open: !collapsed },
                        react_1.default.createElement("div", { className: "w-100 u-flex flex-column" }, RoomBuilderProgressSearchForm)))) : (RoomBuilderProgressSearchForm),
                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: promocode, onChange: changePromoCode, valid: ((_b = context.selectedBasketRow) === null || _b === void 0 ? void 0 : _b.getPromoCodeStatus()) !== BasketRow_1.EPromoCodeStatus.New && promocode
                                    ? ((_c = context.selectedBasketRow) === null || _c === void 0 ? void 0 : _c.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))))),
                !isLargerThanLarge && row.isValid() && (react_1.default.createElement(BEButton_1.default, { wide: true, primary: true, filled: true, onClick: saveRoomChangesOnClick }, t(Translation_1.Translation.Misc.Save))))) : (react_1.default.createElement("div", { className: "w-100" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Step.Room.NextRoom))))),
        react_1.default.createElement("div", { className: "u-flex align-items-center" },
            row.isValid() && (react_1.default.createElement("div", { className: "u-none@xl-" },
                react_1.default.createElement(Tooltip_1.default, { title: "Cancel changes" },
                    react_1.default.createElement("div", { className: "u-flex align-items-center" },
                        react_1.default.createElement(BEButton_1.default, { isText: true, size: "tiny", onClick: cancelRoomChangesOnClick }, t(Translation_1.Translation.Misc.Cancel).toUpperCase()))))),
            react_1.default.createElement("div", { className: "u-flex align-items-center flex-row" },
                isMobile && toggleButton,
                removeButton)))) : (react_1.default.createElement("div", { className: "u-flex w-100 002" },
        react_1.default.createElement("div", { className: "u-flex justify-content-start" },
            react_1.default.createElement("div", { className: "u-flex flex-column" },
                react_1.default.createElement("span", null,
                    react_1.default.createElement(Text_1.default, { inline: isLargerThanLarge },
                        react_1.default.createElement("strong", null, row.getRoom().name)),
                    react_1.default.createElement(Text_1.default, { inline: isLargerThanLarge, type: isLargerThanLarge ? Text_1.TextType.Body : Text_1.TextType.Small },
                        react_1.default.createElement("span", { className: "u-marg-left--light@xl" },
                            StringHelper_1.default.pluralWithDictAndCount(row.getAdults(), Translation_1.Translation.Step.Date.Adult, t),
                            showChildren ? (react_1.default.createElement(react_1.default.Fragment, null,
                                ",\u00A0",
                                StringHelper_1.default.pluralWithDictAndCount(row.getChildren(), Translation_1.Translation.Step.Date.Child, t))) : null,
                            ((_d = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _d === void 0 ? void 0 : _d.supportInfants) ? (react_1.default.createElement(react_1.default.Fragment, null,
                                ",\u00A0",
                                StringHelper_1.default.pluralWithDictAndCount(row.getInfants(), Translation_1.Translation.Step.Date.Infant, t))) : null))),
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Caption, color: Color_1.Color.DarkGrey },
                    react_1.default.createElement("strong", null, row.getStayDateRange('ddd, MMM D, YYYY', ' -')))),
            promocode && row.isPromoCodeValid() && (react_1.default.createElement("div", { className: "u-marg-left d-flex align-items-center" },
                react_1.default.createElement(Pill_1.default, { type: Pill_1.PillType.Success, icon: Icon_1.IconType.Money }, promocode)))),
        react_1.default.createElement("div", { className: "u-flex align-items-center" },
            react_1.default.createElement("div", { className: "u-flex align-items-center room-builder-progress--edit" },
                react_1.default.createElement(Tooltip_1.default, { title: t(Translation_1.Translation.Step.Room.EditThisRoom) },
                    react_1.default.createElement("div", { className: "u-flex align-items-center" },
                        react_1.default.createElement(BEButton_1.default, { isText: true, size: "tiny", onClick: editRoomOnClick }, t(Translation_1.Translation.Misc.Edit).toUpperCase())))),
            removeButton)))));
}
//# sourceMappingURL=RoomBuilderProgressRow.js.map