"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_1 = __importStar(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 RawRoutedApp = (props) => {
    react_1.useEffect(() => {
        if (props.listen) {
            props.history.listen(props.listen);
        }
    });
    //.filter(({ customType }) => customType !== Types.IFRAME)
    // (p) => render(Object.assign({},p, props))
    //console.log("RoutedApp: " , useContext(__RouterContext))
    const ForceLogin = require("infrastructure-components").ForceLogin;
    //console.log("RawRoutedApp: ", props.routes);
    const routes = props.routes.map(({ path, exact, component, render, isSecured }, i) => {
        // NOT using routeConfig.pathToRoute(path) for the Router includes a basename already!
        //console.log("RoutedApp: ", path, typeof path);
        const fixPath = p => (typeof p === 'string' || p instanceof String) ? p : new RegExp(p).toString().replace("/\\/g", "\\\\");
        if (render !== undefined) {
            const wrappedRender = (p) => isSecured ? react_1.default.createElement(ForceLogin, null, render(p)) : render(p);
            return react_1.default.createElement(react_router_dom_1.Route, { key: 'ROUTE_' + i, exact: exact, path: fixPath(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: fixPath(path), render: (p) => react_1.default.createElement(ForceLogin, 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: fixPath(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 }));
    //console.log(routes);
    return react_1.default.createElement(react_router_dom_1.Switch, null,
        routes,
        redirects);
};
const RoutedApp = react_router_dom_1.withRouter(RawRoutedApp);
/**
 * 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, listen, authCallback, preloadedState, preloadedFiles) => {
    const AttachRequest = require("infrastructure-components").AttachRequest;
    const AttachRoutes = require("infrastructure-components").AttachRoutes;
    const AttachUser = require("infrastructure-components").AttachUser;
    const AttachAuth = require("infrastructure-components").AttachAuth;
    const AttachIsomorphicState = require("infrastructure-components").AttachIsomorphicState;
    const AttachStorage = require("infrastructure-components").AttachStorage;
    return react_1.default.createElement(react_router_dom_1.BrowserRouter, { basename: basename },
        react_1.default.createElement(AttachRequest, null,
            react_1.default.createElement(AttachUser, null,
                react_1.default.createElement(AttachAuth, { authCallback: authCallback },
                    react_1.default.createElement(AttachRoutes, { routes: routes },
                        react_1.default.createElement(AttachIsomorphicState, { preloadedState: preloadedState },
                            react_1.default.createElement(AttachStorage, { preloadedFiles: preloadedFiles },
                                react_1.default.createElement(RoutedApp, { routes: routes, redirects: redirects, listen: listen }))))))));
};
exports.createServerApp = (routes, redirects, basename, url, context, request, response, authCallback, setServerValue, addToRenderList, config, isOffline = false, renderListResults, //only filled when we have the results!
isomorphicState) => {
    //console.log("createServerApp: ", isomorphicState);
    const AttachRequest = require("infrastructure-components").AttachRequest;
    const AttachRoutes = require("infrastructure-components").AttachRoutes;
    const AttachUser = require("infrastructure-components").AttachUser;
    const AttachAuth = require("infrastructure-components").AttachAuth;
    const AttachIsomorphicState = require("infrastructure-components").AttachIsomorphicState;
    const AttachStorage = require("infrastructure-components").AttachStorage;
    return react_1.default.createElement(react_router_1.StaticRouter, { context: context, location: url, basename: basename !== "/" ? basename : undefined },
        react_1.default.createElement(AttachRequest, { request: request, response: response },
            react_1.default.createElement(AttachUser, null,
                react_1.default.createElement(AttachAuth, { authCallback: authCallback },
                    react_1.default.createElement(AttachRoutes, { routes: routes },
                        react_1.default.createElement(AttachIsomorphicState, { setServerValue: setServerValue, preloadedState: isomorphicState },
                            react_1.default.createElement(AttachStorage, { addToRenderList: addToRenderList, config: config, isOffline: isOffline, renderListResults: renderListResults },
                                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