"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const react_router_1 = require("react-router");
const react_router_dom_1 = require("react-router-dom");
//import {useContext} from "react"
//import {__RouterContext} from "react-router"
const redirect_w_status_1 = __importDefault(require("./redirect-w-status"));
const force_login_1 = __importDefault(require("./force-login"));
;
const RoutedApp = (props) => {
    //.filter(({ customType }) => customType !== Types.IFRAME)
    // (p) => render(Object.assign({},p, props))
    //console.log("RoutedApp: " , useContext(__RouterContext))
    const routes = props.routes.map(({ path, exact, component, render, isSecured }, i) => {
        //console.log("routepath: ", path)
        // NOT using routeConfig.pathToRoute(path) for the Router includes a basename already!
        if (render !== undefined) {
            const wrappedRender = (p) => isSecured ? react_1.default.createElement(force_login_1.default, null, render(p)) : render(p);
            return react_1.default.createElement(react_router_dom_1.Route, { key: 'ROUTE_' + i, exact: exact, path: path, render: wrappedRender });
        }
        else if (isSecured) {
            const C = component;
            return react_1.default.createElement(react_router_dom_1.Route, { key: 'ROUTE_' + i, exact: exact, path: path, render: (p) => react_1.default.createElement(force_login_1.default, null,
                    react_1.default.createElement(C, Object.assign({}, p))) });
        }
        else {
            return react_1.default.createElement(react_router_dom_1.Route, { key: 'ROUTE_' + i, exact: exact, path: path, component: component });
        }
    });
    const redirects = props.redirects.map(({ from, to, status }, i) => react_1.default.createElement(redirect_w_status_1.default, { key: 'REDIRECT_' + i, from: from, to: to, status: status }));
    return react_1.default.createElement(react_router_dom_1.Switch, null,
        routes,
        redirects);
};
/**
 * TODO when we use an internal link that attaches a path-parameter, do we get this here? maybe we need to force a reload from the server
 *
 * @param routes
 * @param redirects
 * @param basename
 * @returns {any}
 */
exports.createClientApp = (routes, redirects, basename) => {
    const AttachRequest = require("infrastructure-components").AttachRequest;
    const AttachRoutes = require("infrastructure-components").AttachRoutes;
    return react_1.default.createElement(react_router_dom_1.BrowserRouter, { basename: basename },
        react_1.default.createElement(AttachRequest, null,
            react_1.default.createElement(AttachRoutes, { routes: routes },
                react_1.default.createElement(RoutedApp, { routes: routes, redirects: redirects }))));
};
exports.createServerApp = (routes, redirects, basename, url, context, request) => {
    const AttachRequest = require("infrastructure-components").AttachRequest;
    const AttachRoutes = require("infrastructure-components").AttachRoutes;
    return react_1.default.createElement(react_router_1.StaticRouter, { context: context, location: url, basename: basename },
        react_1.default.createElement(AttachRequest, { request: request },
            react_1.default.createElement(AttachRoutes, { routes: routes },
                react_1.default.createElement(RoutedApp, { routes: routes, redirects: redirects }))));
};
exports.createSinglePageApp = (routes, redirects) => {
    // a single page app does not support Identities (so far). There is no identityKey={identityKey}
    const AttachRoutes = require("infrastructure-components").AttachRoutes;
    return react_1.default.createElement(react_router_dom_1.HashRouter, null,
        react_1.default.createElement(AttachRoutes, { routes: routes },
            react_1.default.createElement(RoutedApp, { routes: routes, redirects: redirects })));
};
//# sourceMappingURL=routed-app.js.map