"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolGrepFieldsOutputSchema = exports.grepFieldsResultSchema = exports.grepFieldsPatternStatsSchema = exports.grepFieldsInputSchema = exports.GREP_FIELDS_DESCRIPTION = void 0;
const zod_1 = require("zod");
const outputMetadata_1 = require("../outputMetadata");
const discoveryToolNames_1 = require("./discoveryToolNames");
const toolFindExploresArgs_1 = require("./toolFindExploresArgs");
const GREP_FIELDS_DESCRIPTION = ({ runtime, }) => {
    const grepFields = (0, discoveryToolNames_1.toolNameFor)('grepFields', runtime);
    const findContent = (0, discoveryToolNames_1.toolNameFor)('findContent', runtime);
    return `Tool: ${grepFields}

Purpose:
Find which explores and fields can answer a question by grepping an in-memory, annotated view of the project's fields (names, labels, descriptions, hints and tags). Returns matching fields as \`explore/fieldId  [kind type]\` lines grouped by explore.

Use this as the FIRST step whenever the user asks a data question (counts, totals, breakdowns, trends, "what is", "show me", "how many"). Do NOT call this for questions about existing dashboards/charts (use ${findContent}).

Each pattern is a case-insensitive keyword pattern (not a full regex): use \`|\` to OR synonyms ("revenue|sales") and a space or \`.*\` between words to require all of them ("order.*status" matches fields mentioning both "order" and "status"). Pass 1–5 patterns in a SINGLE call covering the different angles of the question — they run together so you see all results at once instead of grepping one at a time. Start broad with meaningful keywords (e.g. ["revenue|sales", "country|region", "segment|tier"]) and narrow from there — long natural-language phrases will not match. If results are empty, try synonyms or broader patterns before giving up. Read the returned fieldIds and pick the single explore that answers at the right grain before building a query.
`;
};
exports.GREP_FIELDS_DESCRIPTION = GREP_FIELDS_DESCRIPTION;
exports.grepFieldsInputSchema = zod_1.z.object({
    patterns: zod_1.z
        .array(zod_1.z.string().min(1))
        .min(1)
        .max(5)
        .describe('Up to 5 case-insensitive keyword patterns, run together in one call. Use `|` to OR synonyms and a space or `.*` between words to require all of them. Each is matched against field names, labels, descriptions, hints and tags.'),
    exploreName: zod_1.z
        .string()
        .nullable()
        .describe('Restrict the search to this explore only, or null to search all explores.'),
});
// Per-pattern match stats, persisted with the tool result so grep quality is
// observable in production. `matchedAllFields` is the fingerprint of a
// too-broad or broken grep (the pattern discriminated nothing in its scope).
exports.grepFieldsPatternStatsSchema = zod_1.z.array(zod_1.z.object({
    pattern: zod_1.z.string(),
    matchCount: zod_1.z.number(),
    scopeSize: zod_1.z.number(),
    matchedAllFields: zod_1.z.boolean(),
}));
const grepFieldsPatternFieldSchema = zod_1.z.object({
    exploreName: zod_1.z.string(),
    exploreLabel: zod_1.z.string(),
    fieldId: zod_1.z.string(),
    path: zod_1.z.string(),
    kind: zod_1.z.enum(['dimension', 'metric']),
    fieldType: zod_1.z.string(),
    label: zod_1.z.string(),
    description: zod_1.z.string().nullable(),
    hint: zod_1.z.string().nullable(),
    usageInVerifiedCharts: zod_1.z.number(),
    matchLocality: zod_1.z.enum(['name', 'description', 'hint', 'mixed']),
});
const grepFieldsPatternExploreSchema = zod_1.z.object({
    exploreName: zod_1.z.string(),
    exploreLabel: zod_1.z.string(),
    requiredFilters: zod_1.z.array(toolFindExploresArgs_1.findExploresRequiredFilterSchema),
    fields: zod_1.z.array(grepFieldsPatternFieldSchema),
});
const grepFieldsExploreNameMatchSchema = zod_1.z.object({
    exploreName: zod_1.z.string(),
    exploreLabel: zod_1.z.string(),
});
const grepFieldsPatternResultSchema = zod_1.z.object({
    pattern: zod_1.z.string(),
    status: zod_1.z.enum(['matches', 'no_matches', 'no_signal']),
    matchCount: zod_1.z.number(),
    scopeSize: zod_1.z.number(),
    matchedAllFields: zod_1.z.boolean(),
    note: zod_1.z.string(),
    resultsByExplore: zod_1.z.array(grepFieldsPatternExploreSchema),
    metricAmbiguityNote: zod_1.z.string().nullable(),
    matchingExploresByName: zod_1.z.array(grepFieldsExploreNameMatchSchema),
});
const grepFieldsFuzzyMatchSchema = zod_1.z.object({
    exploreName: zod_1.z.string(),
    fieldId: zod_1.z.string(),
    label: zod_1.z.string(),
    fieldType: zod_1.z.string(),
    description: zod_1.z.string().nullable(),
    searchRank: zod_1.z.number().nullable(),
    usageInCharts: zod_1.z.number(),
    usageInVerifiedCharts: zod_1.z.number(),
});
exports.grepFieldsResultSchema = zod_1.z.object({
    description: zod_1.z.string(),
    exploreName: zod_1.z.string().nullable(),
    patterns: zod_1.z.array(grepFieldsPatternResultSchema),
    fuzzyMatches: zod_1.z.array(grepFieldsFuzzyMatchSchema),
    relevantVerifiedAnswers: zod_1.z
        .array(toolFindExploresArgs_1.findExploresRelevantVerifiedAnswerSchema)
        .optional(),
});
exports.toolGrepFieldsOutputSchema = zod_1.z.object({
    result: zod_1.z.string(),
    metadata: outputMetadata_1.baseOutputMetadataSchema.extend({
        patternStats: exports.grepFieldsPatternStatsSchema.optional(),
    }),
});
//# sourceMappingURL=toolGrepFieldsArgs.js.map