"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.loadDefaultErrorComponents = loadDefaultErrorComponents;
exports.loadComponents = loadComponents;
var _constants = require("../shared/lib/constants");
var _path = require("path");
var _require = require("./require");
var _interopDefault = require("../lib/interop-default");
async function loadDefaultErrorComponents(distDir) {
    const Document = (0, _interopDefault).interopDefault(require('next/dist/pages/_document'));
    const AppMod = require('next/dist/pages/_app');
    const App = (0, _interopDefault).interopDefault(AppMod);
    const ComponentMod = require('next/dist/pages/_error');
    const Component = (0, _interopDefault).interopDefault(ComponentMod);
    return {
        App,
        Document,
        Component,
        pageConfig: {},
        buildManifest: require((0, _path).join(distDir, `fallback-${_constants.BUILD_MANIFEST}`)),
        reactLoadableManifest: {},
        ComponentMod,
        AppMod,
        // Use App for fallback
        AppServerMod: AppMod
    };
}
async function loadComponents(distDir, pathname, serverless, serverComponents, rootEnabled) {
    if (serverless) {
        const ComponentMod = await (0, _require).requirePage(pathname, distDir, serverless);
        if (typeof ComponentMod === 'string') {
            return {
                Component: ComponentMod,
                pageConfig: {},
                ComponentMod
            };
        }
        let { default: Component , getStaticProps , getStaticPaths , getServerSideProps ,  } = ComponentMod;
        Component = await Component;
        getStaticProps = await getStaticProps;
        getStaticPaths = await getStaticPaths;
        getServerSideProps = await getServerSideProps;
        const pageConfig = await ComponentMod.config || {};
        return {
            Component,
            pageConfig,
            getStaticProps,
            getStaticPaths,
            getServerSideProps,
            ComponentMod
        };
    }
    const [DocumentMod, AppMod, ComponentMod, AppServerMod] = await Promise.all([
        (0, _require).requirePage('/_document', distDir, serverless, rootEnabled),
        (0, _require).requirePage('/_app', distDir, serverless, rootEnabled),
        (0, _require).requirePage(pathname, distDir, serverless, rootEnabled),
        serverComponents ? (0, _require).requirePage('/_app.server', distDir, serverless, rootEnabled) : null, 
    ]);
    const [buildManifest, reactLoadableManifest, serverComponentManifest] = await Promise.all([
        require((0, _path).join(distDir, _constants.BUILD_MANIFEST)),
        require((0, _path).join(distDir, _constants.REACT_LOADABLE_MANIFEST)),
        serverComponents ? require((0, _path).join(distDir, 'server', _constants.MIDDLEWARE_FLIGHT_MANIFEST + '.json')) : null, 
    ]);
    const Component = (0, _interopDefault).interopDefault(ComponentMod);
    const Document = (0, _interopDefault).interopDefault(DocumentMod);
    const App = (0, _interopDefault).interopDefault(AppMod);
    const { getServerSideProps , getStaticProps , getStaticPaths  } = ComponentMod;
    let isViewPath = false;
    if (rootEnabled) {
        const pagePath = (0, _require).getPagePath(pathname, distDir, serverless, false, undefined, rootEnabled);
        isViewPath = !!(pagePath === null || pagePath === void 0 ? void 0 : pagePath.match(/server[/\\]views[/\\]/));
    }
    return {
        App,
        Document,
        Component,
        buildManifest,
        reactLoadableManifest,
        pageConfig: ComponentMod.config || {},
        ComponentMod,
        AppMod,
        AppServerMod,
        getServerSideProps,
        getStaticProps,
        getStaticPaths,
        serverComponentManifest,
        isViewPath
    };
}

//# sourceMappingURL=load-components.js.map