"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var context_1 = require("../context");
var PaymentsAmount_1 = require("./PaymentsAmount");
var PaymentsLoginOrGuest_1 = require("./PaymentsLoginOrGuest");
var PaymentsOptions_1 = require("./PaymentsOptions");
var PaymentsEth_1 = require("./PaymentsEth");
var PaymentsStripe_1 = require("./PaymentsStripe");
var PaymentsApplePay_1 = require("./PaymentsApplePay");
var PaymentsGooglePay_1 = require("./PaymentsGooglePay");
var PaymentsWrapper_1 = require("./PaymentsWrapper");
var types_1 = require("./types");
var PaymentsSuccess_1 = require("./PaymentsSuccess");
var Payments = /** @class */ (function (_super) {
    __extends(Payments, _super);
    function Payments(props) {
        var _this = _super.call(this, props) || this;
        _this.handleUpdateState = function (paymentState) {
            if (paymentState === types_1.PAYMENT_STATE.STRIPE_PAYMENT && _this.state.usdToSpend < 2) {
                _this.setState({
                    paymentState: paymentState,
                    usdToSpend: 2,
                    selectedUsdToSpend: _this.state.usdToSpend,
                    paymentAdjustedStripe: true,
                    paymentAdjustedEth: true,
                });
            }
            else {
                _this.setState({ paymentState: paymentState, paymentAdjustedStripe: false, paymentAdjustedEth: false });
            }
        };
        _this.handleAmount = function (usdToSpend, shouldPublicize) {
            var paymentAdjustedWarning = usdToSpend < 2 ? true : false;
            if (_this.context && _this.context.currentUser) {
                _this.setState({
                    usdToSpend: usdToSpend,
                    paymentState: types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE,
                    shouldPublicize: shouldPublicize,
                    paymentAdjustedWarning: paymentAdjustedWarning,
                });
            }
            else {
                _this.setState({
                    usdToSpend: usdToSpend,
                    paymentState: types_1.PAYMENT_STATE.PAYMENT_CHOOSE_LOGIN_OR_GUEST,
                    shouldPublicize: shouldPublicize,
                    paymentAdjustedWarning: paymentAdjustedWarning,
                });
            }
        };
        _this.handleUpdateBoostFromEth = function (newUsdToSpend, selectedUsdToSpend, etherToSpend) {
            _this.setState({
                usdToSpend: newUsdToSpend,
                selectedUsdToSpend: selectedUsdToSpend,
                etherToSpend: etherToSpend,
                paymentAdjustedEth: true,
                paymentAdjustedStripe: false,
                resetEthPayments: true,
            });
        };
        _this.state = {
            usdToSpend: 0,
            paymentAdjustedWarning: false,
            paymentAdjustedStripe: false,
            paymentAdjustedEth: false,
            shouldPublicize: false,
            paymentState: types_1.PAYMENT_STATE.SELECT_AMOUNT,
            resetEthPayments: false,
        };
        return _this;
    }
    Payments.prototype.componentDidMount = function () {
        this.context.auth.ensureLoggedInUserEnabled();
    };
    Payments.prototype.render = function () {
        var _this = this;
        if (!this.context) {
            return React.createElement(React.Fragment, null);
        }
        var _a = this.state, usdToSpend = _a.usdToSpend, etherToSpend = _a.etherToSpend, shouldPublicize = _a.shouldPublicize, paymentState = _a.paymentState, selectedUsdToSpend = _a.selectedUsdToSpend, paymentAdjustedWarning = _a.paymentAdjustedWarning, paymentAdjustedStripe = _a.paymentAdjustedStripe, paymentAdjustedEth = _a.paymentAdjustedEth;
        var _b = this.props, postId = _b.postId, paymentAddress = _b.paymentAddress, newsroomName = _b.newsroomName, isStripeConnected = _b.isStripeConnected;
        var showWeb3Login = this.context.auth.showWeb3Login;
        // User logged in from PAYMENT_CHOOSE_LOGIN_OR_GUEST state, which will be reflected in context, and we should now show them SELECT_PAYMENT_TYPE state instead.
        var proceedToPaymentType = paymentState === types_1.PAYMENT_STATE.PAYMENT_CHOOSE_LOGIN_OR_GUEST && !!this.context.currentUser;
        if (paymentState === types_1.PAYMENT_STATE.PAYMENT_CHOOSE_LOGIN_OR_GUEST && !proceedToPaymentType) {
            return (React.createElement(PaymentsLoginOrGuest_1.PaymentsLoginOrGuest, { handleNext: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); }, handleLogin: showWeb3Login }));
        }
        if (proceedToPaymentType || paymentState === types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { usdToSpend: usdToSpend, newsroomName: newsroomName, paymentAdjustedWarning: paymentAdjustedWarning, handleEditAmount: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_AMOUNT); }, handleBack: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_AMOUNT); } },
                React.createElement(PaymentsOptions_1.PaymentsOptions, { postId: postId, usdToSpend: usdToSpend, isStripeConnected: isStripeConnected, renderContext: this.context.renderContext, handleNext: this.handleUpdateState })));
        }
        if (paymentState === types_1.PAYMENT_STATE.ETH_PAYMENT) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { usdToSpend: usdToSpend, newsroomName: newsroomName, paymentAdjustedEth: paymentAdjustedEth, selectedUsdToSpend: selectedUsdToSpend, etherToSpend: etherToSpend, handleBack: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } },
                React.createElement(PaymentsEth_1.PaymentsEth, { postId: postId, newsroomName: newsroomName, paymentAddress: paymentAddress, shouldPublicize: shouldPublicize, usdToSpend: usdToSpend, etherToSpend: this.state.etherToSpend, resetEthPayments: this.state.resetEthPayments, handleBoostUpdate: this.handleUpdateBoostFromEth, handlePaymentSuccess: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.PAYMENT_SUCCESS); }, handleEditPaymentType: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } })));
        }
        if (paymentState === types_1.PAYMENT_STATE.STRIPE_PAYMENT) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { usdToSpend: usdToSpend, newsroomName: newsroomName, paymentAdjustedStripe: paymentAdjustedStripe, selectedUsdToSpend: selectedUsdToSpend, handleBack: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } },
                React.createElement(PaymentsStripe_1.PaymentsStripe, { postId: postId, newsroomName: newsroomName, shouldPublicize: shouldPublicize, usdToSpend: usdToSpend, handlePaymentSuccess: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.PAYMENT_SUCCESS); }, handleEditPaymentType: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } })));
        }
        if (paymentState === types_1.PAYMENT_STATE.APPLE_PAY) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { usdToSpend: usdToSpend, newsroomName: newsroomName, handleBack: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } },
                React.createElement(PaymentsApplePay_1.PaymentsApplePay, { handleEditPaymentType: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); }, newsroomName: newsroomName, usdToSpend: usdToSpend })));
        }
        if (paymentState === types_1.PAYMENT_STATE.GOOGLE_PAY) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { usdToSpend: usdToSpend, newsroomName: newsroomName, handleBack: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); } },
                React.createElement(PaymentsGooglePay_1.PaymentsGooglePay, { handleEditPaymentType: function () { return _this.handleUpdateState(types_1.PAYMENT_STATE.SELECT_PAYMENT_TYPE); }, newsroomName: newsroomName, usdToSpend: usdToSpend })));
        }
        if (paymentState === types_1.PAYMENT_STATE.PAYMENT_SUCCESS) {
            return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { newsroomName: newsroomName },
                React.createElement(PaymentsSuccess_1.PaymentsSuccess, { newsroomName: newsroomName, usdToSpend: usdToSpend, handleClose: this.props.handleClose })));
        }
        return (React.createElement(PaymentsWrapper_1.PaymentsWrapper, { newsroomName: newsroomName, handleBack: this.props.handleClose },
            React.createElement(PaymentsAmount_1.PaymentsAmount, { newsroomName: newsroomName, suggestedAmounts: types_1.SuggestedPaymentAmounts, handleAmount: this.handleAmount })));
    };
    Payments.contextType = context_1.CivilContext;
    return Payments;
}(React.Component));
exports.Payments = Payments;
//# sourceMappingURL=Payments.js.map