"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 = AdditionalOffersModal;
const contexts_1 = require("../../../../contexts/index.js");
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const Currency_1 = __importDefault(require("../../../generic/Currency"));
const Headline_1 = __importDefault(require("../../../generic/Headline"));
const HtmlContentViewer_1 = require("../../../generic/HtmlContentViewer");
const SimpleModal_1 = __importStar(require("../../../generic/modal/SimpleModal"));
const NumberIncrement_1 = __importDefault(require("../../../generic/NumberIncrement"));
const Select_1 = require("../../../generic/Select/Select");
const Text_1 = __importStar(require("../../../generic/Text"));
const ImageProvider_1 = __importStar(require("../../../../providers/ImageProvider"));
const Color_1 = require("../../../../util/Color");
function AdditionalOffersModal(props) {
    var _a, _b;
    const { addon } = props;
    const { t } = (0, react_i18next_1.useTranslation)();
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const [selectedDate, setSelectedDate] = (0, react_1.useState)(props.basketAddonRow ? props.basketAddonRow.getSelectedDate() : '');
    const [selectedTime, setSelectedTime] = (0, react_1.useState)(props.basketAddonRow ? props.basketAddonRow.getSelectedTime() : '');
    const [priceInfo, setPriceInfo] = (0, react_1.useState)({ quantity: 1 });
    const [quantity, setQuantity] = (0, react_1.useState)(props.basketAddonRow ? props.basketAddonRow.getQuantity() : 1);
    const [adultQuantity, setAdultQuantity] = (0, react_1.useState)(props.basketAddonRow ? props.basketAddonRow.getAdultQuantity() : 1);
    const [childQuantity, setChildQuantity] = (0, react_1.useState)(props.basketAddonRow ? props.basketAddonRow.getChildQuantity() : 0);
    react_1.default.useEffect(() => {
        var _a, _b, _c, _d, _e, _f;
        if (addon) {
            if (props.basketAddonRow) {
                setPriceInfo((_a = addon.availability[props.basketAddonRow.getSelectedDate()]) !== null && _a !== void 0 ? _a : addon.availability[addon.firstDate]);
                setSelectedDate((_b = props.basketAddonRow.getSelectedDate()) !== null && _b !== void 0 ? _b : addon.firstDate);
                setSelectedTime((_c = props.basketAddonRow.getSelectedTime()) !== null && _c !== void 0 ? _c : '');
                setQuantity(props.basketAddonRow.getQuantity());
                setAdultQuantity(props.basketAddonRow.getAdultQuantity());
                setChildQuantity(props.basketAddonRow.getChildQuantity());
            }
            else {
                setPriceInfo(addon.availability[addon.firstDate]);
                setSelectedDate(addon.firstDate);
                setSelectedTime(((_f = (_e = (_d = addon.availability[addon.firstDate]) === null || _d === void 0 ? void 0 : _d.selectorTimes) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.value) || '');
            }
            return () => {
                setQuantity(1);
                setAdultQuantity(1);
                setChildQuantity(0);
            };
        }
    }, [addon]);
    const setDate = (date) => {
        var _a, _b, _c;
        setPriceInfo(addon.availability[date]);
        setSelectedDate(date);
        setSelectedTime(((_c = (_b = (_a = addon.availability[date]) === null || _a === void 0 ? void 0 : _a.selectorTimes) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.value) || '');
        if (addon.availability[date].quantity < quantity) {
            setQuantity(addon.availability[date].quantity);
        }
    };
    const onClickAddToCartHandler = () => {
        basketContext.setAddonQuantities(addon, {
            quantity,
            adult: adultQuantity,
            child: childQuantity,
        }, selectedDate, selectedTime);
        props.onClose();
    };
    const setQuantityOnChange = (val) => {
        setQuantity(val);
    };
    const setAdultsQuantityOnChange = (val) => {
        setAdultQuantity(val);
    };
    const setChildQuantityOnChange = (val) => {
        setChildQuantity(val);
    };
    const setDateOnChange = (selected) => {
        setDate(selected);
    };
    const setSelectedTimeOnChange = (selected) => {
        setSelectedTime(selected);
    };
    return (react_1.default.createElement(SimpleModal_1.default, { open: props.isOpen, onClose: props.onClose, size: SimpleModal_1.SimpleModalSize.Small, title: addon ? addon.name : '' }, addon && (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "rs-additional-offers-modal-image" }, ((_a = addon === null || addon === void 0 ? void 0 : addon.image) === null || _a === void 0 ? void 0 : _a.length) ? (react_1.default.createElement("img", { src: ImageProvider_1.default.resizeImage(addon.image, ImageProvider_1.ImageSize.Original), alt: addon.name || 'Addon Image', style: {
                width: '100%',
                height: 'auto',
                display: 'block',
            } })) : (react_1.default.createElement("div", { style: { height: '350px', width: '100%' } }))),
        react_1.default.createElement("div", { className: "u-marg-heavy u-marg@m-" },
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                react_1.default.createElement(Headline_1.default, { bold: true }, addon.name)),
            react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.DarkGrey, asDiv: true },
                    react_1.default.createElement(HtmlContentViewer_1.HtmlContentViewer, { content: addon.description }))),
            react_1.default.createElement("div", { className: "row u-marg-top" },
                react_1.default.createElement("div", { className: "u-flex col-md-6" },
                    react_1.default.createElement(Text_1.default, null, "Date"),
                    react_1.default.createElement(Select_1.Select, { nativeOnMobile: true, options: addon.selectorDates, value: selectedDate, onChange: setDateOnChange, keyName: "value", labelName: "text", className: "u-flex align-items-center" }))),
            ((_b = priceInfo === null || priceInfo === void 0 ? void 0 : priceInfo.selectorTimes) === null || _b === void 0 ? void 0 : _b.length) !== 0 && (react_1.default.createElement("div", { className: "row u-marg-top" },
                react_1.default.createElement("div", { className: "u-flex col-md-6" },
                    react_1.default.createElement(Text_1.default, null, "Time"),
                    react_1.default.createElement(Select_1.Select, { nativeOnMobile: true, options: (priceInfo === null || priceInfo === void 0 ? void 0 : priceInfo.selectorTimes) || [], value: selectedTime, onChange: setSelectedTimeOnChange, keyName: "value", labelName: "text", className: "u-flex align-items-center" })))),
            priceInfo.price && (react_1.default.createElement("div", { className: "row u-marg-top" },
                react_1.default.createElement("div", { className: "u-flex col-md-6" },
                    react_1.default.createElement(Text_1.default, null, "Quantity"),
                    react_1.default.createElement(NumberIncrement_1.default, { number: quantity, min: 1, max: priceInfo.quantity, onChange: setQuantityOnChange })))),
            priceInfo.adultPrice && (react_1.default.createElement("div", { className: "row u-marg-top" },
                react_1.default.createElement("div", { className: "u-flex col-md-6" },
                    react_1.default.createElement(Text_1.default, null, "Adults"),
                    react_1.default.createElement(NumberIncrement_1.default, { number: adultQuantity, min: 0, max: priceInfo.quantity - childQuantity, onChange: setAdultsQuantityOnChange })))),
            priceInfo.childPrice && (react_1.default.createElement("div", { className: "row u-marg-top" },
                react_1.default.createElement("div", { className: "u-flex col-md-6" },
                    react_1.default.createElement(Text_1.default, null, "Children"),
                    react_1.default.createElement(NumberIncrement_1.default, { number: childQuantity, min: 0, max: priceInfo.quantity - adultQuantity, onChange: setChildQuantityOnChange })))),
            react_1.default.createElement("div", { className: "u-flex align-items-center u-marg-top" },
                priceInfo.price && (react_1.default.createElement("h2", { className: "u-marg-none u-flex u-flex-align-center" },
                    react_1.default.createElement(Currency_1.default, null, priceInfo.price),
                    "/ Qty")),
                priceInfo.adultPrice && priceInfo.childPrice && (react_1.default.createElement("h2", { className: "u-marg-none" },
                    react_1.default.createElement("div", { className: "u-marg-none u-flex u-flex-align-center" },
                        react_1.default.createElement(Currency_1.default, null, priceInfo.adultPrice),
                        " / Adult"),
                    react_1.default.createElement("br", null),
                    react_1.default.createElement("div", { className: "u-marg-none u-flex u-flex-align-center" },
                        react_1.default.createElement(Currency_1.default, null, priceInfo.childPrice),
                        " / Child"))),
                priceInfo.adultPrice && !priceInfo.childPrice && (react_1.default.createElement("h2", { className: "u-marg-none u-flex u-flex-align-center" },
                    react_1.default.createElement(Currency_1.default, null, priceInfo.adultPrice),
                    " / Adult")),
                priceInfo.childPrice && !priceInfo.adultPrice && (react_1.default.createElement("h2", { className: "u-marg-none u-flex u-flex-align-center" },
                    react_1.default.createElement(Currency_1.default, null, priceInfo.childPrice),
                    " / Child")),
                react_1.default.createElement(BEButton_1.default, { filled: true, primary: true, onClick: onClickAddToCartHandler }, props.basketAddonRow ? 'Update Cart' : 'Add to Cart')),
            addon.guaranteeDescription && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("h4", { className: "u-marg-bottom--light u-marg-top" }, "Guarantee"),
                react_1.default.createElement("div", { className: "u-marg-bot--heavy" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, addon.guaranteeDescription)))),
            addon.cancelPenalty && (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement("h4", { className: "u-marg-bottom--light u-marg-top" }, "Cancellation Policy"),
                react_1.default.createElement("div", { className: "u-marg-bot--heavy" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, addon.cancelPenalty)))))))));
}
//# sourceMappingURL=AdditionalOffersModal.js.map