"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const contexts_1 = require("../contexts");
const Currency_1 = __importDefault(require("./Currency"));
const Planpay_1 = __importDefault(require("./Planpay"));
const usePlanpayRawPrice = ({ price: priceProp, checkin: checkinProp, currency: currencyProp }) => {
    var _a;
    const basketContext = (0, contexts_1.useBasket)();
    const { planpay, planpayConfig, hasPlanpay } = (0, Planpay_1.default)();
    const { currentCurrency } = (0, react_1.useContext)(contexts_1.CompanyContext);
    const currency = currencyProp !== null && currencyProp !== void 0 ? currencyProp : currentCurrency;
    const price = basketContext.getTotalPrice() || priceProp;
    const { value } = (0, Currency_1.default)({
        children: price,
    });
    const checkin = checkinProp !== null && checkinProp !== void 0 ? checkinProp : (((_a = basketContext.selectedBasketRow) === null || _a === void 0 ? void 0 : _a.getStartDate()) || basketContext.endDate).format('YYYY-MM-DD');
    const [data, setData] = (0, react_1.useState)(undefined);
    (0, react_1.useEffect)(() => {
        if (hasPlanpay && value) {
            const getRawPrice = () => __awaiter(void 0, void 0, void 0, function* () {
                const data = yield planpay.pricePreview.fetch({
                    totalCost: price,
                    merchantId: planpayConfig.username,
                    currencyCode: currency,
                    redemptionDate: checkin,
                });
                setData(data);
            });
            getRawPrice();
        }
    }, [value, hasPlanpay, currency, price]);
    return data;
};
exports.default = usePlanpayRawPrice;
//# sourceMappingURL=usePlanpayRawPrice.js.map