import { jsx as _jsx } from "react/jsx-runtime";
import '../build/polyfills/polyfill-module';
// @ts-ignore react-dom/client exists when using React 18
import ReactDOMClient from 'react-dom/client';
import React, { use } from 'react';
// @ts-ignore
// eslint-disable-next-line import/no-extraneous-dependencies
import { createFromReadableStream } from 'react-server-dom-webpack/client';
import { HeadManagerContext } from '../shared/lib/head-manager-context.shared-runtime';
import { onRecoverableError } from './on-recoverable-error';
import { callServer } from './app-call-server';
import { createMutableActionQueue } from '../shared/lib/router/action-queue';
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../server/dev/hot-reloader-types';
import { isNextRouterError } from './components/is-next-router-error';
import { handleClientError } from './components/react-dev-overlay/internal/helpers/use-error-handler';
import AppRouter from './components/app-router';
import { createInitialRouterState } from './components/router-reducer/create-initial-router-state';
import { MissingSlotContext } from '../shared/lib/app-router-context.shared-runtime';
// Patch console.error to collect information about hydration errors
const origConsoleError = window.console.error;
window.console.error = function() {
    for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
        args[_key] = arguments[_key];
    }
    // See https://github.com/facebook/react/blob/d50323eb845c5fde0d720cae888bf35dedd05506/packages/react-reconciler/src/ReactFiberErrorLogger.js#L78
    const error = process.env.NODE_ENV !== 'production' ? args[1] : args[0];
    if (!isNextRouterError(error)) {
        if (process.env.NODE_ENV !== 'production') {
            const { storeHydrationErrorStateFromConsoleArgs } = require('./components/react-dev-overlay/internal/helpers/hydration-error-info');
            storeHydrationErrorStateFromConsoleArgs(...args);
            handleClientError(error);
        }
        origConsoleError.apply(window.console, args);
    }
};
/// <reference types="react-dom/experimental" />
const appElement = document;
const encoder = new TextEncoder();
let initialServerDataBuffer = undefined;
let initialServerDataWriter = undefined;
let initialServerDataLoaded = false;
let initialServerDataFlushed = false;
let initialFormStateData = null;
function nextServerDataCallback(seg) {
    if (seg[0] === 0) {
        initialServerDataBuffer = [];
    } else if (seg[0] === 1) {
        if (!initialServerDataBuffer) throw new Error('Unexpected server data: missing bootstrap script.');
        if (initialServerDataWriter) {
            initialServerDataWriter.enqueue(encoder.encode(seg[1]));
        } else {
            initialServerDataBuffer.push(seg[1]);
        }
    } else if (seg[0] === 2) {
        initialFormStateData = seg[1];
    } else if (seg[0] === 3) {
        if (!initialServerDataBuffer) throw new Error('Unexpected server data: missing bootstrap script.');
        // Decode the base64 string back to binary data.
        const binaryString = atob(seg[1]);
        const decodedChunk = new Uint8Array(binaryString.length);
        for(var i = 0; i < binaryString.length; i++){
            decodedChunk[i] = binaryString.charCodeAt(i);
        }
        if (initialServerDataWriter) {
            initialServerDataWriter.enqueue(decodedChunk);
        } else {
            initialServerDataBuffer.push(decodedChunk);
        }
    }
}
function isStreamErrorOrUnfinished(ctr) {
    // If `desiredSize` is null, it means the stream is closed or errored. If it is lower than 0, the stream is still unfinished.
    return ctr.desiredSize === null || ctr.desiredSize < 0;
}
// There might be race conditions between `nextServerDataRegisterWriter` and
// `DOMContentLoaded`. The former will be called when React starts to hydrate
// the root, the latter will be called when the DOM is fully loaded.
// For streaming, the former is called first due to partial hydration.
// For non-streaming, the latter can be called first.
// Hence, we use two variables `initialServerDataLoaded` and
// `initialServerDataFlushed` to make sure the writer will be closed and
// `initialServerDataBuffer` will be cleared in the right time.
function nextServerDataRegisterWriter(ctr) {
    if (initialServerDataBuffer) {
        initialServerDataBuffer.forEach((val)=>{
            ctr.enqueue(typeof val === 'string' ? encoder.encode(val) : val);
        });
        if (initialServerDataLoaded && !initialServerDataFlushed) {
            if (isStreamErrorOrUnfinished(ctr)) {
                ctr.error(new Error('The connection to the page was unexpectedly closed, possibly due to the stop button being clicked, loss of Wi-Fi, or an unstable internet connection.'));
            } else {
                ctr.close();
            }
            initialServerDataFlushed = true;
            initialServerDataBuffer = undefined;
        }
    }
    initialServerDataWriter = ctr;
}
// When `DOMContentLoaded`, we can close all pending writers to finish hydration.
const DOMContentLoaded = function() {
    if (initialServerDataWriter && !initialServerDataFlushed) {
        initialServerDataWriter.close();
        initialServerDataFlushed = true;
        initialServerDataBuffer = undefined;
    }
    initialServerDataLoaded = true;
};
// It's possible that the DOM is already loaded.
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', DOMContentLoaded, false);
} else {
    // Delayed in marco task to ensure it's executed later than hydration
    setTimeout(DOMContentLoaded);
}
const nextServerDataLoadingGlobal = self.__next_f = self.__next_f || [];
nextServerDataLoadingGlobal.forEach(nextServerDataCallback);
nextServerDataLoadingGlobal.push = nextServerDataCallback;
const readable = new ReadableStream({
    start (controller) {
        nextServerDataRegisterWriter(controller);
    }
});
const initialServerResponse = createFromReadableStream(readable, {
    callServer
});
// React overrides `.then` and doesn't return a new promise chain,
// so we wrap the action queue in a promise to ensure that its value
// is defined when the promise resolves.
// https://github.com/facebook/react/blob/163365a07872337e04826c4f501565d43dbd2fd4/packages/react-client/src/ReactFlightClient.js#L189-L190
const pendingActionQueue = new Promise((resolve, reject)=>{
    initialServerResponse.then((initialRSCPayload)=>{
        resolve(createMutableActionQueue(createInitialRouterState({
            buildId: initialRSCPayload.b,
            initialFlightData: initialRSCPayload.f,
            initialCanonicalUrl: initialRSCPayload.c,
            initialParallelRoutes: new Map(),
            location: window.location,
            couldBeIntercepted: initialRSCPayload.i,
            postponed: initialRSCPayload.s
        })));
    }, (err)=>reject(err));
});
function ServerRoot() {
    const initialRSCPayload = use(initialServerResponse);
    const actionQueue = use(pendingActionQueue);
    const router = /*#__PURE__*/ _jsx(AppRouter, {
        actionQueue: actionQueue,
        globalErrorComponent: initialRSCPayload.G,
        assetPrefix: initialRSCPayload.p
    });
    if (process.env.NODE_ENV === 'development' && initialRSCPayload.m) {
        // We provide missing slot information in a context provider only during development
        // as we log some additional information about the missing slots in the console.
        return /*#__PURE__*/ _jsx(MissingSlotContext, {
            value: initialRSCPayload.m,
            children: router
        });
    }
    return router;
}
const StrictModeIfEnabled = process.env.__NEXT_STRICT_MODE_APP ? React.StrictMode : React.Fragment;
function Root(param) {
    let { children } = param;
    if (process.env.__NEXT_TEST_MODE) {
        // eslint-disable-next-line react-hooks/rules-of-hooks
        React.useEffect(()=>{
            window.__NEXT_HYDRATED = true;
            window.__NEXT_HYDRATED_CB == null ? void 0 : window.__NEXT_HYDRATED_CB.call(window);
        }, []);
    }
    return children;
}
export function hydrate() {
    const reactEl = /*#__PURE__*/ _jsx(StrictModeIfEnabled, {
        children: /*#__PURE__*/ _jsx(HeadManagerContext.Provider, {
            value: {
                appDir: true
            },
            children: /*#__PURE__*/ _jsx(Root, {
                children: /*#__PURE__*/ _jsx(ServerRoot, {})
            })
        })
    });
    const rootLayoutMissingTags = window.__next_root_layout_missing_tags;
    const hasMissingTags = !!(rootLayoutMissingTags == null ? void 0 : rootLayoutMissingTags.length);
    const options = {
        onRecoverableError
    };
    const isError = document.documentElement.id === '__next_error__' || hasMissingTags;
    if (isError) {
        if (process.env.NODE_ENV !== 'production') {
            // if an error is thrown while rendering an RSC stream, this will catch it in dev
            // and show the error overlay
            const ReactDevOverlay = require('./components/react-dev-overlay/app/ReactDevOverlay').default;
            const INITIAL_OVERLAY_STATE = require('./components/react-dev-overlay/shared').INITIAL_OVERLAY_STATE;
            const getSocketUrl = require('./components/react-dev-overlay/internal/helpers/get-socket-url').getSocketUrl;
            const FallbackLayout = hasMissingTags ? (param)=>{
                let { children } = param;
                return /*#__PURE__*/ _jsx("html", {
                    id: "__next_error__",
                    children: /*#__PURE__*/ _jsx("body", {
                        children: children
                    })
                });
            } : React.Fragment;
            const errorTree = /*#__PURE__*/ _jsx(FallbackLayout, {
                children: /*#__PURE__*/ _jsx(ReactDevOverlay, {
                    state: {
                        ...INITIAL_OVERLAY_STATE,
                        rootLayoutMissingTags
                    },
                    onReactError: ()=>{},
                    children: reactEl
                })
            });
            const socketUrl = getSocketUrl(process.env.__NEXT_ASSET_PREFIX || '');
            const socket = new window.WebSocket("" + socketUrl + "/_next/webpack-hmr");
            // add minimal "hot reload" support for RSC errors
            const handler = (event)=>{
                let obj;
                try {
                    obj = JSON.parse(event.data);
                } catch (e) {}
                if (!obj || !('action' in obj)) {
                    return;
                }
                if (obj.action === HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES) {
                    window.location.reload();
                }
            };
            socket.addEventListener('message', handler);
            ReactDOMClient.createRoot(appElement, options).render(errorTree);
        } else {
            ReactDOMClient.createRoot(appElement, options).render(reactEl);
        }
    } else {
        React.startTransition(()=>ReactDOMClient.hydrateRoot(appElement, reactEl, {
                ...options,
                formState: initialFormStateData
            }));
    }
    // TODO-APP: Remove this logic when Float has GC built-in in development.
    if (process.env.NODE_ENV !== 'production') {
        const { linkGc } = require('./app-link-gc');
        linkGc();
    }
}

//# sourceMappingURL=app-index.js.map