"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 __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 });
exports.TrueRewardsPaymentMethod = void 0;
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const Icon_1 = require("../../../generic/Icon/Icon");
const Text_1 = __importStar(require("../../../generic/Text"));
const TextBox_1 = __importDefault(require("../../../generic/TextBox"));
const Currency_1 = __importDefault(require("../../../../hooks/Currency"));
const index_1 = require("../../../../index");
const Translation_1 = require("../../../../translations/Translation");
const Color_1 = require("../../../../util/Color");
const TrueRewardsPaymentMethod_module_scss_1 = __importDefault(require("./TrueRewardsPaymentMethod.module.scss"));
const TrueRewardsPaymentMethod = ({ onApplied }) => {
    const { t } = (0, react_i18next_1.useTranslation)();
    const { hotel } = (0, index_1.useCurrentHotel)();
    const { getTotalPayNow } = (0, index_1.useBasket)();
    const totalPayNow = getTotalPayNow();
    const [cardReference, setCardReference] = (0, react_1.useState)('');
    const [cardBalance, setCardBalance] = (0, react_1.useState)(null);
    const [errorMessage, setErrorMessage] = (0, react_1.useState)('');
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const trimmedReference = cardReference.trim();
    const canSubmit = trimmedReference.length > 6;
    const remaining = cardBalance !== null && totalPayNow !== null ? cardBalance - totalPayNow : 0;
    const { symbol, prettyValue: remainingPrettyValue, isLoading: isRemainingLoading } = (0, Currency_1.default)({ children: remaining });
    const { prettyValue: cardBalancePrettyValue, isLoading: isCardBalanceLoading } = (0, Currency_1.default)({ children: cardBalance });
    const checkCardBalance = () => __awaiter(void 0, void 0, void 0, function* () {
        setErrorMessage('');
        setCardBalance(null);
        onApplied === null || onApplied === void 0 ? void 0 : onApplied(null, false);
        if (hotel === null || hotel === void 0 ? void 0 : hotel.hotelID) {
            try {
                setIsLoading(true);
                const balance = yield index_1.API.TrueRewards.fetchBalance(trimmedReference, hotel.hotelID);
                if (balance === null) {
                    setErrorMessage(t(Translation_1.Translation.Step.Confirmation.TrueRewardsInvalid));
                }
                else if (balance === 0) {
                    setErrorMessage(t(Translation_1.Translation.Step.Confirmation.TrueRewardsZeroBalance));
                }
                else {
                    setCardBalance(balance);
                    if (totalPayNow && totalPayNow > balance) {
                        onApplied === null || onApplied === void 0 ? void 0 : onApplied(balance, true);
                    }
                    else {
                        onApplied === null || onApplied === void 0 ? void 0 : onApplied(null, true);
                    }
                }
            }
            catch (e) {
                console.error(e);
                setErrorMessage(t(Translation_1.Translation.Step.Confirmation.TrueRewardsFailed));
            }
            finally {
                setIsLoading(false);
            }
        }
    });
    return (react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.container },
        react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.inputRow },
            react_1.default.createElement(TextBox_1.default, { value: cardReference, onChange: (val) => {
                    setCardReference(val.target.value);
                    setErrorMessage('');
                }, placeholder: t(Translation_1.Translation.Step.Confirmation.EnterCode) }),
            react_1.default.createElement(BEButton_1.default, { className: TrueRewardsPaymentMethod_module_scss_1.default.applyCodeButton, isLoading: isLoading, size: "small", onClick: checkCardBalance, disabled: !canSubmit, icon: cardBalance !== null ? Icon_1.IconType.Check : Icon_1.IconType.ArrowRight, iconPosition: "right", iconSize: "18px", rounded: true }, cardBalance !== null ? t(Translation_1.Translation.Step.Confirmation.CodeApplied) : t(Translation_1.Translation.Step.Confirmation.ApplyCode))),
        cardBalance !== null && !isCardBalanceLoading && !isRemainingLoading ? (react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.infoBlock },
            react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.leftContent },
                react_1.default.createElement(Text_1.default, { bold: true, type: Text_1.TextType.Small }, totalPayNow && cardBalance >= totalPayNow
                    ? t(Translation_1.Translation.Step.Confirmation.TrueRewardsCoversFullBooking)
                    : t(Translation_1.Translation.Step.Confirmation.TrueRewardsInsufficient)),
                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, totalPayNow && cardBalance >= totalPayNow
                    ? t(Translation_1.Translation.Step.Confirmation.TrueRewardsCoversFullInstructions)
                    : t(Translation_1.Translation.Step.Confirmation.TrueRewardsInsufficientInstructions))),
            react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.rightContent },
                react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.card },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Step.Confirmation.YourBalance)),
                    react_1.default.createElement(Text_1.default, { bold: true, color: Color_1.Color.Navy },
                        symbol,
                        cardBalancePrettyValue)),
                react_1.default.createElement("div", { className: TrueRewardsPaymentMethod_module_scss_1.default.card },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Step.Confirmation.Remaining)),
                    react_1.default.createElement(Text_1.default, { bold: true, color: remaining < 0 ? Color_1.Color.Danger : Color_1.Color.Navy },
                        symbol,
                        remainingPrettyValue))))) : (react_1.default.createElement(react_1.default.Fragment, null,
            errorMessage && (react_1.default.createElement(Text_1.default, { color: Color_1.Color.Danger, type: Text_1.TextType.Small, style: { marginTop: '8px' } }, errorMessage)),
            react_1.default.createElement(Text_1.default, { color: Color_1.Color.DarkGrey, type: Text_1.TextType.Small, style: { marginTop: '8px' } }, t(Translation_1.Translation.Step.Confirmation.TrueRewardsInputHelperText))))));
};
exports.TrueRewardsPaymentMethod = TrueRewardsPaymentMethod;
//# sourceMappingURL=TrueRewardsPaymentMethod.js.map