"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.BE_PEOPLE_PICKER_BLOCK = void 0;
exports.OriginalPeoplePickerBlock = OriginalPeoplePickerBlock;
exports.default = BEPeoplePickerBlock;
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 Icon_1 = require("../../generic/Icon/Icon");
const PromoCodeInput_1 = __importDefault(require("../../generic/PromoCodeInput"));
const OccupancyWarningBlock_1 = __importDefault(require("./OccupancyWarningBlock"));
const PeoplePickerRow_1 = __importDefault(require("./PeoplePickerRow"));
const BasketRow_1 = __importStar(require("../../../models/BasketRow"));
const DIProvider_1 = require("../../../providers/DIProvider");
const DataLayer_1 = __importStar(require("../../../util/DataLayer"));
exports.BE_PEOPLE_PICKER_BLOCK = 'BEPeoplePickerBlock';
function OriginalPeoplePickerBlock() {
    var _a, _b;
    const context = (0, react_1.useContext)(contexts_1.BasketContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const updateRoomValues = (row, values) => {
        const { adults, children, infants, promoCode } = values;
        if (adults !== row.getAdults()) {
            row.setAdults(adults);
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.DATES);
        }
        if (children !== row.getChildren()) {
            row.setChildren(children);
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.DATES);
        }
        if (infants !== row.getInfants()) {
            row.setInfants(infants);
            DataLayer_1.default.instance.sendInteraction('Guests', DataLayer_1.InteractionType.NUMBER_INPUT, DataLayer_1.InteractionStep.DATES);
        }
        if (promoCode !== row.getPromoCode()) {
            DataLayer_1.default.instance.sendInteraction('Promo Code', DataLayer_1.InteractionType.FORM_FIELD, DataLayer_1.InteractionStep.DATES);
            row.setPromoCode(promoCode);
        }
        context.updateBasketRow(row);
    };
    const basketRows = context.currentBasketRows;
    const { t } = (0, react_i18next_1.useTranslation)();
    const addNewBasketRow = () => {
        context.addBasketRow(new BasketRow_1.default());
    };
    const updatePromoCode = (code) => {
        if (context.selectedBasketRow) {
            context.selectedBasketRow.setManualInputPromoCode(!!true);
            context.selectedBasketRow.setPromoCode(code);
            context.updateBasketRow(context.selectedBasketRow);
        }
    };
    const selectedRow = context.selectedBasketRow;
    const promoCodeStatus = (_a = selectedRow === null || selectedRow === void 0 ? void 0 : selectedRow.getPromoCodeStatus()) !== null && _a !== void 0 ? _a : null;
    const addAnotherRoomOnClickHandler = () => {
        DataLayer_1.default.instance.sendInteraction('Add Another Room', DataLayer_1.InteractionType.BUTTON, DataLayer_1.InteractionStep.DATES);
        addNewBasketRow();
    };
    const changePromoCode = (val) => {
        updatePromoCode(val);
    };
    const promocode = (_b = context.selectedBasketRow) === null || _b === void 0 ? void 0 : _b.getPromoCode(hotel === null || hotel === void 0 ? void 0 : hotel.memberOnlyPromoCode);
    return (react_1.default.createElement("div", { className: "people-picker--items" },
        basketRows.map((item, index) => {
            const changeRoomValues = (val) => {
                updateRoomValues(item, val);
            };
            const removeBasketRowHandler = () => {
                context.removeBasketRow(item);
            };
            return (react_1.default.createElement("div", { key: item.getID() },
                react_1.default.createElement(PeoplePickerRow_1.default, { number: index, details: {
                        adults: item.getAdults(),
                        children: item.getChildren(),
                        infants: item.getInfants(),
                        promoCode: item.getPromoCode(),
                        roomCode: item.getRoomCode(),
                    }, onChange: changeRoomValues, onRemove: removeBasketRowHandler })));
        }),
        react_1.default.createElement("div", { className: "u-marg-bottom" },
            react_1.default.createElement(PromoCodeInput_1.default, { value: promocode, onChange: changePromoCode, valid: !promocode || promoCodeStatus === null || promoCodeStatus === BasketRow_1.EPromoCodeStatus.New ? undefined : promoCodeStatus === BasketRow_1.EPromoCodeStatus.Valid, variant: "outlined", row: context.selectedBasketRow })),
        context.canAddAnotherRoom ? (react_1.default.createElement(BEButton_1.default, { isText: true, primary: true, icon: Icon_1.IconType.Add, stopIconAnimation: true, onClick: addAnotherRoomOnClickHandler }, t(Translation_1.Translation.Misc.AddAnotherRoom))) : (react_1.default.createElement(react_1.default.Fragment, null)),
        react_1.default.createElement(OccupancyWarningBlock_1.default, null)));
}
function BEPeoplePickerBlock() {
    return (0, DIProvider_1.withDI)(exports.BE_PEOPLE_PICKER_BLOCK, OriginalPeoplePickerBlock, null);
}
//# sourceMappingURL=BEPeoplePickerBlock.js.map