var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
    if (typeof path === "string" && /^\.\.?\//.test(path)) {
        return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
            return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
        });
    }
    return path;
};
import * as fs from "node:fs/promises";
import path from "pathe";
import pc from "picocolors";
import { getPackageManagerRunner } from "../util/detect-package-manager.js";
import { exists } from "../util/exists.js";
import ignore from "../util/ignore-matcher.js";
import { logger } from "../util/logger.js";
import { Website } from "./website.js";
export async function Nextjs(id, props = {}) {
    const runner = await getPackageManagerRunner();
    const isIgnored = await isFileIgnored("wrangler.jsonc", props.cwd);
    if (!isIgnored) {
        logger.warn([
            pc.bold(`The ${pc.cyan("wrangler.jsonc")} file for Next.js ${pc.cyan(`"${id}"`)} is not ignored.`),
            "This may cause unexpected behavior. Please add the file to your .gitignore.",
        ].join(" "));
    }
    const dev = normalizeCommand(props.dev, {
        command: `${runner} next dev`,
        env: {
            NEXTJS_ENV: "development",
        },
    });
    let domain = typeof props.dev === "object" ? props.dev.domain : undefined;
    if (!domain) {
        let port;
        const args = dev.command.split(" ");
        const findArg = (arg) => {
            if (args.find((arg) => arg.startsWith(`${arg}=`))) {
                return args.find((arg) => arg.startsWith(`${arg}=`))?.split("=")[1];
            }
            else if (args.includes(arg)) {
                const index = args.indexOf(arg);
                return args[index + 1];
            }
            return undefined;
        };
        if ((port = findArg("--port"))) {
            domain = `localhost:${port}`;
        }
        else if ((port = findArg("-p"))) {
            domain = `localhost:${port}`;
        }
        else {
            try {
                const config = await import(__rewriteRelativeImportExtension(path.resolve(props.cwd ?? process.cwd(), "next.config.ts")));
                port = config.default?.devServer?.port ?? 3000;
            }
            catch { }
        }
        if (port) {
            domain = `localhost:${port}`;
        }
    }
    return await Website(id, {
        bundle: {
            minify: true,
        },
        ...props,
        entrypoint: props.entrypoint ?? ".open-next/worker.js",
        wrangler: {
            path: "wrangler.jsonc",
            secrets: isIgnored,
            ...(props.wrangler ?? {}),
        },
        build: normalizeCommand(props.build, {
            command: `${runner} opennextjs-cloudflare build`,
            env: {
                NEXTJS_ENV: "production",
            },
        }),
        dev: {
            ...dev,
            domain,
        },
        // OpenNext generates the files, but relies on us to bundle them.
        noBundle: props.noBundle ?? false,
        spa: false,
        compatibilityFlags: [
            "nodejs_compat",
            "global_fetch_strictly_public",
            ...(props.compatibilityFlags ?? []),
        ],
        assets: props.assets ?? ".open-next/assets",
    });
}
/**
 * Check if a file is ignored by .gitignore.
 *
 * @param filename - The name of the file to check.
 * @param cwd - The current working directory.
 * @returns True if the file is ignored, false otherwise.
 */
const isFileIgnored = async (filename, cwd) => {
    const matcher = ignore();
    // Read .gitignore from current directory and walk up the directory tree
    let currentDir = path.resolve(cwd ?? process.cwd());
    const gitignorePatterns = [];
    while (currentDir !== path.dirname(currentDir)) {
        const gitignorePath = path.join(currentDir, ".gitignore");
        try {
            const gitignoreContent = await fs.readFile(gitignorePath, "utf8");
            // unshift to prioritize local .gitignore
            gitignorePatterns.unshift(...gitignoreContent
                .split("\n")
                .filter((line) => line.trim() && !line.startsWith("#")));
        }
        catch {
            // .gitignore doesn't exist in this directory, continue up
        }
        if (await exists(path.join(currentDir, ".git"))) {
            break;
        }
        currentDir = path.dirname(currentDir);
    }
    matcher.add(gitignorePatterns);
    return matcher.ignores(filename);
};
const normalizeCommand = (input, defaults) => {
    return {
        command: typeof input === "string" ? input : (input?.command ?? defaults.command),
        env: {
            ...defaults.env,
            ...(typeof input === "object" ? (input?.env ?? {}) : {}),
        },
        memoize: typeof input === "object" ? input?.memoize : undefined,
    };
};
//# sourceMappingURL=next.js.map