"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.BookingWizardGuestSelector = void 0;
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const AutoAutoHeight_1 = __importDefault(require("../../../../animations/AutoAutoHeight"));
const BEButton_1 = __importDefault(require("../../BEButton"));
const Icon_1 = __importStar(require("../../Icon/Icon"));
const NumberIncrement_1 = __importDefault(require("../../NumberIncrement"));
const Overlay_1 = require("../../Overlay/Overlay");
const Text_1 = __importStar(require("../../Text"));
const Translation_1 = require("../../../../translations/Translation");
const StringHelper_1 = __importDefault(require("../../../../util/StringHelper"));
const AgedBetweenLabel_1 = require("../../AgedBetweenLabel/AgedBetweenLabel");
const BookingWizard_1 = require("../BookingWizard");
const BookingWizard_module_scss_1 = __importDefault(require("../BookingWizard.module.scss"));
const BookingWizardBottomSheet_1 = __importStar(require("../BookingWizardBottomSheet/BookingWizardBottomSheet"));
const BookingWizardContent_1 = require("../BookingWizardContent");
const BookingWizardContext_1 = require("../BookingWizardContext");
const BookingWizardGuestSelector_module_scss_1 = __importDefault(require("./BookingWizardGuestSelector.module.scss"));
const BookingWizardGuestSelector = ({ disableChild, showInfants, minAdultAge, maxAdultAge, maxAdults = 10, maxChildren = 10, maxInfants = 10, disableRoom, defaultValue, followElement, hideTooltip, onChange, onEditingChange, ageRange, notes, onOverlayOpenChange, promoCodeConfig, submitConfig, }) => {
    var _a, _b, _c, _d;
    const { overlay, type: overlayType, layout, isSmallContainer, stateSections, isImagesTheme, isImagesThemeMobile, onPrevHandler } = (0, BookingWizardContext_1.useBookingWizard)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const ref = (0, react_1.useRef)(null);
    const targetRef = (0, react_1.useRef)(null);
    const [adults, setAdults] = (0, react_1.useState)((_a = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.adults) !== null && _a !== void 0 ? _a : 1);
    const [children, setChildren] = (0, react_1.useState)((_b = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.children) !== null && _b !== void 0 ? _b : 0);
    const [infants, setInfants] = (0, react_1.useState)((_c = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.infants) !== null && _c !== void 0 ? _c : 0);
    const [room, setRoom] = (0, react_1.useState)((_d = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.room) !== null && _d !== void 0 ? _d : 1);
    const [editingAdults, setEditingAdults] = (0, react_1.useState)(adults);
    const [editingChildren, setEditingChildren] = (0, react_1.useState)(children);
    const [editingInfants, setEditingInfants] = (0, react_1.useState)(infants);
    const [editingRoom, setEditingRoom] = (0, react_1.useState)(room);
    const [open, setOpen] = (0, react_1.useState)(false);
    const autoApply = isImagesTheme;
    (0, react_1.useEffect)(() => {
        var _a, _b, _c, _d;
        setAdults((_a = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.adults) !== null && _a !== void 0 ? _a : 1);
        setChildren((_b = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.children) !== null && _b !== void 0 ? _b : 0);
        setInfants((_c = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.infants) !== null && _c !== void 0 ? _c : 0);
        setRoom((_d = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.room) !== null && _d !== void 0 ? _d : 1);
    }, [defaultValue, isImagesTheme]);
    (0, react_1.useEffect)(() => {
        setEditingAdults(adults);
    }, [adults]);
    (0, react_1.useEffect)(() => {
        setEditingChildren(children);
    }, [children]);
    (0, react_1.useEffect)(() => {
        setEditingInfants(infants);
    }, [infants]);
    (0, react_1.useEffect)(() => {
        if (stateSections && stateSections[BookingWizard_1.EBookingWizardSection.Guest]) {
            setOpen(stateSections[BookingWizard_1.EBookingWizardSection.Guest].opening);
        }
    }, [stateSections]);
    const onApply = () => {
        setAdults(editingAdults);
        setChildren(editingChildren);
        setInfants(editingInfants);
        setRoom(editingRoom);
        setOpen(false);
        onChange === null || onChange === void 0 ? void 0 : onChange({ adults: editingAdults, children: editingChildren, infants: editingInfants, room: editingRoom });
    };
    (0, react_1.useEffect)(() => {
        onEditingChange === null || onEditingChange === void 0 ? void 0 : onEditingChange({ adults: editingAdults, children: editingChildren, infants: editingInfants, room: editingRoom });
    }, [editingAdults, editingChildren, editingAdults, editingRoom]);
    (0, react_1.useEffect)(() => {
        onOverlayOpenChange === null || onOverlayOpenChange === void 0 ? void 0 : onOverlayOpenChange(open);
        return () => {
            onOverlayOpenChange === null || onOverlayOpenChange === void 0 ? void 0 : onOverlayOpenChange(false);
        };
    }, [open]);
    const getValueAsText = () => {
        if (layout === 'horizontal-condensed') {
            return adults + children;
        }
        if (layout === 'booking-summary') {
            return StringHelper_1.default.pluralWithCount(adults + children + infants, 'Guest', t);
        }
        if (isImagesTheme) {
            if (adults + children + infants === 0) {
                return t(Translation_1.Translation.Misc.GuestsPlaceHolder);
            }
        }
        const parts = [StringHelper_1.default.pluralWithCount(adults, 'Adult', t)];
        if (children > 0) {
            parts.push(StringHelper_1.default.pluralWithCount(children, 'Child', t));
        }
        if (infants > 0) {
            parts.push(StringHelper_1.default.pluralWithCount(infants, 'Infant', t));
        }
        return parts.join(' - ');
    };
    const GuestNotes = () => {
        return notes ? react_1.default.createElement("div", { className: BookingWizard_module_scss_1.default.notes }, notes) : null;
    };
    const onChangeAdults = (applyButton, newValue) => {
        if (applyButton) {
            setEditingAdults(newValue);
        }
        else {
            setAdults(newValue);
            if (autoApply) {
                onChange === null || onChange === void 0 ? void 0 : onChange({ adults: newValue, children: editingChildren, infants: editingInfants, room: editingRoom });
            }
        }
    };
    const onChangeChildren = (applyButton, newValue) => {
        if (applyButton) {
            setEditingChildren(newValue);
        }
        else {
            setChildren(newValue);
            if (autoApply) {
                onChange === null || onChange === void 0 ? void 0 : onChange({ adults: editingAdults, children: newValue, infants: editingInfants, room: editingRoom });
            }
        }
    };
    const onChangeInfants = (applyButton, newValue) => {
        if (applyButton) {
            setEditingInfants(newValue);
        }
        else {
            setInfants(newValue);
            if (autoApply) {
                onChange === null || onChange === void 0 ? void 0 : onChange({ adults: editingAdults, children: editingChildren, infants: newValue, room: editingRoom });
            }
        }
    };
    const onChangeRoom = (applyButton, newValue) => {
        if (applyButton) {
            setEditingRoom(newValue);
        }
        else {
            setRoom(newValue);
            if (autoApply) {
                onChange === null || onChange === void 0 ? void 0 : onChange({ adults: editingAdults, children: editingChildren, infants: editingInfants, room: newValue });
            }
        }
    };
    const inner = (applyButton = true) => {
        var _a, _b, _c, _d;
        return (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['popper--box'] },
            react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['popper--content'], ref: ref },
                react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['row-wrapper'] },
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.row },
                        react_1.default.createElement(Text_1.default, { className: BookingWizardGuestSelector_module_scss_1.default['title'], type: Text_1.TextType.Small }, StringHelper_1.default.plural(editingAdults, 'Adult', t)),
                        react_1.default.createElement(NumberIncrement_1.default, { number: editingAdults, min: 1, max: maxAdults, onChange: (value) => onChangeAdults(applyButton, value), textSize: Text_1.TextType.Small, hideTooltips: hideTooltip })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['sub-title'] },
                        react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: minAdultAge, max: maxAdultAge })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.divider })),
                disableChild ? null : (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['row-wrapper'] },
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.row },
                        react_1.default.createElement(Text_1.default, { className: BookingWizardGuestSelector_module_scss_1.default['title'], type: Text_1.TextType.Small }, StringHelper_1.default.plural(editingChildren, 'Child', t)),
                        react_1.default.createElement(NumberIncrement_1.default, { number: editingChildren, min: 0, max: maxChildren, onChange: (value) => onChangeChildren(applyButton, value), textSize: Text_1.TextType.Small, hideTooltips: hideTooltip })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['sub-title'] },
                        react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: (_a = ageRange === null || ageRange === void 0 ? void 0 : ageRange.child) === null || _a === void 0 ? void 0 : _a.min, max: (_b = ageRange === null || ageRange === void 0 ? void 0 : ageRange.child) === null || _b === void 0 ? void 0 : _b.max })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.divider }))),
                showInfants ? (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['row-wrapper'] },
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.row },
                        react_1.default.createElement(Text_1.default, { className: BookingWizardGuestSelector_module_scss_1.default['title'], type: Text_1.TextType.Small }, StringHelper_1.default.plural(editingInfants, 'Infant', t)),
                        react_1.default.createElement(NumberIncrement_1.default, { number: editingInfants, min: 0, max: maxInfants, onChange: (value) => onChangeInfants(applyButton, value), textSize: Text_1.TextType.Small, hideTooltips: hideTooltip })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['sub-title'] },
                        react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: (_c = ageRange === null || ageRange === void 0 ? void 0 : ageRange.infant) === null || _c === void 0 ? void 0 : _c.min, max: (_d = ageRange === null || ageRange === void 0 ? void 0 : ageRange.infant) === null || _d === void 0 ? void 0 : _d.max })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.divider }))) : null,
                disableRoom ? null : (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['row-wrapper'] },
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.row },
                        react_1.default.createElement(Text_1.default, { className: BookingWizardGuestSelector_module_scss_1.default['title'], type: Text_1.TextType.Small }, StringHelper_1.default.plural(editingRoom, 'Room', t)),
                        react_1.default.createElement(NumberIncrement_1.default, { number: editingRoom, min: 1, max: 10, onChange: (value) => onChangeRoom(applyButton, value), textSize: Text_1.TextType.Small, hideTooltips: hideTooltip })),
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.divider }))),
                react_1.default.createElement(GuestNotes, null),
                isImagesThemeMobile && promoCodeConfig && (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['row-wrapper'] },
                    react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.row },
                        react_1.default.createElement(BookingWizardContent_1.BWPromoCodeSection, { divider: promoCodeConfig.divider, withLabel: promoCodeConfig.withLabel, promoCode: promoCodeConfig.promoCode, setPromoCode: promoCodeConfig.setPromoCode, onChange: promoCodeConfig.onChange, valid: promoCodeConfig.valid, hideTooltip: promoCodeConfig.hideTooltip, t: t, inactive: false, hidden: false, isImagesTheme: true })))),
                applyButton ? (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default['popper--action'] },
                    react_1.default.createElement(BEButton_1.default, { wide: overlayType === 'bottom-sheet', className: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default.applyBtn, {
                            [BookingWizardGuestSelector_module_scss_1.default['--booking-summary']]: layout === 'booking-summary',
                        }), onClick: onApply }, t(Translation_1.Translation.Misc.Apply)))) : null)));
    };
    const content = () => {
        if (overlayType === 'inline') {
            return (react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.popper },
                react_1.default.createElement(AutoAutoHeight_1.default, { open: open }, inner(false))));
        }
        if (['bottom-sheet', 'top-sheet'].includes(overlayType)) {
            return (react_1.default.createElement(BookingWizardBottomSheet_1.default, Object.assign({ className: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default.container, BookingWizardGuestSelector_module_scss_1.default['--bottom-sheet'], {
                    [BookingWizardGuestSelector_module_scss_1.default['custom-overlay-images-theme']]: isImagesTheme,
                }), open: open, onDismiss: () => {
                    if (!isImagesThemeMobile) {
                        setOpen(false);
                    }
                }, header: react_1.default.createElement(BookingWizardBottomSheet_1.BWBottomSheetHeader, { title: t(Translation_1.Translation.Misc.HowManyGuests), onClose: () => setOpen(false) }), footer: submitConfig ? (react_1.default.createElement(BookingWizardBottomSheet_1.BWBottomSheetFooter, { hidePrev: stateSections[BookingWizard_1.EBookingWizardSection.Guest].isFirst, onPrev: () => onPrevHandler(BookingWizard_1.EBookingWizardSection.Guest), onNext: () => submitConfig.onSubmit(), nextLabel: t(Translation_1.Translation.Misc.BookNow), disabledNext: submitConfig.disabledNext })) : null }, ('top-sheet' === overlayType && {
                snapPoints: ({ maxHeight }) => [maxHeight],
            })),
                react_1.default.createElement("div", { className: BookingWizardGuestSelector_module_scss_1.default.popper },
                    inner(!autoApply),
                    " ")));
        }
        return (react_1.default.createElement(Overlay_1.Overlay, { offset: overlay === null || overlay === void 0 ? void 0 : overlay.offset, usePortal: false, followElement: followElement || targetRef.current, placement: overlay === null || overlay === void 0 ? void 0 : overlay.placement, fallbackPlacements: overlay === null || overlay === void 0 ? void 0 : overlay.fallbackPlacements, strategy: "absolute", open: open, onClose: () => setOpen === null || setOpen === void 0 ? void 0 : setOpen(!open), className: BookingWizardGuestSelector_module_scss_1.default.popper, containerClassName: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default['custom-overlay'], {
                [BookingWizardGuestSelector_module_scss_1.default['small-container']]: isSmallContainer,
                [BookingWizardGuestSelector_module_scss_1.default['custom-overlay-images-theme']]: isImagesTheme,
            }), hideStyles: true }, inner(!isImagesTheme)));
    };
    const DropdownIcon = () => {
        return ['horizontal-condensed', 'booking-summary'].includes(layout) || isImagesTheme ? react_1.default.createElement(react_1.default.Fragment, null) : react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Dropdown });
    };
    return (react_1.default.createElement("div", { ref: targetRef, className: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default.container, {
            [BookingWizardGuestSelector_module_scss_1.default['--inline']]: overlayType === 'inline',
            [BookingWizardGuestSelector_module_scss_1.default['--open']]: open,
        }) },
        react_1.default.createElement("div", { className: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default['container--search']), onClick: () => setOpen === null || setOpen === void 0 ? void 0 : setOpen(!open) },
            react_1.default.createElement(Text_1.default, { className: (0, classnames_1.default)(BookingWizardGuestSelector_module_scss_1.default['text-display'], {
                    [BookingWizardGuestSelector_module_scss_1.default['--line-1']]: layout === 'horizontal-condensed',
                    [BookingWizardGuestSelector_module_scss_1.default['--line-booking-summary']]: layout === 'booking-summary',
                    [BookingWizard_module_scss_1.default.value]: isImagesTheme,
                    [BookingWizard_module_scss_1.default['text-display']]: isImagesTheme,
                }), bold: true }, getValueAsText()),
            react_1.default.createElement(DropdownIcon, null)),
        content()));
};
exports.BookingWizardGuestSelector = BookingWizardGuestSelector;
//# sourceMappingURL=BookingWizardGuestSelector.js.map