"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRuntimeData = exports.withRuntimeTags = void 0;
const exenv_1 = require("exenv");
const runtimeContainerId = '__MICRO_REACT_RUNTIME__';
exports.withRuntimeTags = ({ publicPaths, page: { name } }) => `<script id="${runtimeContainerId}" type="application/json">${JSON.stringify({
    publicPaths,
    page: { name },
})}</script>`;
const isRuntimeData = (obj) => {
    var _a;
    const paths = obj === null || obj === void 0 ? void 0 : obj.publicPaths;
    return (typeof (paths === null || paths === void 0 ? void 0 : paths.assets) === 'string' &&
        typeof (paths === null || paths === void 0 ? void 0 : paths.data) === 'string' &&
        typeof ((_a = obj === null || obj === void 0 ? void 0 : obj.page) === null || _a === void 0 ? void 0 : _a.name) === 'string');
};
const ensureRuntime = (obj) => {
    if (!isRuntimeData(obj)) {
        throw new Error(`💣 Error while validating runtime data ${JSON.stringify(obj)}`);
    }
    return obj;
};
exports.getRuntimeData = () => {
    var _a;
    console.assert(exenv_1.canUseDOM, '💣 Runtime hydration can only be run on the browser');
    const dataElement = document.getElementById(runtimeContainerId);
    const maybeRuntime = JSON.parse((_a = dataElement === null || dataElement === void 0 ? void 0 : dataElement.textContent) !== null && _a !== void 0 ? _a : '{}');
    return ensureRuntime(maybeRuntime);
};
