var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import React from 'react';
import { default as ApplePay } from '../lib/wallets/ApplePay';
import { Alert } from './Alert';
export function withDigitalWallet(Component) {
    return class DigitalWalletProvider extends React.Component {
        constructor(props) {
            super(props);
            this.promises = [];
            this.handleApplePaySetup = () => __awaiter(this, void 0, void 0, function* () {
                return this.applePay.setupApplePay()
                    .then(success => {
                    if (success) {
                        this.setState({
                            showApplePayButton: true,
                            showApplePaySetupButton: false
                        });
                    }
                    else {
                        Alert.alert('Apple Pay was unable to complete your request.');
                    }
                })
                    .catch(e => {
                    console.error(e);
                    Alert.alert('Apple Pay was unable to complete your request.');
                });
            });
            this.applePay = new ApplePay(props.applePayMerchantIdentifier);
            this.promises.push(this.initApplePay());
        }
        initApplePay() {
            return __awaiter(this, void 0, void 0, function* () {
                return new Promise((resolve, reject) => {
                    if (this.applePay.isEnabled()) {
                        this.applePay.hasActiveCard()
                            .then(hasActiveCard => {
                            if (hasActiveCard) {
                                this.setState({
                                    showApplePayButton: true,
                                    showApplePaySetupButton: false
                                });
                                return resolve();
                            }
                            else if (this.applePay.canCallSetup()) {
                                this.setState({
                                    showApplePayButton: false,
                                    showApplePaySetupButton: true
                                });
                                return resolve();
                            }
                        })
                            .catch(e => {
                            return reject(e);
                        });
                    }
                });
            });
        }
        componentDidMount() {
            Promise.all(this.promises)
                .catch(e => {
                console.error(e);
            });
        }
        render() {
            return (<Component {...this.props} {...this.state} applePaySetupPress={this.handleApplePaySetup}/>);
        }
    };
}
//# sourceMappingURL=DigitalWalletProvider.js.map