"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var utils_1 = require("@joincivil/utils");
var react_apollo_1 = require("react-apollo");
var useStateWithLocalStorage_1 = __importDefault(require("../hooks/useStateWithLocalStorage"));
exports.LoadUser = function (props) {
    var apolloSessionKey = utils_1.getApolloSessionKey();
    var auth = useStateWithLocalStorage_1.default(apolloSessionKey)[0];
    useStateWithLocalStorage_1.default("network"); // basically just pay attention to when network is set
    var renderFunction = props.children;
    var hasAuthToken = auth && auth.token;
    if (!hasAuthToken) {
        return React.createElement(React.Fragment, null, renderFunction({ user: null, loading: false }));
    }
    return (React.createElement(react_apollo_1.Query, { query: utils_1.getCurrentUserQuery }, function (_a) {
        var loading = _a.loading, error = _a.error, data = _a.data, refetch = _a.refetch;
        if (loading || error) {
            return renderFunction({ user: null, loading: loading, refetch: refetch });
        }
        return renderFunction({ user: data.currentUser, loading: loading, refetch: refetch });
    }));
};
//# sourceMappingURL=LoadUser.js.map