"use strict";
/**
 * This module is responsible for generating the HTML page response for
 * the react application middleware.
 */
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 cli_core_1 = require("@bluebase/cli-core");
// tslint:disable-next-line:no-submodule-imports
const server_1 = require("react-dom/server");
const HTML_1 = __importDefault(require("../../components/HTML"));
const react_helmet_1 = __importDefault(require("react-helmet"));
const SplashScreen_1 = __importDefault(require("../../components/SplashScreen"));
const getClientBundleEntryAssets_1 = __importDefault(require("./getClientBundleEntryAssets"));
// PRIVATES
function KeyedComponent({ children }) {
    return react_1.Children.only(children);
}
function stylesheetTag(stylesheetFilePath) {
    return react_1.default.createElement("link", { href: stylesheetFilePath, media: "screen, projection", rel: "stylesheet", type: "text/css" });
}
function scriptTag(jsFilePath) {
    return react_1.default.createElement("script", { type: "text/javascript", src: jsFilePath });
}
const getServerHTML = (configs) => (props) => {
    const ifElse = cli_core_1.Utils.ifElse;
    const removeNil = cli_core_1.Utils.removeNil;
    // Resolve the assets (js/css) for the client bundle's entry chunk.
    const clientEntryAssets = getClientBundleEntryAssets_1.default(configs)();
    const { reactAppString, styleElement } = props;
    const helmet = react_helmet_1.default.rewind();
    // // Creates an inline script definition that is protected by the nonce.
    // 	const inlineScript = (body: any) =>
    // 	<script nonce={nonce} type="text/javascript" dangerouslySetInnerHTML={{ __html: body }} />;
    const headerElements = removeNil([
        ...ifElse(helmet)(() => helmet.meta.toComponent(), []),
        ...ifElse(helmet)(() => helmet.title.toComponent(), []),
        ...ifElse(helmet)(() => helmet.base.toComponent(), []),
        ...ifElse(helmet)(() => helmet.link.toComponent(), []),
        ifElse(clientEntryAssets && clientEntryAssets.css)(() => stylesheetTag(clientEntryAssets.css)),
        ...ifElse(helmet)(() => helmet.style.toComponent(), []),
        ifElse(helmet)(() => styleElement),
    ]);
    // const devVendorDLL = configs.client.devVendorDLL;
    const bodyElements = removeNil([
        // When we are in development mode our development server will
        // generate a vendor DLL in order to dramatically reduce our
        // compilation times.  Therefore we need to inject the path to the
        // vendor dll bundle below.
        // 	ifElse(
        // 		process.env.BUILD_FLAG_IS_DEV === 'true' && devVendorDLL && devVendorDLL.enabled,
        // )(() =>
        // 	scriptTag(
        // 		`${configs.client.publicPath}/${devVendorDLL && devVendorDLL.name}.js?t=${Date.now()}`,
        // 	),
        // ),
        ifElse(clientEntryAssets && clientEntryAssets.js)(() => scriptTag(clientEntryAssets.js)),
        ...ifElse(helmet)(() => helmet.script.toComponent(), []),
    ]);
    return react_1.default.createElement(HTML_1.default, { htmlAttributes: ifElse(helmet)(() => helmet.htmlAttributes.toComponent(), null), headerElements: headerElements.map((x, idx) => {
            return react_1.default.createElement(KeyedComponent, { key: idx }, x);
        }), bodyElements: bodyElements.map((x, idx) => {
            return react_1.default.createElement(KeyedComponent, { key: idx }, x);
        }), appBodyString: reactAppString || server_1.renderToStaticMarkup(react_1.default.createElement(SplashScreen_1.default, null)) });
};
exports.default = getServerHTML;
//# sourceMappingURL=ServerHTML.js.map