"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.getPageStaticInfo = getPageStaticInfo;
var _extractConstValue = require("./extract-const-value");
var _parseModule = require("./parse-module");
var _fs = require("fs");
var _tryToParsePath = require("../../lib/try-to-parse-path");
var _utils = require("../utils");
async function getPageStaticInfo(params) {
    const { isDev , pageFilePath , nextConfig  } = params;
    const fileContent = await tryToReadFile(pageFilePath, !isDev) || '';
    if (/runtime|getStaticProps|getServerSideProps|matcher/.test(fileContent)) {
        var ref;
        const swcAST = await (0, _parseModule).parseModule(pageFilePath, fileContent);
        const { ssg , ssr  } = checkExports(swcAST);
        const config = (0, _extractConstValue).tryToExtractExportedConstValue(swcAST, 'config') || {};
        const runtime = (config === null || config === void 0 ? void 0 : config.runtime) === 'edge' ? 'edge' : ssr || ssg ? (config === null || config === void 0 ? void 0 : config.runtime) || ((ref = nextConfig.experimental) === null || ref === void 0 ? void 0 : ref.runtime) : undefined;
        const middlewareConfig = (0, _utils).isMiddlewareFile(params.page) && getMiddlewareConfig(config);
        return {
            ssr,
            ssg,
            ...middlewareConfig && {
                middleware: middlewareConfig
            },
            ...runtime && {
                runtime
            }
        };
    }
    return {
        ssr: false,
        ssg: false
    };
}
/**
 * Receives a parsed AST from SWC and checks if it belongs to a module that
 * requires a runtime to be specified. Those are:
 *   - Modules with `export function getStaticProps | getServerSideProps`
 *   - Modules with `export { getStaticProps | getServerSideProps } <from ...>`
 */ function checkExports(swcAST) {
    if (Array.isArray(swcAST === null || swcAST === void 0 ? void 0 : swcAST.body)) {
        try {
            for (const node of swcAST.body){
                var ref3, ref1;
                if (node.type === 'ExportDeclaration' && ((ref3 = node.declaration) === null || ref3 === void 0 ? void 0 : ref3.type) === 'FunctionDeclaration' && [
                    'getStaticProps',
                    'getServerSideProps'
                ].includes((ref1 = node.declaration.identifier) === null || ref1 === void 0 ? void 0 : ref1.value)) {
                    return {
                        ssg: node.declaration.identifier.value === 'getStaticProps',
                        ssr: node.declaration.identifier.value === 'getServerSideProps'
                    };
                }
                if (node.type === 'ExportNamedDeclaration') {
                    const values = node.specifiers.map((specifier)=>{
                        var ref, ref2;
                        return specifier.type === 'ExportSpecifier' && ((ref = specifier.orig) === null || ref === void 0 ? void 0 : ref.type) === 'Identifier' && ((ref2 = specifier.orig) === null || ref2 === void 0 ? void 0 : ref2.value);
                    });
                    return {
                        ssg: values.some((value)=>[
                                'getStaticProps'
                            ].includes(value)
                        ),
                        ssr: values.some((value)=>[
                                'getServerSideProps'
                            ].includes(value)
                        )
                    };
                }
            }
        } catch (err) {}
    }
    return {
        ssg: false,
        ssr: false
    };
}
async function tryToReadFile(filePath, shouldThrow) {
    try {
        return await _fs.promises.readFile(filePath, {
            encoding: 'utf8'
        });
    } catch (error) {
        if (shouldThrow) {
            throw error;
        }
    }
}
function getMiddlewareConfig(config) {
    const result = {};
    if (config.matcher) {
        result.pathMatcher = new RegExp(getMiddlewareRegExpStrings(config.matcher).join('|'));
        if (result.pathMatcher.source.length > 4096) {
            throw new Error(`generated matcher config must be less than 4096 characters.`);
        }
    }
    return result;
}
function getMiddlewareRegExpStrings(matcherOrMatchers) {
    if (Array.isArray(matcherOrMatchers)) {
        return matcherOrMatchers.flatMap((x)=>getMiddlewareRegExpStrings(x)
        );
    }
    if (typeof matcherOrMatchers !== 'string') {
        throw new Error('`matcher` must be a path matcher or an array of path matchers');
    }
    let matcher = matcherOrMatchers;
    if (!matcher.startsWith('/')) {
        throw new Error('`matcher`: path matcher must start with /');
    }
    const parsedPage = (0, _tryToParsePath).tryToParsePath(matcher);
    if (parsedPage.error) {
        throw new Error(`Invalid path matcher: ${matcher}`);
    }
    const dataMatcher = `/_next/data/:__nextjsBuildId__${matcher}.json`;
    const parsedDataRoute = (0, _tryToParsePath).tryToParsePath(dataMatcher);
    if (parsedDataRoute.error) {
        throw new Error(`Invalid data path matcher: ${dataMatcher}`);
    }
    const regexes = [
        parsedPage.regexStr,
        parsedDataRoute.regexStr
    ].filter((x)=>!!x
    );
    if (regexes.length < 2) {
        throw new Error("Can't parse matcher");
    } else {
        return regexes;
    }
}

//# sourceMappingURL=get-page-static-info.js.map