"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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
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 react_router_dom_1 = require("react-router-dom");
var utils_1 = require("@joincivil/utils");
var react_apollo_1 = require("react-apollo");
exports.AuthenticatedRoute = function (_a) {
    var render = _a.render, redirectTo = _a.redirectTo, authUrl = _a.authUrl, _b = _a.onlyAllowUnauthenticated, onlyAllowUnauthenticated = _b === void 0 ? false : _b, otherProps = __rest(_a, ["render", "redirectTo", "authUrl", "onlyAllowUnauthenticated"]);
    var auth = utils_1.getApolloSession();
    var hasAuthToken = !!auth && !!auth.token;
    // TODO(jorgelo): Refactor this boolean logic.
    if (onlyAllowUnauthenticated) {
        if (hasAuthToken) {
            return React.createElement(react_router_dom_1.Redirect, { to: redirectTo });
        }
    }
    else {
        if (!hasAuthToken) {
            return React.createElement(react_router_dom_1.Redirect, { to: authUrl });
        }
    }
    if (!render) {
        throw new Error("Please set a render function");
    }
    // TODO(jorgelo): Get the line below working without the ts-ignore
    // @ts-ignore
    var renderChildren = function () { return render(otherProps); };
    if (onlyAllowUnauthenticated && !hasAuthToken) {
        if (render) {
            return renderChildren();
        }
        return null;
    }
    return (React.createElement(react_router_dom_1.Route, __assign({}, otherProps),
        React.createElement(react_apollo_1.Query, { query: utils_1.getCurrentUserQuery }, function (_a) {
            var loading = _a.loading, error = _a.error, data = _a.data;
            if (loading) {
                return null;
            }
            if (error && !onlyAllowUnauthenticated) {
                return React.createElement(react_router_dom_1.Redirect, { to: redirectTo });
            }
            if (render) {
                return renderChildren();
            }
            return null;
        })));
};
exports.UnauthenticatedRoute = function (props) { return (React.createElement(exports.AuthenticatedRoute, __assign({ onlyAllowUnauthenticated: true }, props))); };
//# sourceMappingURL=AuthenticatedRoute.js.map