import { z } from 'zod';
import { type CooperativeOption } from '../utils.js';
import { type AstGrepNode, type AstGrepRuleConfig } from './ast-grep.js';
import { type StructuralQueryCandidate } from './structural.js';
import type { CodebaseIndex } from './types.js';
export interface CodePatternOptions {
    language: string;
    /** Convenience form: compiled to `{ pattern }`. Exactly one of pattern/rule. */
    pattern?: string;
    /** Full ast-grep rule object (the YAML `rule:` body as JSON). */
    rule?: Record<string, unknown>;
    /** Per-metavariable sub-rules, as in ast-grep `constraints:`. */
    constraints?: Record<string, unknown>;
    /** Local named utility rules, as in ast-grep `utils:`. */
    utils?: Record<string, unknown>;
    limit?: number;
    mapTokens?: number;
    tokenizer?: string;
    countTokens?: (text: string) => number;
    /**
     * Stop after the first match whose serialized payload exceeds the remaining
     * token budget. This bounds work but can skip a smaller later match that the
     * default full scan would admit.
     */
    budgetEarlyExit?: boolean;
    signal?: AbortSignal;
    cooperative?: CooperativeOption;
    maxRuleBytes?: number;
    maxSourceBytes?: number;
    maxMatchesPerFile?: number;
    maxDiagnostics?: number;
    maxJoinCandidates?: number;
    maxJoinNodes?: number;
    /** Restrict matching to files under these repo-relative prefixes. */
    files?: readonly string[];
}
/** @internal Deterministic fault seam for exercising per-file engine degradation. */
export interface CodePatternRuntime {
    findAll?(root: AstGrepNode, config: AstGrepRuleConfig): AstGrepNode[];
}
export interface CodeRuleMetaVariable {
    text: string;
    startLine: number;
    startColumn: number;
    endLine: number;
    endColumn: number;
}
export interface CodeRuleMatch {
    relativePath: string;
    kind: string;
    text: string;
    startLine: number;
    startColumn: number;
    endLine: number;
    endColumn: number;
    metaVariables?: {
        single?: Record<string, CodeRuleMetaVariable>;
        multi?: Record<string, CodeRuleMetaVariable[]>;
    };
    nodeId?: string;
    symbol?: string;
    joinStatus?: 'resolved' | 'ambiguous' | 'unresolved';
    joinBasis?: 'exact-span' | 'line-span';
    nodeCandidates?: StructuralQueryCandidate[];
}
export interface CodePatternResult {
    status: 'resolved';
    language: string;
    grammar: string;
    engine: {
        name: string;
        version: string;
    };
    rule: Record<string, unknown>;
    scope?: {
        files?: string[];
    };
    matches: CodeRuleMatch[];
    totalMatches: number;
    omitted: number;
    truncated: boolean;
    filesParsed: number;
    filesSkipped: number;
    mapTokens: number;
    tokensUsed: number;
    tokenizer: string;
    diagnostics: string[];
    honesty: {
        freshness: 'unverified';
        stalenessReason: string;
        coverage: 'full' | 'partial';
        truncated: boolean;
        omitted: number;
        filesSkipped: number;
        unjoinedMatches: number;
        ambiguousSpanJoins: number;
        eligibleFiles?: number;
        filesQueried?: number;
        syntaxErrorFiles?: number;
        parserCoveragePartialFiles?: number;
        joinSearchTruncated?: number;
        diagnosticsOmitted?: number;
        matchScanLimitHit?: boolean;
        fileMatchLimitFiles?: number;
        /** The opt-in budget work bound stopped the scan before all matches were observed. */
        budgetEarlyExitHit?: boolean;
    };
}
/**
 * @internal Own and release every handle, including those not yet visited when
 * a callback throws or returns false to stop iteration.
 */
export declare function consumeOwnedAstGrepHandles<T extends {
    free(): void;
}>(handles: readonly T[], visit: (handle: T, index: number) => Promise<boolean | void>): Promise<void>;
export declare function queryCodePattern(index: CodebaseIndex, options: CodePatternOptions, runtime?: CodePatternRuntime): Promise<CodePatternResult>;
/** One user-defined fact predicate: an ast-grep rule with an id and a message. */
export declare const CustomCodeFactRuleSchema: z.ZodObject<{
    id: z.ZodString;
    language: z.ZodString;
    message: z.ZodOptional<z.ZodString>;
    files: z.ZodOptional<z.ZodArray<z.ZodString>>;
    rule: z.ZodRecord<z.ZodString, z.ZodUnknown>;
    constraints: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
    utils: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
}, z.core.$strict>;
export declare const CodeRulesManifestSchema: z.ZodObject<{
    rules: z.ZodArray<z.ZodObject<{
        id: z.ZodString;
        language: z.ZodString;
        message: z.ZodOptional<z.ZodString>;
        files: z.ZodOptional<z.ZodArray<z.ZodString>>;
        rule: z.ZodRecord<z.ZodString, z.ZodUnknown>;
        constraints: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
        utils: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
    }, z.core.$strict>>;
}, z.core.$strict>;
export type CustomCodeFactRule = z.infer<typeof CustomCodeFactRuleSchema>;
export type CodeRulesManifest = z.infer<typeof CodeRulesManifestSchema>;
export interface CustomCodeFactsOptions {
    limit?: number;
    mapTokens?: number;
    tokenizer?: string;
    countTokens?: (text: string) => number;
    budgetEarlyExit?: boolean;
    signal?: AbortSignal;
    cooperative?: CooperativeOption;
    maxSourceBytes?: number;
}
export interface CustomCodeFactResult {
    id: string;
    message?: string;
    result: CodePatternResult;
}
export interface CustomCodeFactsResult {
    status: 'resolved';
    engine: {
        name: string;
        version: string;
    };
    rules: CustomCodeFactResult[];
}
export declare function parseCodeRulesManifest(raw: string): CodeRulesManifest;
/**
 * Evaluate user-defined rule predicates against the index, one bounded result
 * per rule. Like `layer-violations`, the user supplies the rules; OCC refuses
 * to infer them.
 */
export declare function queryCustomCodeFacts(index: CodebaseIndex, manifest: CodeRulesManifest, options?: CustomCodeFactsOptions): Promise<CustomCodeFactsResult>;
