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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useState } from 'react';
import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
import { NavigationColumn } from './ui/commons/NavigationColumn';
import { WalletSelector } from './ui/WalletSelector/WalletSelector';
import { EmojiForm } from './ui/Notifications/EmojiForm';
import { generateCode, TEST_CONTRACT_ADDRESS, TEST_PRIVATE_KEY, TEST_ACCOUNT_ADDRESS, TEST_MESSAGE_HASH, TEST_TRANSACTION_HASH } from '@universal-login/commons';
import { EmojiPanel } from './ui/WalletSelector/EmojiPanel';
import { Settings } from './ui/Settings/Settings';
import { Onboarding } from './ui/Onboarding/Onboarding';
import { useServices } from './core/services/useServices';
import { LogoButton } from './ui/UFlow/LogoButton';
import { CreateRandomInstance } from './ui/commons/CreateRandomInstance';
import './ui/styles/playground.css';
import { DeployedWallet, DeployingWallet, setBetaNotice } from '@universal-login/sdk';
import { Spinner } from './ui/commons/Spinner';
import { useAsync } from './ui/hooks/useAsync';
import { WalletService } from '@universal-login/sdk';
import { mockNotifications, CONNECTION_REAL_ADDRESS } from './ui/PlaygroundUtils/mockNotifications';
import { ModalWrapper } from './ui/Modals/ModalWrapper';
import { WaitingForOnRampProvider } from './ui/TopUp/Fiat/WaitingForOnRampProvider';
import { TopUp } from './ui/TopUp/TopUp';
import { WaitingForTransaction } from './ui/commons/WaitingForTransaction';
import { ThemesPlayground } from './ui/Playground/ThemesPlayground';
import { ThemeProvider } from './ui/themes/Theme';
export var App = function () {
    var sdk = useServices().sdk;
    var _a = __read(useAsync(function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, sdk.fetchRelayerConfig()];
                case 1:
                    _a.sent();
                    return [2 /*return*/, sdk.getRelayerConfig()];
            }
        });
    }); }, []), 1), relayerConfig = _a[0];
    setBetaNotice(sdk);
    var _b = __read(useState(function () { return new WalletService(sdk); }), 1), walletService = _b[0];
    var name = 'test.mylogin.eth';
    var deployingWallet = new DeployingWallet({
        contractAddress: TEST_ACCOUNT_ADDRESS,
        name: name,
        privateKey: TEST_PRIVATE_KEY,
        deploymentHash: TEST_MESSAGE_HASH,
    }, sdk);
    var futureWallet = {
        contractAddress: TEST_ACCOUNT_ADDRESS,
        privateKey: TEST_PRIVATE_KEY,
        deploy: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/, deployingWallet];
        }); }); },
        waitForBalance: (function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/];
        }); }); }),
        setSupportedToken: (function () { }),
    };
    function tryEnablingMetamask() {
        return __awaiter(this, void 0, void 0, function () {
            var ethereum, error_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        ethereum = window.ethereum;
                        if (!ethereum) return [3 /*break*/, 4];
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, ethereum.enable()];
                    case 2:
                        _a.sent();
                        return [2 /*return*/, ethereum.selectedAddress];
                    case 3:
                        error_1 = _a.sent();
                        return [3 /*break*/, 4];
                    case 4: return [2 /*return*/];
                }
            });
        });
    }
    return (React.createElement(BrowserRouter, null,
        React.createElement(ThemeProvider, null,
            React.createElement("div", { className: "playground" },
                React.createElement(NavigationColumn, null),
                React.createElement("div", { className: "playground-content" },
                    React.createElement(Switch, null,
                        React.createElement(Route, { exact: true, path: "/", render: function () { return (React.createElement("p", null, "Welcome to Universal Login")); } }),
                        React.createElement(Route, { exact: true, path: "/themes", render: function () { return React.createElement(ThemesPlayground, null); } }),
                        React.createElement(Route, { exact: true, path: "/logoButton" },
                            React.createElement("div", null,
                                React.createElement("button", { onClick: function () { return mockNotifications(sdk); } }, "Create mock notifications"),
                                React.createElement("hr", null),
                                React.createElement(CreateRandomInstance, { walletService: walletService }),
                                React.createElement("hr", null),
                                React.createElement(LogoButton, { walletService: walletService }))),
                        React.createElement(Route, { exact: true, path: "/onboarding/success", render: function (_a) {
                                var history = _a.history;
                                return React.createElement(React.Fragment, null,
                                    React.createElement("div", null, " Success!!! "),
                                    React.createElement("br", null),
                                    React.createElement("button", { onClick: function () {
                                            walletService.disconnect();
                                            history.push('/onboarding');
                                        } }, "Disconnect"));
                            } }),
                        React.createElement(Route, { exact: true, path: "/onboarding", render: function (_a) {
                                var history = _a.history;
                                return React.createElement(Onboarding, { sdk: sdk, walletService: walletService, domains: ['mylogin.eth', 'universal-id.eth', 'poppularapp.eth'], tryEnablingMetamask: tryEnablingMetamask, onConnect: function () { return console.log('connected'); }, onCreate: function () { return history.push('/onboarding/success'); }, hideModal: function () { return history.push('/'); } });
                            } }),
                        React.createElement(Route, { exact: true, path: "/walletSelector" },
                            React.createElement(WalletSelector, { onCreateClick: function () { console.log('create'); }, onConnectClick: function () { console.log('connect'); }, sdk: sdk, domains: ['mylogin.eth', 'myapp.eth'] })),
                        React.createElement(Route, { exact: true, path: "/keyboard", render: function () {
                                return relayerConfig ? (React.createElement("div", null,
                                    React.createElement(EmojiPanel, { code: generateCode(CONNECTION_REAL_ADDRESS) }),
                                    React.createElement("hr", null),
                                    React.createElement(EmojiForm, { notifications: [], onDenyClick: function () { }, setPublicKey: function () { } }))) : React.createElement(Spinner, null);
                            } }),
                        React.createElement(Route, { exact: true, path: "/topUp", render: function () {
                                return React.createElement("div", null,
                                    React.createElement(Link, { to: "/topUpRegular" }, "Regular"),
                                    React.createElement("br", null),
                                    React.createElement(Link, { to: "/topUpDeployment" }, "Deployment"));
                            } }),
                        React.createElement(Route, { exact: true, path: "/topUpRegular", render: function () {
                                var topUpWalletService = new WalletService(sdk);
                                var topUpDeployedWallet = new DeployedWallet(TEST_CONTRACT_ADDRESS, 'bob.mylogin.eth', TEST_PRIVATE_KEY, sdk);
                                topUpWalletService.setWallet(topUpDeployedWallet.asApplicationWallet);
                                if (!relayerConfig) {
                                    return React.createElement(Spinner, null);
                                }
                                return React.createElement(TopUp, { walletService: topUpWalletService, hideModal: function () { return history.back(); }, isModal: true });
                            } }),
                        React.createElement(Route, { exact: true, path: "/topUpDeployment", render: function () {
                                var topUpWalletService = new WalletService(sdk);
                                topUpWalletService.setFutureWallet(futureWallet, name);
                                if (!relayerConfig) {
                                    return React.createElement(Spinner, null);
                                }
                                return React.createElement(TopUp, { walletService: topUpWalletService, hideModal: function () { return history.back(); }, isModal: true });
                            } }),
                        React.createElement(Route, { exact: true, path: "/settings", render: function () { return React.createElement(Settings, { deployedWallet: new DeployedWallet(TEST_CONTRACT_ADDRESS, 'bob.mylogin.eth', TEST_PRIVATE_KEY, sdk) }); } }),
                        React.createElement(Route, { exact: true, path: "/recover", render: function () { return (React.createElement("div", null,
                                React.createElement("p", null, "Recover"))); } }),
                        React.createElement(Route, { exact: true, path: "/waiting" },
                            React.createElement(ModalWrapper, null,
                                React.createElement(WaitingForOnRampProvider, { onRampProviderName: 'ramp' }))),
                        React.createElement(Route, { exact: true, path: "/waitForTransaction" },
                            React.createElement("div", null,
                                React.createElement(ThemesPlayground, null),
                                React.createElement(Link, { to: "/waitForTransactionWithHash" }, "With hash"),
                                React.createElement("br", null),
                                React.createElement(Link, { to: "/waitForTransactionWithoutHash" }, "Without hash"))),
                        React.createElement(Route, { path: "/waitForTransactionWithHash", render: function (_a) {
                                var location = _a.location;
                                if (!relayerConfig) {
                                    return React.createElement(Spinner, null);
                                }
                                else {
                                    return (React.createElement(ModalWrapper, { hideModal: function () { return console.log('hide modal'); }, message: "This is a test environment running on Ropsten network" },
                                        React.createElement(WaitingForTransaction, { action: "Waiting for transfer", relayerConfig: relayerConfig, transactionHash: TEST_TRANSACTION_HASH })));
                                }
                            } }),
                        React.createElement(Route, { path: "/waitForTransactionWithoutHash", render: function (_a) {
                                var location = _a.location;
                                if (!relayerConfig) {
                                    return React.createElement(Spinner, null);
                                }
                                else {
                                    return (React.createElement(ModalWrapper, { hideModal: function () { return console.log('hide modal'); }, message: "This is a test environment running on Ropsten network" },
                                        React.createElement(WaitingForTransaction, { action: "Waiting for transfer", relayerConfig: relayerConfig, transactionHash: undefined })));
                                }
                            } }),
                        React.createElement(Route, { component: function () { return (React.createElement("p", null, "not found")); } })))))));
};
export default App;
//# sourceMappingURL=App.js.map