"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 = PeoplePickerRow;
const hooks_1 = require("../../../hooks/index.js");
const web_1 = require("@react-spring/web");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const AgedBetweenLabel_1 = require("../../generic/AgedBetweenLabel/AgedBetweenLabel");
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const LineBreak_1 = __importDefault(require("../../generic/LineBreak"));
const NumberIncrement_1 = __importDefault(require("../../generic/NumberIncrement"));
const Text_1 = __importStar(require("../../generic/Text"));
const useSearchScopeHook_1 = require("../../../hooks/useSearchScopeHook");
const Color_1 = require("../../../util/Color");
function PeoplePickerRow(pickerProps) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { adults, children, infants, roomCode } = pickerProps.details;
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const [animatedIn, setAnimation] = (0, react_1.useState)(false);
    const firstUpdate = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(() => {
        setAnimation(true);
    }, []);
    const onInputChanged = (val) => {
        if (pickerProps.onChange) {
            pickerProps.onChange(Object.assign(Object.assign({}, pickerProps.details), val));
        }
    };
    const onClose = () => {
        if (pickerProps.onRemove) {
            setAnimation(false);
            setTimeout(() => {
                var _a;
                (_a = pickerProps.onRemove) === null || _a === void 0 ? void 0 : _a.call(pickerProps);
            }, 500);
        }
    };
    const adultsChangeHandler = (val) => {
        onInputChanged({ adults: val });
    };
    const childrenChangeHandler = (val) => {
        onInputChanged({ children: val });
    };
    const infantChangeHandler = (val) => {
        onInputChanged({ infants: val });
    };
    const { getMaxSelectableAdults, showChildren, getMaxSelectableChildren, getMaxSelectableInfants } = (0, useSearchScopeHook_1.useSearchScopeHook)();
    const maxAdults = getMaxSelectableAdults({ adults, children, infants });
    const maxChildren = getMaxSelectableChildren({ adults });
    const maxInfants = getMaxSelectableInfants({ adults });
    const roomName = ((_b = (_a = hotel === null || hotel === void 0 ? void 0 : hotel.rooms) === null || _a === void 0 ? void 0 : _a[roomCode]) === null || _b === void 0 ? void 0 : _b.name) || '';
    const containerRef = (0, react_1.useRef)(null);
    const [containerHeight, setContainerHeight] = (0, react_1.useState)(0);
    (0, react_1.useEffect)(() => {
        const resizeObserver = new ResizeObserver((entries) => {
            for (const entry of entries) {
                setContainerHeight(entry.contentRect.height);
            }
        });
        if (containerRef.current) {
            resizeObserver.observe(containerRef.current);
        }
        return () => {
            if (containerRef.current) {
                resizeObserver.unobserve(containerRef.current);
            }
        };
    }, [containerRef.current]);
    const heightProps = (0, web_1.useSpring)({
        height: containerHeight + 33, // 33 is for <LineBreak>
        opacity: animatedIn ? 1 : 0,
    });
    return (react_1.default.createElement(web_1.animated.div, { ref: firstUpdate, style: heightProps },
        react_1.default.createElement("div", { className: "people-picker--item", ref: containerRef },
            pickerProps.number > 0 && (react_1.default.createElement("div", { className: "people-picker--item-row" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey },
                    react_1.default.createElement("strong", null,
                        t(Translation_1.Translation.Misc.Room),
                        "\u00A0",
                        pickerProps.number + 1)),
                react_1.default.createElement("div", { className: "people-picker--item-remove", onClick: onClose },
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Close, color: Color_1.Color.Grey })))),
            roomName && (react_1.default.createElement("div", { className: "people-picker--item-row" },
                react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Date.Room)),
                react_1.default.createElement(Text_1.default, { className: "room-name" }, roomName))),
            react_1.default.createElement("div", null,
                react_1.default.createElement("div", { className: "people-picker--item-row" },
                    react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Date.Adult_plural)),
                    react_1.default.createElement(NumberIncrement_1.default, { number: adults, min: 1, max: maxAdults, onChange: adultsChangeHandler })),
                react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: hotel === null || hotel === void 0 ? void 0 : hotel.minAdultAge, max: hotel === null || hotel === void 0 ? void 0 : hotel.maxAdultAge })),
            showChildren ? (react_1.default.createElement("div", null,
                react_1.default.createElement("div", { className: "people-picker--item-row" },
                    react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Date.Child_plural)),
                    react_1.default.createElement(NumberIncrement_1.default, { number: children, min: 0, max: maxChildren, onChange: childrenChangeHandler })),
                react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: (_d = (_c = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _c === void 0 ? void 0 : _c.childRange) === null || _d === void 0 ? void 0 : _d.min, max: (_f = (_e = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _e === void 0 ? void 0 : _e.childRange) === null || _f === void 0 ? void 0 : _f.max }))) : null,
            ((_g = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _g === void 0 ? void 0 : _g.supportInfants) ? (react_1.default.createElement("div", null,
                react_1.default.createElement("div", { className: "people-picker--item-row" },
                    react_1.default.createElement(Text_1.default, null, t(Translation_1.Translation.Step.Date.Infant_plural)),
                    react_1.default.createElement(NumberIncrement_1.default, { number: infants, min: 0, max: maxInfants, onChange: infantChangeHandler })),
                react_1.default.createElement(AgedBetweenLabel_1.AgedBetweenLabel, { min: (_j = (_h = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _h === void 0 ? void 0 : _h.infantRange) === null || _j === void 0 ? void 0 : _j.min, max: (_l = (_k = hotel === null || hotel === void 0 ? void 0 : hotel.childConfiguration) === null || _k === void 0 ? void 0 : _k.infantRange) === null || _l === void 0 ? void 0 : _l.max }))) : null),
        react_1.default.createElement(LineBreak_1.default, { smallMargins: true })));
}
//# sourceMappingURL=PeoplePickerRow.js.map