"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 CivilContext_1 = require("./CivilContext");
var core_1 = require("@joincivil/core");
var ethereum_react_1 = require("@kirby-web3/ethereum-react");
var react_apollo_1 = require("react-apollo");
var CivilInnerProvider = function (_a) {
    var children = _a.children, featureFlags = _a.featureFlags, config = _a.config, apolloClient = _a.apolloClient;
    // context
    var kirbyCtx = React.useContext(ethereum_react_1.KirbyEthereumContext);
    var web3 = kirbyCtx.web3;
    if (kirbyCtx === null) {
        return React.createElement("div", null);
    }
    var _b = React.useState(null), currentUser = _b[0], setCurrentUser = _b[1];
    var baseContext = React.useMemo(function () {
        function onAuthChange(nextUser) {
            setCurrentUser(nextUser);
        }
        var ctx = CivilContext_1.buildCivilContext({ web3: web3, apolloClient: apolloClient, featureFlags: featureFlags, config: config, onAuthChange: onAuthChange });
        return ctx;
    }, []);
    // state
    var civilContext = React.useMemo(function () {
        return __assign(__assign({}, baseContext), { currentUser: currentUser });
    }, [baseContext, currentUser]);
    // render
    return React.createElement(CivilContext_1.CivilContext.Provider, { value: civilContext }, children);
};
exports.CivilProvider = function (_a) {
    var children = _a.children, featureFlags = _a.featureFlags, pluginConfig = _a.pluginConfig, config = _a.config, makeLegacyWeb3 = _a.makeLegacyWeb3;
    var featureFlagService = React.useMemo(function () {
        return new core_1.FeatureFlagService(featureFlags);
    }, [featureFlags]);
    if (!featureFlagService.featureEnabled("kirby")) {
        var mockKirby_1 = React.useMemo(function () {
            var web3 = makeLegacyWeb3();
            console.log("not using kirby", web3);
            return { web3: web3 };
        }, []);
        return (React.createElement(react_apollo_1.ApolloConsumer, null, function (client) { return (React.createElement(ethereum_react_1.KirbyEthereumContext.Provider, { value: mockKirby_1 },
            React.createElement(CivilInnerProvider, { featureFlags: featureFlags, config: config, apolloClient: client }, children))); }));
    }
    return (React.createElement(react_apollo_1.ApolloConsumer, null, function (client) { return (React.createElement(ethereum_react_1.KirbyEthereumProvider, { config: pluginConfig },
        React.createElement(CivilInnerProvider, { featureFlags: featureFlags, config: config, apolloClient: client }, children))); }));
};
//# sourceMappingURL=CivilProvider.js.map