"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 });
const React = __importStar(require("react"));
const universal_cookie_1 = __importDefault(require("universal-cookie"));
const auth_middleware_1 = require("../authentication/auth-middleware");
const exenv_1 = __importDefault(require("exenv"));
//import {withRequest} from "../components/attach-request";
exports.isLoggedIn = (identityKey) => {
    const val = new universal_cookie_1.default().get(identityKey);
    //console.log("forceLogin: ",val);
    return val !== undefined;
};
// create a context with the default value: empty
const LoggedInContext = React.createContext(undefined);
/**
 * implements [[RequireServerRenderingSpec]]
 *
 * the ForceLogin Component is a HOC that passes the userid to its child components
 * the forceLogin gets the cookie value regardless on whether it runs on the server or in the browser
 * see: https://www.npmjs.com/package/universal-cookie
 *
 * see hocs with context: https://itnext.io/combining-hocs-with-the-new-reacts-context-api-9d3617dccf0b
 *
 * how to check whether running on server or in browser: https://www.npmjs.com/package/exenv
 */
class ForceLogin extends React.Component {
    /**
     * When we run in the browser: if not logged in, then make the page reload from the server to
     * make the login-middlewares apply
     */
    componentDidMount() {
        //console.log("ForceLogin-Component: ", this.props.identityKey)
        if (!exports.isLoggedIn(auth_middleware_1.IC_USER_ID)) {
            window.location.reload();
        }
    }
    render() {
        console.log("ForceLogin: request ->", this.props.request);
        // we provide the information which user is logged in
        return React.createElement(LoggedInContext.Provider, { value: exenv_1.default.canUseDOM ?
                new universal_cookie_1.default().get(auth_middleware_1.IC_USER_ID) :
                new universal_cookie_1.default(this.props.request.headers.cookie).get(auth_middleware_1.IC_USER_ID) }, this.props.children);
    }
}
/**
 * Pass the information on whether the user `isLoggedIn` as prop to the component
 * @param Component
 * @returns {function(any): any}
 */
function withUserId(Component) {
    return function WrapperComponent(props) {
        return (React.createElement(LoggedInContext.Consumer, null, value => React.createElement(Component, Object.assign({}, props, { userId: value }))));
    };
}
exports.withUserId = withUserId;
function getUserId(request) {
    if (request) {
        return new universal_cookie_1.default(request.headers.cookie).get(auth_middleware_1.IC_USER_ID);
    }
    else if (exenv_1.default.canUseDOM) {
        return new universal_cookie_1.default().get(auth_middleware_1.IC_USER_ID);
    }
    return undefined;
}
exports.getUserId = getUserId;
/**
 * we MUST NOT IMPORT CONTEXTs directly, but require them at time of use generally from Infrastructure-Components
 * because this then resolves to node_modules
 */
exports.default = require("infrastructure-components").withRequest(ForceLogin);
//# sourceMappingURL=force-login.js.map