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());
    });
};
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { SigningCyberClient } from '@cybercongress/cyber-js';
import configKeplr, { getKeplr } from './keplrUtils';
function createClient(signer) {
    return __awaiter(this, void 0, void 0, function* () {
        const cyber = (yield import('./config')).CYBER;
        const client = yield SigningCyberClient.connectWithSigner(cyber.CYBER_NODE_URL_API, signer);
        return client;
    });
}
const SignerClientContext = React.createContext({
    signer: undefined,
    signingClient: undefined,
    initSigner: () => { },
});
export function useSigningClient() {
    const signingClient = useContext(SignerClientContext);
    return signingClient;
}
function SigningClientProvider({ children }) {
    const [signer, setSigner] = useState();
    const [signingClient, setSigningClient] = useState();
    function initSigner() {
        return __awaiter(this, void 0, void 0, function* () {
            const cyber = (yield import('./config')).CYBER;
            const windowKeplr = yield getKeplr();
            if (windowKeplr && windowKeplr.experimentalSuggestChain) {
                yield windowKeplr.experimentalSuggestChain(yield configKeplr(cyber.BECH32_PREFIX_ACC_ADDR_CYBER));
                yield windowKeplr.enable(cyber.CHAIN_ID);
                const offlineSigner = yield windowKeplr.getOfflineSignerAuto(cyber.CHAIN_ID);
                console.log('offlineSigner', offlineSigner);
                const clientJs = yield createClient(offlineSigner);
                console.log('clientJs', clientJs);
                setSigner(offlineSigner);
                setSigningClient(clientJs);
            }
        });
    }
    useEffect(() => {
        const initWindowKeplr = () => __awaiter(this, void 0, void 0, function* () {
            const windowKeplr = yield getKeplr();
            if (windowKeplr) {
                initSigner();
            }
        });
        initWindowKeplr();
        window.addEventListener('keplr_keystorechange', () => {
            initSigner();
        });
    }, []);
    const value = useMemo(() => ({ initSigner, signer, signingClient }), [signer, signingClient]);
    return (React.createElement(SignerClientContext.Provider, { value: value }, children));
}
export default SigningClientProvider;
//# sourceMappingURL=signerClient.js.map