// engine/schema-parity-auditor/type-checker.ts — Checks TypeScript types for optional mark
import { readFile } from 'node:fs/promises';
import { glob } from 'glob';
/**
 * Checks whether a given fieldName has an optional mark ("?") in any TypeScript type
 * file under projectPath/src/types/.
 *
 * Returns the first file where the field was found (with or without "?").
 */
export async function checkTypeOptional(fieldName, projectPath) {
    const pattern = `${projectPath}/src/types/**/*.ts`;
    const files = await glob(pattern);
    for (const file of files) {
        const result = await checkFieldInFile(fieldName, file);
        if (result.found) {
            return result;
        }
    }
    // Field not found in any type file — treat as no discrepancy (can't determine)
    return { found: false, hasOptionalMark: false, typeFile: '' };
}
async function checkFieldInFile(fieldName, filePath) {
    const content = await readFile(filePath, 'utf-8');
    return checkFieldInContent(fieldName, filePath, content);
}
/**
 * Checks a file's content for the given fieldName.
 * Exported for unit testing.
 */
export function checkFieldInContent(fieldName, filePath, content) {
    // Match field with optional mark: "fieldName?:"
    const optionalRe = new RegExp(`\\b${escapeRegex(fieldName)}\\?:`);
    // Match field without optional mark: "fieldName:" (but NOT "fieldName?:")
    const requiredRe = new RegExp(`\\b${escapeRegex(fieldName)}(?<!\\?):`);
    if (optionalRe.test(content)) {
        return { found: true, hasOptionalMark: true, typeFile: filePath };
    }
    if (requiredRe.test(content)) {
        return { found: true, hasOptionalMark: false, typeFile: filePath };
    }
    return { found: false, hasOptionalMark: false, typeFile: '' };
}
function escapeRegex(str) {
    return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
//# sourceMappingURL=type-checker.js.map