import { readFile } from "node:fs/promises";
import * as path from "node:path";
import { glob } from "glob";
import { getLogger } from "./logger.js";
/**
 * Creates ignore patterns by combining default patterns with patterns from .gitignore file.
 * Searches for .gitignore file in the specified directory and parses its contents.
 *
 * @param appRootDirPath - The root directory to search for .gitignore file
 * @returns Array of ignore patterns that can be used with glob
 */
export async function createGitIgnorePatterns(appRootDirPath) {
    const defaultPatterns = [
        "node_modules/**",
        "dist/**",
        "build/**",
        ".superblocks/**",
        "**/*.tsbuildinfo",
        "**/*.tgz",
        "**/*.sbswap*",
        ".cache/**",
        "coverage/**",
    ];
    try {
        // Search for .gitignore file
        const gitignoreFiles = await glob(".gitignore", {
            cwd: appRootDirPath,
            nodir: true,
        });
        if (gitignoreFiles.length === 0) {
            getLogger().debug("No .gitignore file found, using default ignore patterns");
            return defaultPatterns;
        }
        const gitignoreFile = gitignoreFiles[0];
        if (!gitignoreFile) {
            return defaultPatterns;
        }
        const gitignorePath = path.join(appRootDirPath, gitignoreFile);
        const gitignoreContent = await readFile(gitignorePath, "utf-8");
        // Parse gitignore content into individual patterns
        const gitignorePatterns = gitignoreContent
            .split("\n")
            .map((line) => line.trim())
            .filter((line) => line && !line.startsWith("#"));
        return Array.from(new Set([...defaultPatterns, ...gitignorePatterns]));
    }
    catch (error) {
        // If .gitignore can't be read, use default patterns
        getLogger().warn("Could not read .gitignore, using default ignore patterns:", error instanceof Error ? error.message : String(error));
        return defaultPatterns;
    }
}
//# sourceMappingURL=ignore-patterns.js.map