"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultArchpilotIgnoreContent = exports.archpilotIgnoreFileName = void 0;
exports.createArchpilotIgnoreMatcher = createArchpilotIgnoreMatcher;
exports.loadArchpilotIgnoreMatcher = loadArchpilotIgnoreMatcher;
exports.ensureArchpilotIgnoreFileSync = ensureArchpilotIgnoreFileSync;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
exports.archpilotIgnoreFileName = '.archpilotignore';
exports.defaultArchpilotIgnoreContent = [
    'node_modules/',
    '.git/',
    '.vscode-test/',
    '.archpilot/reports/',
    '.archpilot/history/',
    '.archpilot/badges/',
    '.next/',
    'dist/',
    'build/',
    'out/',
    'coverage/',
    '',
    'test/',
    'tests/',
    '__tests__/',
    'fixtures/',
    'smoke/',
    'examples/',
    '',
].join('\n');
function normalizeRelativePath(value) {
    return value
        .replaceAll('\\', '/')
        .replace(/^\.\/+/u, '')
        .replace(/\/+/gu, '/')
        .replace(/\/+$/u, '') || '.';
}
function normalizePattern(value) {
    const trimmed = value.trim();
    if (trimmed.length === 0 || trimmed.startsWith('#')) {
        return undefined;
    }
    return trimmed.replaceAll('\\', '/').replace(/^\.\/+/u, '').replace(/\/+/gu, '/');
}
function parseArchpilotIgnore(contents) {
    return contents
        .split(/\r?\n/u)
        .map(normalizePattern)
        .filter((entry) => typeof entry === 'string' && entry.length > 0);
}
function matchesFolderPattern(relativePath, pattern) {
    const folder = normalizeRelativePath(pattern.slice(0, -1));
    if (folder === '.') {
        return false;
    }
    return (relativePath === folder ||
        relativePath.startsWith(`${folder}/`) ||
        relativePath.includes(`/${folder}/`));
}
function matchesNamePattern(relativePath, pattern) {
    const normalizedPattern = normalizeRelativePath(pattern);
    if (relativePath === normalizedPattern) {
        return true;
    }
    return relativePath.split('/').includes(normalizedPattern);
}
function createArchpilotIgnoreMatcher(patterns) {
    const ignoredPatterns = [...patterns];
    return {
        ignoredPatterns,
        isIgnored(relativePath) {
            const normalizedPath = normalizeRelativePath(relativePath);
            if (normalizedPath === '.') {
                return false;
            }
            return ignoredPatterns.some((pattern) => pattern.endsWith('/')
                ? matchesFolderPattern(normalizedPath, pattern)
                : matchesNamePattern(normalizedPath, pattern));
        },
    };
}
function loadArchpilotIgnoreMatcher(workspaceRoot) {
    const ignorePath = path.join(workspaceRoot, exports.archpilotIgnoreFileName);
    let contents;
    try {
        contents = fs.readFileSync(ignorePath, { encoding: 'utf8' });
    }
    catch {
        contents = undefined;
    }
    const defaultPatterns = parseArchpilotIgnore(exports.defaultArchpilotIgnoreContent);
    const customPatterns = contents ? parseArchpilotIgnore(contents) : [];
    return createArchpilotIgnoreMatcher([...defaultPatterns, ...customPatterns]);
}
function ensureArchpilotIgnoreFileSync(workspaceRoot) {
    const ignorePath = path.join(workspaceRoot, exports.archpilotIgnoreFileName);
    try {
        fs.accessSync(ignorePath);
        return false;
    }
    catch {
        fs.writeFileSync(ignorePath, exports.defaultArchpilotIgnoreContent, { encoding: 'utf8' });
        return true;
    }
}
//# sourceMappingURL=archpilotIgnore.js.map