"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 auth_middleware_1 = require("../authentication/auth-middleware");
const exenv_1 = __importDefault(require("exenv"));
const universal_cookie_1 = __importDefault(require("universal-cookie"));
const iso_libs_1 = require("../libs/iso-libs");
// create empty context as default
const UserContext = React.createContext({});
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;
function getWebToken(request) {
    if (request) {
        return new universal_cookie_1.default(request.headers.cookie).get(auth_middleware_1.IC_WEB_TOKEN);
    }
    else if (exenv_1.default.canUseDOM) {
        return new universal_cookie_1.default().get(auth_middleware_1.IC_WEB_TOKEN);
    }
    return undefined;
}
exports.getWebToken = getWebToken;
/**
 * This HOC attaches the User-Id to a webapp, may be undefined!
 */
const AttachUser = (props) => {
    //console.log("attaching user: ", getUserId(undefined));
    return React.createElement(UserContext.Provider, { value: {
            userId: exenv_1.default.canUseDOM ?
                new universal_cookie_1.default().get(auth_middleware_1.IC_USER_ID) :
                new universal_cookie_1.default(props.request.headers.cookie).get(auth_middleware_1.IC_USER_ID)
        } }, props.children);
};
/**
 * @param Component
 * @returns {function(any): any}
 */
function withUser(Component) {
    return function WrapperComponent(props) {
        return (React.createElement(UserContext.Consumer, null, (context) => {
            return React.createElement(Component, Object.assign({}, props, { userId: context.userId }));
        }));
    };
}
exports.withUser = withUser;
function userLogout(pathUrl) {
    const path = require('path');
    const cookies = new universal_cookie_1.default();
    cookies.remove(auth_middleware_1.IC_USER_ID, { path: '/' });
    cookies.remove(auth_middleware_1.IC_WEB_TOKEN, { path: '/' });
    if (pathUrl !== undefined) {
        window.location.href = path.join(iso_libs_1.getBasename(), pathUrl);
    }
    else {
        window.location.reload();
    }
}
exports.userLogout = userLogout;
exports.default = require("infrastructure-components").withRequest(AttachUser);
//# sourceMappingURL=attach-user.js.map