"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 = AdditionalOffers;
const contexts_1 = require("../../../../contexts/index.js");
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 react_use_gesture_1 = require("react-use-gesture");
const react_use_measure_1 = __importDefault(require("react-use-measure"));
const Translation_1 = require("translations/Translation");
const AutoAutoHeight_1 = __importDefault(require("../../../../animations/AutoAutoHeight"));
const BookingAPI_1 = __importDefault(require("../../../../api/BookingAPI"));
const Headline_1 = __importDefault(require("../../../generic/Headline"));
const Icon_1 = __importStar(require("../../../generic/Icon/Icon"));
const LargeLoader_1 = __importDefault(require("../../../generic/loader/LargeLoader"));
const Text_1 = __importDefault(require("../../../generic/Text"));
const AdditionalOffersColumn_1 = __importDefault(require("./AdditionalOffersColumn"));
const AdditionalOffersModal_1 = __importDefault(require("./AdditionalOffersModal"));
const Color_1 = require("../../../../util/Color");
function AdditionalOffers() {
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const ccx = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const [{ x }, set] = (0, web_1.useSpring)(() => ({ x: 0 }));
    const [ref, bounds] = (0, react_use_measure_1.default)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const [addons, setAddons] = react_1.default.useState([]);
    const [isModalOpen, setIsModalOpen] = (0, react_1.useState)(false);
    const [selectedAddon, setSelectedAddon] = (0, react_1.useState)(null);
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const [lastReleaseX, setLastReleaseX] = (0, react_1.useState)(0);
    const [lastIndex, setLastIndex] = (0, react_1.useState)(0);
    // -1 can't go back
    // 0 default,
    // 1 can't go forward;
    const [buttonDisabled, setButtonDisabled] = (0, react_1.useState)(-1);
    react_1.default.useEffect(() => {
        let isCancelled = false;
        const rows = basketContext.getAllValidRows();
        setIsLoading(true);
        BookingAPI_1.default.Addons.findAddons(rows, hotel === null || hotel === void 0 ? void 0 : hotel.hotelID).then((foundedAddons) => {
            if (!isCancelled) {
                setIsLoading(false);
                setAddons(foundedAddons);
            }
        });
        return () => {
            setIsLoading(false);
            isCancelled = true;
        };
    }, [ccx.currentLanguage]);
    const cardWidth = 222;
    const isScrollable = cardWidth * addons.length >= bounds.width;
    const bind = (0, react_use_gesture_1.useDrag)(({ down, movement: [mx] }) => {
        animateTrack(down, mx);
    });
    const animateTrack = (down, mx) => {
        if (!isScrollable)
            return;
        if (!down) {
            const releasePosition = lastReleaseX + mx;
            const maxX = (addons.length - 1) * cardWidth;
            let restingLocation = 0;
            if (releasePosition > 0) {
                restingLocation = 0;
                setButtonDisabled(-1);
            }
            else if (releasePosition < -(maxX - bounds.width + cardWidth)) {
                restingLocation = -(maxX - bounds.width + cardWidth);
                setButtonDisabled(1);
            }
            else {
                const magic = -releasePosition / cardWidth;
                const lastTempIndex = releasePosition % cardWidth > -cardWidth / 2 ? Math.floor(magic) : Math.ceil(magic);
                restingLocation = -(lastTempIndex * cardWidth);
                setLastIndex(lastTempIndex);
                setButtonDisabled(0);
            }
            set({ x: restingLocation });
            setLastReleaseX(restingLocation);
        }
        else {
            set({ x: lastReleaseX + mx * 1.5 });
        }
    };
    const nextImage = () => {
        animateTrack(false, -cardWidth);
    };
    const prevImage = () => {
        animateTrack(false, cardWidth);
    };
    const closeModalOnClose = () => {
        setIsModalOpen(false);
    };
    return (react_1.default.createElement(AutoAutoHeight_1.default, { open: (!isLoading && (addons === null || addons === void 0 ? void 0 : addons.length) !== 0) || !!(hotel === null || hotel === void 0 ? void 0 : hotel.showAddonLoader) },
        react_1.default.createElement("div", { className: "u-pad-bottom--heavy " },
            react_1.default.createElement("div", { className: "u-marg-bottom" },
                react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Step.Confirmation.AdditionalOfferings))),
            isLoading && (hotel === null || hotel === void 0 ? void 0 : hotel.showAddonLoader) ? (react_1.default.createElement(LargeLoader_1.default, null)) : (addons === null || addons === void 0 ? void 0 : addons.length) === 0 ? (react_1.default.createElement("div", null,
                react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey }, t(Translation_1.Translation.Step.Confirmation.NoAdditionalOfferings)))) : (react_1.default.createElement("div", { className: 'additional-offers' + (addons.length > 2 ? '' : ' --wide'), ref: ref },
                isScrollable && (react_1.default.createElement("div", { className: "additional-offers--navigation" },
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.ArrowLeft2, onClick: prevImage, color: buttonDisabled === -1 ? Color_1.Color.DarkGrey : Color_1.Color.White, backgroundColor: Color_1.Color.Navy, borderRadius: "round", size: "24px" }),
                    react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.ArrowRight2, onClick: nextImage, color: buttonDisabled === 1 ? Color_1.Color.DarkGrey : Color_1.Color.White, backgroundColor: Color_1.Color.Navy, borderRadius: "round", size: "24px" }))),
                react_1.default.createElement("div", { className: "additional-offers--track-container" },
                    react_1.default.createElement(web_1.animated.div, Object.assign({ className: "additional-offers--track" }, bind(), { style: { left: x } }), addons.map((addon, index) => {
                        const onCardClickedHandler = () => {
                            setIsModalOpen(true);
                            setSelectedAddon(addon);
                        };
                        return react_1.default.createElement(AdditionalOffersColumn_1.default, { selected: !!basketContext.getRowForAddon(addon), key: index, addon: addon, cardClicked: onCardClickedHandler });
                    }))))),
            react_1.default.createElement(AdditionalOffersModal_1.default, { isOpen: isModalOpen && !!selectedAddon, addon: selectedAddon, basketAddonRow: basketContext.getRowForAddon(selectedAddon), onClose: closeModalOnClose }))));
}
//# sourceMappingURL=AdditionalOffers.js.map