import { ServerClientChangeType } from "../../../build/swc";
import ws from "next/dist/compiled/ws";
import fs from "fs";
import url from "url";
import path from "path";
import qs from "querystring";
import Watchpack from "watchpack";
import { loadEnvConfig } from "@next/env";
import isError from "../../../lib/is-error";
import findUp from "next/dist/compiled/find-up";
import { buildCustomRoute } from "./filesystem";
import * as Log from "../../../build/output/log";
import HotReloader, { matchNextPageBundleRequest } from "../../dev/hot-reloader-webpack";
import { setGlobal } from "../../../trace/shared";
import loadJsConfig from "../../../build/load-jsconfig";
import { createValidFileMatcher } from "../find-page-file";
import { eventCliSession } from "../../../telemetry/events";
import { getDefineEnv } from "../../../build/webpack-config";
import { logAppDirError } from "../../dev/log-app-dir-error";
import { getSortedRoutes } from "../../../shared/lib/router/utils";
import { getStaticInfoIncludingLayouts, sortByPageExts } from "../../../build/entries";
import { verifyTypeScriptSetup } from "../../../lib/verifyTypeScriptSetup";
import { verifyPartytownSetup } from "../../../lib/verify-partytown-setup";
import { getRouteRegex } from "../../../shared/lib/router/utils/route-regex";
import { normalizeAppPath } from "../../../shared/lib/router/utils/app-paths";
import { buildDataRoute } from "./build-data-route";
import { getRouteMatcher } from "../../../shared/lib/router/utils/route-matcher";
import { normalizePathSep } from "../../../shared/lib/page-path/normalize-path-sep";
import { createClientRouterFilter } from "../../../lib/create-client-router-filter";
import { absolutePathToPage } from "../../../shared/lib/page-path/absolute-path-to-page";
import { generateInterceptionRoutesRewrites } from "../../../lib/generate-interception-routes-rewrites";
import { APP_BUILD_MANIFEST, APP_PATHS_MANIFEST, BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, COMPILER_NAMES, DEV_CLIENT_PAGES_MANIFEST, DEV_MIDDLEWARE_MANIFEST, MIDDLEWARE_MANIFEST, NEXT_FONT_MANIFEST, PAGES_MANIFEST, PHASE_DEVELOPMENT_SERVER } from "../../../shared/lib/constants";
import { getMiddlewareRouteMatcher } from "../../../shared/lib/router/utils/middleware-route-matcher";
import { NextBuildContext } from "../../../build/build-context";
import { isMiddlewareFile, NestedMiddlewareError, isInstrumentationHookFile, getPossibleMiddlewareFilenames, getPossibleInstrumentationHookFilenames } from "../../../build/worker";
import { createOriginalStackFrame, getErrorSource, getSourceById, parseStack } from "next/dist/compiled/@next/react-dev-overlay/dist/middleware";
import { mkdir, readFile, writeFile } from "fs/promises";
import { PageNotFoundError } from "../../../shared/lib/utils";
import { srcEmptySsgManifest } from "../../../build/webpack/plugins/build-manifest-plugin";
import { devPageFiles } from "../../../build/webpack/plugins/next-types-plugin/shared";
import { pathToRegexp } from "next/dist/compiled/path-to-regexp";
const wsServer = new ws.Server({
    noServer: true
});
async function verifyTypeScript(opts) {
    let usingTypeScript = false;
    const verifyResult = await verifyTypeScriptSetup({
        dir: opts.dir,
        distDir: opts.nextConfig.distDir,
        intentDirs: [
            opts.pagesDir,
            opts.appDir
        ].filter(Boolean),
        typeCheckPreflight: false,
        tsconfigPath: opts.nextConfig.typescript.tsconfigPath,
        disableStaticImages: opts.nextConfig.images.disableStaticImages,
        hasAppDir: !!opts.appDir,
        hasPagesDir: !!opts.pagesDir
    });
    if (verifyResult.version) {
        usingTypeScript = true;
    }
    return usingTypeScript;
}
async function startWatcher(opts) {
    const { nextConfig , appDir , pagesDir , dir  } = opts;
    const { useFileSystemPublicRoutes  } = nextConfig;
    const usingTypeScript = await verifyTypeScript(opts);
    const distDir = path.join(opts.dir, opts.nextConfig.distDir);
    setGlobal("distDir", distDir);
    setGlobal("phase", PHASE_DEVELOPMENT_SERVER);
    const validFileMatcher = createValidFileMatcher(nextConfig.pageExtensions, appDir);
    async function propagateToWorkers(field, args) {
        var _opts_renderWorkers_app, _opts_renderWorkers_pages;
        await ((_opts_renderWorkers_app = opts.renderWorkers.app) == null ? void 0 : _opts_renderWorkers_app.propagateServerField(field, args));
        await ((_opts_renderWorkers_pages = opts.renderWorkers.pages) == null ? void 0 : _opts_renderWorkers_pages.propagateServerField(field, args));
    }
    const serverFields = {};
    let hotReloader;
    if (opts.turbo) {
        const { loadBindings  } = require("../../../build/swc");
        let bindings = await loadBindings();
        const { jsConfig  } = await loadJsConfig(dir, opts.nextConfig);
        const project = await bindings.turbo.createProject({
            projectPath: dir,
            rootPath: opts.nextConfig.experimental.outputFileTracingRoot || dir,
            nextConfig: opts.nextConfig,
            jsConfig,
            watch: true,
            env: process.env
        });
        const iter = project.entrypointsSubscribe();
        const curEntries = new Map();
        let changeSubscriptions = new Map();
        let prevMiddleware = undefined;
        const globalEntries = {
            app: undefined,
            document: undefined,
            error: undefined
        };
        let currentEntriesHandlingResolve;
        let currentEntriesHandling = new Promise((resolve)=>currentEntriesHandlingResolve = resolve);
        const issues = new Map();
        async function processResult(key, result) {
            if (result) {
                await (global._nextDeleteCache == null ? void 0 : global._nextDeleteCache(result.serverPaths.map((p)=>path.join(distDir, p)).concat([
                    // We need to clear the chunk cache in react
                    require.resolve("next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.edge.development.js"),
                    // And this redirecting module as well
                    require.resolve("next/dist/compiled/react-server-dom-webpack/client.edge.js")
                ])));
                const oldSet = issues.get(key) ?? new Set();
                const newSet = new Set();
                for (const issue of result.issues){
                    // TODO better formatting
                    if (issue.severity !== "error" && issue.severity !== "fatal") continue;
                    const issueKey = `${issue.severity} - ${issue.filePath} - ${issue.title}\n${issue.description}\n\n`;
                    if (!oldSet.has(issueKey)) {
                        console.error(`  ⚠ ${key} ${issue.severity} - ${issue.filePath}\n    ${issue.title.replace(/\n/g, "\n    ")}\n    ${issue.description.replace(/\n/g, "\n    ")}\n\n`);
                    }
                    newSet.add(issueKey);
                }
                for (const issue of oldSet){
                    if (!newSet.has(issue)) {
                        console.error(`✅ ${key} fixed ${issue}`);
                    }
                }
                issues.set(key, newSet);
            }
            return result;
        }
        const clearCache = (filePath)=>{
            return global._nextDeleteCache == null ? void 0 : global._nextDeleteCache([
                filePath
            ]);
        };
        async function loadPartialManifest(name, pageName, type = "pages") {
            const manifestPath = path.posix.join(distDir, `server`, type === "app-route" ? "app" : type, type === "middleware" ? "" : pageName === "/" ? "index" : pageName === "/index" || pageName.startsWith("/index/") ? `/index${pageName}` : pageName, type === "app" ? "page" : type === "app-route" ? "route" : "", name);
            return JSON.parse(await readFile(path.posix.join(manifestPath), "utf-8"));
        }
        const buildManifests = new Map();
        const appBuildManifests = new Map();
        const pagesManifests = new Map();
        const appPathsManifests = new Map();
        const middlewareManifests = new Map();
        const clientToHmrSubscription = new Map();
        const clients = new Set();
        async function loadMiddlewareManifest(pageName, type) {
            middlewareManifests.set(pageName, await loadPartialManifest(MIDDLEWARE_MANIFEST, pageName, type));
        }
        async function loadBuildManifest(pageName, type = "pages") {
            buildManifests.set(pageName, await loadPartialManifest(BUILD_MANIFEST, pageName, type));
        }
        async function loadAppBuildManifest(pageName) {
            appBuildManifests.set(pageName, await loadPartialManifest(APP_BUILD_MANIFEST, pageName, "app"));
        }
        async function loadPagesManifest(pageName) {
            pagesManifests.set(pageName, await loadPartialManifest(PAGES_MANIFEST, pageName));
        }
        async function loadAppPathManifest(pageName, type = "app") {
            appPathsManifests.set(pageName, await loadPartialManifest(APP_PATHS_MANIFEST, pageName, type));
        }
        async function changeSubscription(page, endpoint, makePayload) {
            if (!endpoint || changeSubscriptions.has(page)) return;
            const changed = await endpoint.changed();
            changeSubscriptions.set(page, changed);
            for await (const change of changed){
                const payload = makePayload(page, change.change);
                if (payload) hotReloader.send(payload);
            }
        }
        try {
            async function handleEntries() {
                for await (const entrypoints of iter){
                    if (!currentEntriesHandlingResolve) {
                        currentEntriesHandling = new Promise(// eslint-disable-next-line no-loop-func
                        (resolve)=>currentEntriesHandlingResolve = resolve);
                    }
                    globalEntries.app = entrypoints.pagesAppEndpoint;
                    globalEntries.document = entrypoints.pagesDocumentEndpoint;
                    globalEntries.error = entrypoints.pagesErrorEndpoint;
                    curEntries.clear();
                    for (const [pathname, route] of entrypoints.routes){
                        switch(route.type){
                            case "page":
                            case "page-api":
                            case "app-page":
                            case "app-route":
                                {
                                    curEntries.set(pathname, route);
                                    break;
                                }
                            default:
                                Log.info(`skipping ${pathname} (${route.type})`);
                                break;
                        }
                    }
                    for (const [pathname, subscription] of changeSubscriptions){
                        if (pathname === "") {
                            continue;
                        }
                        if (!curEntries.has(pathname)) {
                            subscription.return == null ? void 0 : subscription.return();
                            changeSubscriptions.delete(pathname);
                        }
                    }
                    const { middleware  } = entrypoints;
                    // We check for explicit true/false, since it's initialized to
                    // undefined during the first loop (middlewareChanges event is
                    // unnecessary during the first serve)
                    if (prevMiddleware === true && !middleware) {
                        var _changeSubscriptions_get;
                        // Went from middleware to no middleware
                        hotReloader.send({
                            event: "middlewareChanges"
                        });
                        (_changeSubscriptions_get = changeSubscriptions.get("")) == null ? void 0 : _changeSubscriptions_get.return == null ? void 0 : _changeSubscriptions_get.return();
                        changeSubscriptions.delete("");
                    } else if (prevMiddleware === false && middleware) {
                        // Went from no middleware to middleware
                        hotReloader.send({
                            event: "middlewareChanges"
                        });
                    }
                    if (middleware) {
                        var _middlewareManifests_get;
                        await processResult("middleware", await middleware.endpoint.writeToDisk());
                        await loadMiddlewareManifest("middleware", "middleware");
                        serverFields.actualMiddlewareFile = "middleware";
                        serverFields.middleware = {
                            match: null,
                            page: "/",
                            matchers: (_middlewareManifests_get = middlewareManifests.get("middleware")) == null ? void 0 : _middlewareManifests_get.middleware["/"].matchers
                        };
                        changeSubscription("", middleware.endpoint, ()=>{
                            return {
                                event: "middlewareChanges"
                            };
                        });
                        prevMiddleware = true;
                    } else {
                        middlewareManifests.delete("middleware");
                        serverFields.actualMiddlewareFile = undefined;
                        serverFields.middleware = undefined;
                        prevMiddleware = false;
                    }
                    await propagateToWorkers("actualMiddlewareFile", serverFields.actualMiddlewareFile);
                    await propagateToWorkers("middleware", serverFields.middleware);
                    currentEntriesHandlingResolve();
                    currentEntriesHandlingResolve = undefined;
                }
            }
            handleEntries().catch((err)=>{
                console.error(err);
                process.exit(1);
            });
        } catch (e) {
            console.error(e);
        }
        function mergeBuildManifests(manifests) {
            const manifest = {
                pages: {
                    "/_app": []
                },
                // Something in next.js depends on these to exist even for app dir rendering
                devFiles: [],
                ampDevFiles: [],
                polyfillFiles: [],
                lowPriorityFiles: [
                    "static/development/_ssgManifest.js",
                    "static/development/_buildManifest.js"
                ],
                rootMainFiles: [],
                ampFirstPages: []
            };
            for (const m of manifests){
                Object.assign(manifest.pages, m.pages);
                if (m.rootMainFiles.length) manifest.rootMainFiles = m.rootMainFiles;
            }
            return manifest;
        }
        function mergeAppBuildManifests(manifests) {
            const manifest = {
                pages: {}
            };
            for (const m of manifests){
                Object.assign(manifest.pages, m.pages);
            }
            return manifest;
        }
        function mergePagesManifests(manifests) {
            const manifest = {};
            for (const m of manifests){
                Object.assign(manifest, m);
            }
            return manifest;
        }
        function mergeMiddlewareManifests(manifests) {
            const manifest = {
                version: 2,
                middleware: {},
                sortedMiddleware: [],
                functions: {}
            };
            for (const m of manifests){
                Object.assign(manifest.functions, m.functions);
                Object.assign(manifest.middleware, m.middleware);
            }
            for (const fun of Object.values(manifest.functions).concat(Object.values(manifest.middleware))){
                for (const matcher of fun.matchers){
                    if (!matcher.regexp) {
                        matcher.regexp = pathToRegexp(matcher.originalSource, [], {
                            delimiter: "/",
                            sensitive: false,
                            strict: true
                        }).source.replaceAll("\\/", "/");
                    }
                }
            }
            manifest.sortedMiddleware = Object.keys(manifest.middleware);
            return manifest;
        }
        async function writeBuildManifest() {
            const buildManifest = mergeBuildManifests(buildManifests.values());
            const buildManifestPath = path.join(distDir, "build-manifest.json");
            await clearCache(buildManifestPath);
            await writeFile(buildManifestPath, JSON.stringify(buildManifest, null, 2), "utf-8");
            const content = {
                __rewrites: {
                    afterFiles: [],
                    beforeFiles: [],
                    fallback: []
                },
                ...Object.fromEntries([
                    ...curEntries.keys()
                ].map((pathname)=>[
                        pathname,
                        `static/chunks/pages${pathname === "/" ? "/index" : pathname}.js`
                    ])),
                sortedPages: [
                    ...curEntries.keys()
                ]
            };
            const buildManifestJs = `self.__BUILD_MANIFEST = ${JSON.stringify(content)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`;
            await writeFile(path.join(distDir, "static", "development", "_buildManifest.js"), buildManifestJs, "utf-8");
            await writeFile(path.join(distDir, "static", "development", "_ssgManifest.js"), srcEmptySsgManifest, "utf-8");
        }
        async function writeAppBuildManifest() {
            const appBuildManifest = mergeAppBuildManifests(appBuildManifests.values());
            const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST);
            await clearCache(appBuildManifestPath);
            await writeFile(appBuildManifestPath, JSON.stringify(appBuildManifest, null, 2), "utf-8");
        }
        async function writePagesManifest() {
            const pagesManifest = mergePagesManifests(pagesManifests.values());
            const pagesManifestPath = path.join(distDir, "server", PAGES_MANIFEST);
            await clearCache(pagesManifestPath);
            await writeFile(pagesManifestPath, JSON.stringify(pagesManifest, null, 2), "utf-8");
        }
        async function writeAppPathsManifest() {
            const appPathsManifest = mergePagesManifests(appPathsManifests.values());
            const appPathsManifestPath = path.join(distDir, "server", APP_PATHS_MANIFEST);
            await clearCache(appPathsManifestPath);
            await writeFile(appPathsManifestPath, JSON.stringify(appPathsManifest, null, 2), "utf-8");
        }
        async function writeMiddlewareManifest() {
            const middlewareManifest = mergeMiddlewareManifests(middlewareManifests.values());
            const middlewareManifestPath = path.join(distDir, "server/middleware-manifest.json");
            await clearCache(middlewareManifestPath);
            await writeFile(middlewareManifestPath, JSON.stringify(middlewareManifest, null, 2), "utf-8");
        }
        async function writeOtherManifests() {
            const loadableManifestPath = path.join(distDir, "react-loadable-manifest.json");
            await clearCache(loadableManifestPath);
            await writeFile(loadableManifestPath, JSON.stringify({}, null, 2), "utf-8");
            // TODO: turbopack should write the correct
            // version of this
            const fontManifestPath = path.join(distDir, "server", NEXT_FONT_MANIFEST + ".json");
            await clearCache(fontManifestPath);
            await writeFile(fontManifestPath, JSON.stringify({
                pages: {},
                app: {},
                appUsingSizeAdjust: false,
                pagesUsingSizeAdjust: false
            }, null, 2));
        }
        async function subscribeToHmrEvents(id, client) {
            let mapping = clientToHmrSubscription.get(client);
            if (mapping === undefined) {
                mapping = new Map();
                clientToHmrSubscription.set(client, mapping);
            }
            if (mapping.has(id)) return;
            const subscription = project.hmrEvents(id);
            mapping.set(id, subscription);
            for await (const data of subscription){
                hotReloader.send({
                    type: "turbopack-message",
                    data
                });
            }
        }
        function unsubscribeToHmrEvents(id, client) {
            const mapping = clientToHmrSubscription.get(client);
            const subscription = mapping == null ? void 0 : mapping.get(id);
            subscription == null ? void 0 : subscription.return();
        }
        // Write empty manifests
        await mkdir(path.join(distDir, "server"), {
            recursive: true
        });
        await mkdir(path.join(distDir, "static/development"), {
            recursive: true
        });
        await writeFile(path.join(distDir, "package.json"), JSON.stringify({
            type: "commonjs"
        }, null, 2));
        await currentEntriesHandling;
        await writeBuildManifest();
        await writeAppBuildManifest();
        await writePagesManifest();
        await writeAppPathsManifest();
        await writeMiddlewareManifest();
        await writeOtherManifests();
        const turbopackHotReloader = {
            activeWebpackConfigs: undefined,
            serverStats: null,
            edgeServerStats: null,
            async run (req, _res, _parsedUrl) {
                var _req_url;
                // intercept page chunks request and ensure them with turbopack
                if ((_req_url = req.url) == null ? void 0 : _req_url.startsWith("/_next/static/chunks/pages/")) {
                    const params = matchNextPageBundleRequest(req.url);
                    if (params) {
                        const decodedPagePath = `/${params.path.map((param)=>decodeURIComponent(param)).join("/")}`;
                        await hotReloader.ensurePage({
                            page: decodedPagePath,
                            clientOnly: false
                        }).catch(console.error);
                    }
                }
                // Request was not finished.
                return {
                    finished: undefined
                };
            },
            onHMR (req, socket, head) {
                wsServer.handleUpgrade(req, socket, head, (client)=>{
                    clients.add(client);
                    client.on("close", ()=>clients.delete(client));
                    // server sends:
                    //   - Middleware HMR:
                    //     - { action: 'building' }
                    //     - { action: 'sync', hash, errors, warnings, versionInfo }
                    //     - { action: 'built', hash }
                    client.addEventListener("message", ({ data  })=>{
                        const parsedData = JSON.parse(typeof data !== "string" ? data.toString() : data);
                        // Next.js messages
                        switch(parsedData.event){
                            case "ping":
                                {
                                    // const result = parsedData.appDirRoute
                                    // ? handleAppDirPing(parsedData.tree)
                                    // : handlePing(parsedData.page)
                                    const result = {
                                        success: true
                                    };
                                    hotReloader.send({
                                        ...result,
                                        [parsedData.appDirRoute ? "action" : "event"]: "pong"
                                    });
                                    break;
                                }
                            case "client-error":
                            case "client-warning":
                            case "client-success":
                            case "server-component-reload-page":
                            case "client-reload-page":
                            case "client-full-reload":
                            case "span-end":
                                break;
                            default:
                                // Might be a Turbopack message...
                                if (!parsedData.type) {
                                    throw new Error(`unrecognized HMR message "${data}"`);
                                }
                        }
                        // Turbopack messages
                        switch(parsedData.type){
                            case "turbopack-subscribe":
                                subscribeToHmrEvents(parsedData.path, client);
                                break;
                            case "turbopack-unsubscribe":
                                unsubscribeToHmrEvents(parsedData.path, client);
                                break;
                            default:
                                // Might have been a Next.js message...
                                if (!parsedData.event) {
                                    throw new Error(`unrecognized Turbopack message "${data}"`);
                                }
                        }
                    });
                    client.send(JSON.stringify({
                        type: "turbopack-connected"
                    }));
                });
            },
            send (action, ...data) {
                const payload = JSON.stringify(typeof action === "string" ? {
                    action,
                    data
                } : action);
                for (const client of clients){
                    client.send(payload);
                }
            },
            setHmrServerError (_error) {
            // Not implemented yet.
            },
            clearHmrServerError () {
            // Not implemented yet.
            },
            async start () {
            // Not implemented yet.
            },
            async stop () {
            // Not implemented yet.
            },
            async getCompilationErrors (_page) {
                return [];
            },
            invalidate () {
            // Not implemented yet.
            },
            async buildFallbackError () {
            // Not implemented yet.
            },
            async ensurePage ({ page: inputPage , // Unused parameters
            // clientOnly,
            // appPaths,
            match , isApp  }) {
                var _match_definition;
                let page = (match == null ? void 0 : (_match_definition = match.definition) == null ? void 0 : _match_definition.pathname) ?? inputPage;
                if (page === "/_error") {
                    var _globalEntries_app, _globalEntries_document, _globalEntries_error;
                    await processResult("_app", await ((_globalEntries_app = globalEntries.app) == null ? void 0 : _globalEntries_app.writeToDisk()));
                    await loadBuildManifest("_app");
                    await loadPagesManifest("_app");
                    await processResult("_document", await ((_globalEntries_document = globalEntries.document) == null ? void 0 : _globalEntries_document.writeToDisk()));
                    changeSubscription("_document", globalEntries == null ? void 0 : globalEntries.document, ()=>{
                        return {
                            action: "reloadPage"
                        };
                    });
                    await loadPagesManifest("_document");
                    await processResult(page, await ((_globalEntries_error = globalEntries.error) == null ? void 0 : _globalEntries_error.writeToDisk()));
                    await loadBuildManifest("_error");
                    await loadPagesManifest("_error");
                    await writeBuildManifest();
                    await writePagesManifest();
                    await writeMiddlewareManifest();
                    await writeOtherManifests();
                    return;
                }
                await currentEntriesHandling;
                const route = curEntries.get(page);
                if (!route) {
                    // TODO: why is this entry missing in turbopack?
                    if (page === "/_app") return;
                    if (page === "/_document") return;
                    if (page === "/middleware") return;
                    throw new PageNotFoundError(`route not found ${page}`);
                }
                switch(route.type){
                    case "page":
                        {
                            var _globalEntries_app1, _globalEntries_document1;
                            if (isApp) {
                                throw new Error(`mis-matched route type: isApp && page for ${page}`);
                            }
                            await processResult("_app", await ((_globalEntries_app1 = globalEntries.app) == null ? void 0 : _globalEntries_app1.writeToDisk()));
                            await loadBuildManifest("_app");
                            await loadPagesManifest("_app");
                            await processResult("_document", await ((_globalEntries_document1 = globalEntries.document) == null ? void 0 : _globalEntries_document1.writeToDisk()));
                            changeSubscription("_document", globalEntries == null ? void 0 : globalEntries.document, ()=>{
                                return {
                                    action: "reloadPage"
                                };
                            });
                            await loadPagesManifest("_document");
                            const writtenEndpoint = await processResult(page, await route.htmlEndpoint.writeToDisk());
                            changeSubscription(page, route.dataEndpoint, (pageName, change)=>{
                                switch(change){
                                    case ServerClientChangeType.Server:
                                    case ServerClientChangeType.Both:
                                        return {
                                            event: "serverOnlyChanges",
                                            pages: [
                                                pageName
                                            ]
                                        };
                                    default:
                                }
                            });
                            const type = writtenEndpoint == null ? void 0 : writtenEndpoint.type;
                            await loadBuildManifest(page);
                            await loadPagesManifest(page);
                            if (type === "edge") {
                                await loadMiddlewareManifest(page, "pages");
                            } else {
                                middlewareManifests.delete(page);
                            }
                            await writeBuildManifest();
                            await writePagesManifest();
                            await writeMiddlewareManifest();
                            await writeOtherManifests();
                            break;
                        }
                    case "page-api":
                        {
                            // We don't throw on ensureOpts.isApp === true here
                            // since this can happen when app pages make
                            // api requests to page API routes.
                            const writtenEndpoint = await processResult(page, await route.endpoint.writeToDisk());
                            const type = writtenEndpoint == null ? void 0 : writtenEndpoint.type;
                            await loadPagesManifest(page);
                            if (type === "edge") {
                                await loadMiddlewareManifest(page, "pages");
                            } else {
                                middlewareManifests.delete(page);
                            }
                            await writePagesManifest();
                            await writeMiddlewareManifest();
                            await writeOtherManifests();
                            break;
                        }
                    case "app-page":
                        {
                            await processResult(page, await route.htmlEndpoint.writeToDisk());
                            changeSubscription(page, route.rscEndpoint, (_page, change)=>{
                                switch(change){
                                    case ServerClientChangeType.Server:
                                    case ServerClientChangeType.Both:
                                        return {
                                            action: "serverComponentChanges"
                                        };
                                    default:
                                }
                            });
                            await loadAppBuildManifest(page);
                            await loadBuildManifest(page, "app");
                            await loadAppPathManifest(page, "app");
                            await writeAppBuildManifest();
                            await writeBuildManifest();
                            await writeAppPathsManifest();
                            await writeMiddlewareManifest();
                            await writeOtherManifests();
                            break;
                        }
                    case "app-route":
                        {
                            var _ref;
                            const type = (_ref = await processResult(page, await route.endpoint.writeToDisk())) == null ? void 0 : _ref.type;
                            await loadAppPathManifest(page, "app-route");
                            if (type === "edge") {
                                await loadMiddlewareManifest(page, "app-route");
                            } else {
                                middlewareManifests.delete(page);
                            }
                            await writeAppBuildManifest();
                            await writeAppPathsManifest();
                            await writeMiddlewareManifest();
                            await writeMiddlewareManifest();
                            await writeOtherManifests();
                            break;
                        }
                    default:
                        {
                            throw new Error(`unknown route type ${route.type} for ${page}`);
                        }
                }
            }
        };
        hotReloader = turbopackHotReloader;
    } else {
        hotReloader = new HotReloader(opts.dir, {
            appDir,
            pagesDir,
            distDir: distDir,
            config: opts.nextConfig,
            buildId: "development",
            telemetry: opts.telemetry,
            rewrites: opts.fsChecker.rewrites,
            previewProps: opts.fsChecker.prerenderManifest.preview
        });
    }
    await hotReloader.start();
    if (opts.nextConfig.experimental.nextScriptWorkers) {
        await verifyPartytownSetup(opts.dir, path.join(distDir, CLIENT_STATIC_FILES_PATH));
    }
    opts.fsChecker.ensureCallback(async function ensure(item) {
        if (item.type === "appFile" || item.type === "pageFile") {
            await hotReloader.ensurePage({
                clientOnly: false,
                page: item.itemPath,
                isApp: item.type === "appFile"
            });
        }
    });
    let resolved = false;
    let prevSortedRoutes = [];
    await new Promise(async (resolve, reject)=>{
        if (pagesDir) {
            // Watchpack doesn't emit an event for an empty directory
            fs.readdir(pagesDir, (_, files)=>{
                if (files == null ? void 0 : files.length) {
                    return;
                }
                if (!resolved) {
                    resolve();
                    resolved = true;
                }
            });
        }
        const pages = pagesDir ? [
            pagesDir
        ] : [];
        const app = appDir ? [
            appDir
        ] : [];
        const directories = [
            ...pages,
            ...app
        ];
        const rootDir = pagesDir || appDir;
        const files = [
            ...getPossibleMiddlewareFilenames(path.join(rootDir, ".."), nextConfig.pageExtensions),
            ...getPossibleInstrumentationHookFilenames(path.join(rootDir, ".."), nextConfig.pageExtensions)
        ];
        let nestedMiddleware = [];
        const envFiles = [
            ".env.development.local",
            ".env.local",
            ".env.development",
            ".env"
        ].map((file)=>path.join(dir, file));
        files.push(...envFiles);
        // tsconfig/jsconfig paths hot-reloading
        const tsconfigPaths = [
            path.join(dir, "tsconfig.json"),
            path.join(dir, "jsconfig.json")
        ];
        files.push(...tsconfigPaths);
        const wp = new Watchpack({
            ignored: (pathname)=>{
                return !files.some((file)=>file.startsWith(pathname)) && !directories.some((d)=>pathname.startsWith(d) || d.startsWith(pathname));
            }
        });
        const fileWatchTimes = new Map();
        let enabledTypeScript = usingTypeScript;
        let previousClientRouterFilters;
        let previousConflictingPagePaths = new Set();
        wp.on("aggregated", async ()=>{
            var _serverFields_middleware, _serverFields_middleware1, _generateInterceptionRoutesRewrites;
            let middlewareMatchers;
            const routedPages = [];
            const knownFiles = wp.getTimeInfoEntries();
            const appPaths = {};
            const pageNameSet = new Set();
            const conflictingAppPagePaths = new Set();
            const appPageFilePaths = new Map();
            const pagesPageFilePaths = new Map();
            let envChange = false;
            let tsconfigChange = false;
            let conflictingPageChange = 0;
            let hasRootAppNotFound = false;
            const { appFiles , pageFiles  } = opts.fsChecker;
            appFiles.clear();
            pageFiles.clear();
            devPageFiles.clear();
            const sortedKnownFiles = [
                ...knownFiles.keys()
            ].sort(sortByPageExts(nextConfig.pageExtensions));
            for (const fileName of sortedKnownFiles){
                if (!files.includes(fileName) && !directories.some((d)=>fileName.startsWith(d))) {
                    continue;
                }
                const meta = knownFiles.get(fileName);
                const watchTime = fileWatchTimes.get(fileName);
                const watchTimeChange = watchTime && watchTime !== (meta == null ? void 0 : meta.timestamp);
                fileWatchTimes.set(fileName, meta.timestamp);
                if (envFiles.includes(fileName)) {
                    if (watchTimeChange) {
                        envChange = true;
                    }
                    continue;
                }
                if (tsconfigPaths.includes(fileName)) {
                    if (fileName.endsWith("tsconfig.json")) {
                        enabledTypeScript = true;
                    }
                    if (watchTimeChange) {
                        tsconfigChange = true;
                    }
                    continue;
                }
                if ((meta == null ? void 0 : meta.accuracy) === undefined || !validFileMatcher.isPageFile(fileName)) {
                    continue;
                }
                const isAppPath = Boolean(appDir && normalizePathSep(fileName).startsWith(normalizePathSep(appDir) + "/"));
                const isPagePath = Boolean(pagesDir && normalizePathSep(fileName).startsWith(normalizePathSep(pagesDir) + "/"));
                const rootFile = absolutePathToPage(fileName, {
                    dir: dir,
                    extensions: nextConfig.pageExtensions,
                    keepIndex: false,
                    pagesType: "root"
                });
                if (isMiddlewareFile(rootFile)) {
                    var _staticInfo_middleware;
                    const staticInfo = await getStaticInfoIncludingLayouts({
                        pageFilePath: fileName,
                        config: nextConfig,
                        appDir: appDir,
                        page: rootFile,
                        isDev: true,
                        isInsideAppDir: isAppPath,
                        pageExtensions: nextConfig.pageExtensions
                    });
                    if (nextConfig.output === "export") {
                        Log.error('Middleware cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
                        continue;
                    }
                    serverFields.actualMiddlewareFile = rootFile;
                    await propagateToWorkers("actualMiddlewareFile", serverFields.actualMiddlewareFile);
                    middlewareMatchers = ((_staticInfo_middleware = staticInfo.middleware) == null ? void 0 : _staticInfo_middleware.matchers) || [
                        {
                            regexp: ".*",
                            originalSource: "/:path*"
                        }
                    ];
                    continue;
                }
                if (isInstrumentationHookFile(rootFile) && nextConfig.experimental.instrumentationHook) {
                    NextBuildContext.hasInstrumentationHook = true;
                    serverFields.actualInstrumentationHookFile = rootFile;
                    await propagateToWorkers("actualInstrumentationHookFile", serverFields.actualInstrumentationHookFile);
                    continue;
                }
                if (fileName.endsWith(".ts") || fileName.endsWith(".tsx")) {
                    enabledTypeScript = true;
                }
                if (!(isAppPath || isPagePath)) {
                    continue;
                }
                // Collect all current filenames for the TS plugin to use
                devPageFiles.add(fileName);
                let pageName = absolutePathToPage(fileName, {
                    dir: isAppPath ? appDir : pagesDir,
                    extensions: nextConfig.pageExtensions,
                    keepIndex: isAppPath,
                    pagesType: isAppPath ? "app" : "pages"
                });
                if (!isAppPath && pageName.startsWith("/api/") && nextConfig.output === "export") {
                    Log.error('API Routes cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export');
                    continue;
                }
                if (isAppPath) {
                    const isRootNotFound = validFileMatcher.isRootNotFound(fileName);
                    hasRootAppNotFound = true;
                    if (isRootNotFound) {
                        continue;
                    }
                    if (!isRootNotFound && !validFileMatcher.isAppRouterPage(fileName)) {
                        continue;
                    }
                    // Ignore files/directories starting with `_` in the app directory
                    if (normalizePathSep(pageName).includes("/_")) {
                        continue;
                    }
                    const originalPageName = pageName;
                    pageName = normalizeAppPath(pageName).replace(/%5F/g, "_");
                    if (!appPaths[pageName]) {
                        appPaths[pageName] = [];
                    }
                    appPaths[pageName].push(originalPageName);
                    if (useFileSystemPublicRoutes) {
                        appFiles.add(pageName);
                    }
                    if (routedPages.includes(pageName)) {
                        continue;
                    }
                } else {
                    if (useFileSystemPublicRoutes) {
                        pageFiles.add(pageName);
                        // always add to nextDataRoutes for now but in future only add
                        // entries that actually use getStaticProps/getServerSideProps
                        opts.fsChecker.nextDataRoutes.add(pageName);
                    }
                }
                (isAppPath ? appPageFilePaths : pagesPageFilePaths).set(pageName, fileName);
                if (appDir && pageNameSet.has(pageName)) {
                    conflictingAppPagePaths.add(pageName);
                } else {
                    pageNameSet.add(pageName);
                }
                /**
         * If there is a middleware that is not declared in the root we will
         * warn without adding it so it doesn't make its way into the system.
         */ if (/[\\\\/]_middleware$/.test(pageName)) {
                    nestedMiddleware.push(pageName);
                    continue;
                }
                routedPages.push(pageName);
            }
            const numConflicting = conflictingAppPagePaths.size;
            conflictingPageChange = numConflicting - previousConflictingPagePaths.size;
            if (conflictingPageChange !== 0) {
                if (numConflicting > 0) {
                    let errorMessage = `Conflicting app and page file${numConflicting === 1 ? " was" : "s were"} found, please remove the conflicting files to continue:\n`;
                    for (const p of conflictingAppPagePaths){
                        const appPath = path.relative(dir, appPageFilePaths.get(p));
                        const pagesPath = path.relative(dir, pagesPageFilePaths.get(p));
                        errorMessage += `  "${pagesPath}" - "${appPath}"\n`;
                    }
                    hotReloader.setHmrServerError(new Error(errorMessage));
                } else if (numConflicting === 0) {
                    hotReloader.clearHmrServerError();
                    await propagateToWorkers("reloadMatchers", undefined);
                }
            }
            previousConflictingPagePaths = conflictingAppPagePaths;
            let clientRouterFilters;
            if (nextConfig.experimental.clientRouterFilter) {
                clientRouterFilters = createClientRouterFilter(Object.keys(appPaths), nextConfig.experimental.clientRouterFilterRedirects ? (nextConfig._originalRedirects || []).filter((r)=>!r.internal) : [], nextConfig.experimental.clientRouterFilterAllowedRate);
                if (!previousClientRouterFilters || JSON.stringify(previousClientRouterFilters) !== JSON.stringify(clientRouterFilters)) {
                    envChange = true;
                    previousClientRouterFilters = clientRouterFilters;
                }
            }
            if (!usingTypeScript && enabledTypeScript) {
                // we tolerate the error here as this is best effort
                // and the manual install command will be shown
                await verifyTypeScript(opts).then(()=>{
                    tsconfigChange = true;
                }).catch(()=>{});
            }
            if (envChange || tsconfigChange) {
                var _hotReloader_activeWebpackConfigs;
                if (envChange) {
                    loadEnvConfig(dir, true, Log, true);
                    await propagateToWorkers("loadEnvConfig", [
                        {
                            dev: true,
                            forceReload: true,
                            silent: true
                        }
                    ]);
                }
                let tsconfigResult;
                if (tsconfigChange) {
                    try {
                        tsconfigResult = await loadJsConfig(dir, nextConfig);
                    } catch (_) {
                    /* do we want to log if there are syntax errors in tsconfig  while editing? */ }
                }
                (_hotReloader_activeWebpackConfigs = hotReloader.activeWebpackConfigs) == null ? void 0 : _hotReloader_activeWebpackConfigs.forEach((config, idx)=>{
                    const isClient = idx === 0;
                    const isNodeServer = idx === 1;
                    const isEdgeServer = idx === 2;
                    const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0;
                    if (tsconfigChange) {
                        var _config_resolve, _config_resolve_plugins;
                        (_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_plugins = _config_resolve.plugins) == null ? void 0 : _config_resolve_plugins.forEach((plugin)=>{
                            // look for the JsConfigPathsPlugin and update with
                            // the latest paths/baseUrl config
                            if (plugin && plugin.jsConfigPlugin && tsconfigResult) {
                                var _config_resolve, _config_resolve_modules, _jsConfig_compilerOptions;
                                const { resolvedBaseUrl , jsConfig  } = tsconfigResult;
                                const currentResolvedBaseUrl = plugin.resolvedBaseUrl;
                                const resolvedUrlIndex = (_config_resolve = config.resolve) == null ? void 0 : (_config_resolve_modules = _config_resolve.modules) == null ? void 0 : _config_resolve_modules.findIndex((item)=>item === currentResolvedBaseUrl);
                                if (resolvedBaseUrl && resolvedBaseUrl !== currentResolvedBaseUrl) {
                                    var _config_resolve1, _config_resolve_modules1;
                                    // remove old baseUrl and add new one
                                    if (resolvedUrlIndex && resolvedUrlIndex > -1) {
                                        var _config_resolve2, _config_resolve_modules2;
                                        (_config_resolve2 = config.resolve) == null ? void 0 : (_config_resolve_modules2 = _config_resolve2.modules) == null ? void 0 : _config_resolve_modules2.splice(resolvedUrlIndex, 1);
                                    }
                                    (_config_resolve1 = config.resolve) == null ? void 0 : (_config_resolve_modules1 = _config_resolve1.modules) == null ? void 0 : _config_resolve_modules1.push(resolvedBaseUrl);
                                }
                                if ((jsConfig == null ? void 0 : (_jsConfig_compilerOptions = jsConfig.compilerOptions) == null ? void 0 : _jsConfig_compilerOptions.paths) && resolvedBaseUrl) {
                                    Object.keys(plugin.paths).forEach((key)=>{
                                        delete plugin.paths[key];
                                    });
                                    Object.assign(plugin.paths, jsConfig.compilerOptions.paths);
                                    plugin.resolvedBaseUrl = resolvedBaseUrl;
                                }
                            }
                        });
                    }
                    if (envChange) {
                        var _config_plugins;
                        (_config_plugins = config.plugins) == null ? void 0 : _config_plugins.forEach((plugin)=>{
                            // we look for the DefinePlugin definitions so we can
                            // update them on the active compilers
                            if (plugin && typeof plugin.definitions === "object" && plugin.definitions.__NEXT_DEFINE_ENV) {
                                const newDefine = getDefineEnv({
                                    allowedRevalidateHeaderKeys: undefined,
                                    clientRouterFilters,
                                    config: nextConfig,
                                    dev: true,
                                    distDir,
                                    fetchCacheKeyPrefix: undefined,
                                    hasRewrites,
                                    isClient,
                                    isEdgeServer,
                                    isNodeOrEdgeCompilation: isNodeServer || isEdgeServer,
                                    isNodeServer,
                                    middlewareMatchers: undefined,
                                    previewModeId: undefined
                                });
                                Object.keys(plugin.definitions).forEach((key)=>{
                                    if (!(key in newDefine)) {
                                        delete plugin.definitions[key];
                                    }
                                });
                                Object.assign(plugin.definitions, newDefine);
                            }
                        });
                    }
                });
                hotReloader.invalidate({
                    reloadAfterInvalidation: envChange
                });
            }
            if (nestedMiddleware.length > 0) {
                Log.error(new NestedMiddlewareError(nestedMiddleware, dir, pagesDir || appDir).message);
                nestedMiddleware = [];
            }
            // Make sure to sort parallel routes to make the result deterministic.
            serverFields.appPathRoutes = Object.fromEntries(Object.entries(appPaths).map(([k, v])=>[
                    k,
                    v.sort()
                ]));
            await propagateToWorkers("appPathRoutes", serverFields.appPathRoutes);
            // TODO: pass this to fsChecker/next-dev-server?
            serverFields.middleware = middlewareMatchers ? {
                match: null,
                page: "/",
                matchers: middlewareMatchers
            } : undefined;
            await propagateToWorkers("middleware", serverFields.middleware);
            serverFields.hasAppNotFound = hasRootAppNotFound;
            opts.fsChecker.middlewareMatcher = ((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) ? getMiddlewareRouteMatcher((_serverFields_middleware1 = serverFields.middleware) == null ? void 0 : _serverFields_middleware1.matchers) : undefined;
            opts.fsChecker.interceptionRoutes = ((_generateInterceptionRoutesRewrites = generateInterceptionRoutesRewrites(Object.keys(appPaths))) == null ? void 0 : _generateInterceptionRoutesRewrites.map((item)=>buildCustomRoute("before_files_rewrite", item, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes))) || [];
            const exportPathMap = typeof nextConfig.exportPathMap === "function" && await (nextConfig.exportPathMap == null ? void 0 : nextConfig.exportPathMap({}, {
                dev: true,
                dir: opts.dir,
                outDir: null,
                distDir: distDir,
                buildId: "development"
            })) || {};
            for (const [key, value] of Object.entries(exportPathMap || {})){
                opts.fsChecker.interceptionRoutes.push(buildCustomRoute("before_files_rewrite", {
                    source: key,
                    destination: `${value.page}${value.query ? "?" : ""}${qs.stringify(value.query)}`
                }, opts.nextConfig.basePath, opts.nextConfig.experimental.caseSensitiveRoutes));
            }
            try {
                // we serve a separate manifest with all pages for the client in
                // dev mode so that we can match a page after a rewrite on the client
                // before it has been built and is populated in the _buildManifest
                const sortedRoutes = getSortedRoutes(routedPages);
                opts.fsChecker.dynamicRoutes = sortedRoutes.map((page)=>{
                    const regex = getRouteRegex(page);
                    return {
                        regex: regex.re.toString(),
                        match: getRouteMatcher(regex),
                        page
                    };
                });
                const dataRoutes = [];
                for (const page of sortedRoutes){
                    const route = buildDataRoute(page, "development");
                    const routeRegex = getRouteRegex(route.page);
                    dataRoutes.push({
                        ...route,
                        regex: routeRegex.re.toString(),
                        match: getRouteMatcher({
                            // TODO: fix this in the manifest itself, must also be fixed in
                            // upstream builder that relies on this
                            re: opts.nextConfig.i18n ? new RegExp(route.dataRouteRegex.replace(`/development/`, `/development/(?<nextLocale>.+?)/`)) : new RegExp(route.dataRouteRegex),
                            groups: routeRegex.groups
                        })
                    });
                }
                opts.fsChecker.dynamicRoutes.unshift(...dataRoutes);
                if (!(prevSortedRoutes == null ? void 0 : prevSortedRoutes.every((val, idx)=>val === sortedRoutes[idx]))) {
                    const addedRoutes = sortedRoutes.filter((route)=>!prevSortedRoutes.includes(route));
                    const removedRoutes = prevSortedRoutes.filter((route)=>!sortedRoutes.includes(route));
                    // emit the change so clients fetch the update
                    hotReloader.send("devPagesManifestUpdate", {
                        devPagesManifest: true
                    });
                    addedRoutes.forEach((route)=>{
                        hotReloader.send("addedPage", route);
                    });
                    removedRoutes.forEach((route)=>{
                        hotReloader.send("removedPage", route);
                    });
                }
                prevSortedRoutes = sortedRoutes;
                if (!resolved) {
                    resolve();
                    resolved = true;
                }
            } catch (e) {
                if (!resolved) {
                    reject(e);
                    resolved = true;
                } else {
                    Log.warn("Failed to reload dynamic routes:", e);
                }
            } finally{
                // Reload the matchers. The filesystem would have been written to,
                // and the matchers need to re-scan it to update the router.
                await propagateToWorkers("reloadMatchers", undefined);
            }
        });
        wp.watch({
            directories: [
                dir
            ],
            startTime: 0
        });
    });
    const clientPagesManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_PAGES_MANIFEST}`;
    opts.fsChecker.devVirtualFsItems.add(clientPagesManifestPath);
    const devMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_MIDDLEWARE_MANIFEST}`;
    opts.fsChecker.devVirtualFsItems.add(devMiddlewareManifestPath);
    async function requestHandler(req, res) {
        const parsedUrl = url.parse(req.url || "/");
        if (parsedUrl.pathname === clientPagesManifestPath) {
            res.statusCode = 200;
            res.setHeader("Content-Type", "application/json; charset=utf-8");
            res.end(JSON.stringify({
                pages: prevSortedRoutes.filter((route)=>!opts.fsChecker.appFiles.has(route))
            }));
            return {
                finished: true
            };
        }
        if (parsedUrl.pathname === devMiddlewareManifestPath) {
            var _serverFields_middleware;
            res.statusCode = 200;
            res.setHeader("Content-Type", "application/json; charset=utf-8");
            res.end(JSON.stringify(((_serverFields_middleware = serverFields.middleware) == null ? void 0 : _serverFields_middleware.matchers) || []));
            return {
                finished: true
            };
        }
        return {
            finished: false
        };
    }
    async function logErrorWithOriginalStack(err, type) {
        let usedOriginalStack = false;
        if (isError(err) && err.stack) {
            try {
                const frames = parseStack(err.stack);
                // Filter out internal edge related runtime stack
                const frame = frames.find(({ file  })=>{
                    return !(file == null ? void 0 : file.startsWith("eval")) && !(file == null ? void 0 : file.includes("web/adapter")) && !(file == null ? void 0 : file.includes("web/globals")) && !(file == null ? void 0 : file.includes("sandbox/context")) && !(file == null ? void 0 : file.includes("<anonymous>"));
                });
                if ((frame == null ? void 0 : frame.lineNumber) && (frame == null ? void 0 : frame.file)) {
                    var _hotReloader_edgeServerStats, _hotReloader_serverStats, _frame_file, _frame_file1, _hotReloader_serverStats1, _hotReloader_edgeServerStats1;
                    const moduleId = frame.file.replace(/^(webpack-internal:\/\/\/|file:\/\/)/, "");
                    const modulePath = frame.file.replace(/^(webpack-internal:\/\/\/|file:\/\/)(\(.*\)\/)?/, "");
                    const src = getErrorSource(err);
                    const isEdgeCompiler = src === COMPILER_NAMES.edgeServer;
                    const compilation = isEdgeCompiler ? (_hotReloader_edgeServerStats = hotReloader.edgeServerStats) == null ? void 0 : _hotReloader_edgeServerStats.compilation : (_hotReloader_serverStats = hotReloader.serverStats) == null ? void 0 : _hotReloader_serverStats.compilation;
                    const source = await getSourceById(!!((_frame_file = frame.file) == null ? void 0 : _frame_file.startsWith(path.sep)) || !!((_frame_file1 = frame.file) == null ? void 0 : _frame_file1.startsWith("file:")), moduleId, compilation);
                    const originalFrame = await createOriginalStackFrame({
                        line: frame.lineNumber,
                        column: frame.column,
                        source,
                        frame,
                        moduleId,
                        modulePath,
                        rootDirectory: opts.dir,
                        errorMessage: err.message,
                        serverCompilation: isEdgeCompiler ? undefined : (_hotReloader_serverStats1 = hotReloader.serverStats) == null ? void 0 : _hotReloader_serverStats1.compilation,
                        edgeCompilation: isEdgeCompiler ? (_hotReloader_edgeServerStats1 = hotReloader.edgeServerStats) == null ? void 0 : _hotReloader_edgeServerStats1.compilation : undefined
                    }).catch(()=>{});
                    if (originalFrame) {
                        const { originalCodeFrame , originalStackFrame  } = originalFrame;
                        const { file , lineNumber , column , methodName  } = originalStackFrame;
                        Log[type === "warning" ? "warn" : "error"](`${file} (${lineNumber}:${column}) @ ${methodName}`);
                        if (isEdgeCompiler) {
                            err = err.message;
                        }
                        if (type === "warning") {
                            Log.warn(err);
                        } else if (type === "app-dir") {
                            logAppDirError(err);
                        } else if (type) {
                            Log.error(`${type}:`, err);
                        } else {
                            Log.error(err);
                        }
                        console[type === "warning" ? "warn" : "error"](originalCodeFrame);
                        usedOriginalStack = true;
                    }
                }
            } catch (_) {
            // failed to load original stack using source maps
            // this un-actionable by users so we don't show the
            // internal error and only show the provided stack
            }
        }
        if (!usedOriginalStack) {
            if (type === "warning") {
                Log.warn(err);
            } else if (type === "app-dir") {
                logAppDirError(err);
            } else if (type) {
                Log.error(`${type}:`, err);
            } else {
                Log.error(err);
            }
        }
    }
    return {
        serverFields,
        hotReloader,
        requestHandler,
        logErrorWithOriginalStack,
        async ensureMiddleware () {
            if (!serverFields.actualMiddlewareFile) return;
            return hotReloader.ensurePage({
                page: serverFields.actualMiddlewareFile,
                clientOnly: false
            });
        }
    };
}
export async function setupDev(opts) {
    const isSrcDir = path.relative(opts.dir, opts.pagesDir || opts.appDir || "").startsWith("src");
    const result = await startWatcher(opts);
    opts.telemetry.record(eventCliSession(path.join(opts.dir, opts.nextConfig.distDir), opts.nextConfig, {
        webpackVersion: 5,
        isSrcDir,
        turboFlag: false,
        cliCommand: "dev",
        appDir: !!opts.appDir,
        pagesDir: !!opts.pagesDir,
        isCustomServer: !!opts.isCustomServer,
        hasNowJson: !!await findUp("now.json", {
            cwd: opts.dir
        })
    }));
    return result;
}

//# sourceMappingURL=setup-dev.js.map