"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.aiPromptSchema = exports.textOrSymbolSchema = exports.snippetDescriptorSchema = exports.javaScriptDescriptorSchema = exports.onFailureEnum = exports.BROWSER_FIND_REQUIRED_STEP_TYPES = exports.stepWithBrowserFindSchema = void 0;
exports.hasBrowserFindLocator = hasBrowserFindLocator;
const zod_1 = require("zod");
const common_1 = require("../common");
const find_descriptors_1 = require("../find-descriptors");
exports.stepWithBrowserFindSchema = common_1.baseStepDescriptorSchema
    .extend({
    find: find_descriptors_1.browserFindDescriptorSchema.optional(),
    selector: find_descriptors_1.simpleElementSelectorSchema.optional(),
    selectorAncestors: zod_1.z.array(find_descriptors_1.simpleElementSelectorSchema).optional(),
})
    .passthrough();
function hasBrowserFindLocator(value) {
    if (typeof value !== 'object' || value === null) {
        return false;
    }
    const step = value;
    return step.find !== undefined || step.selector !== undefined;
}
exports.BROWSER_FIND_REQUIRED_STEP_TYPES = new Set([
    'Click',
    'Click and Hold',
    'DoubleClick',
    'RightClick',
    'Hover',
    'Release',
    'EnterText',
    'SendKeys',
    'Select',
    'SetFiles',
]);
exports.onFailureEnum = zod_1.z
    .enum(['continue', 'terminate', 'failAtEnd'])
    .describe('Behavior when a step fails: continue = pass with warning, terminate = fail immediately, failAtEnd = continue executing but mark test failed at the end');
const parameterOverrideSchema = zod_1.z.object({
    id: zod_1.z.string(),
    value: zod_1.z.string(),
    actionCode: zod_1.z.string().optional(),
});
const reusableSnippetSchema = zod_1.z.object({
    snippetId: zod_1.z.string(),
    snippetInvariantId: zod_1.z.string(),
    parameterOverrides: zod_1.z.array(parameterOverrideSchema),
    actionCode: zod_1.z.string().optional(),
});
const inlineSnippetSchema = zod_1.z.object({
    inlineSnippet: zod_1.z
        .object({
        body: zod_1.z.string().optional().describe('JavaScript source code'),
        parameters: zod_1.z
            .array(zod_1.z
            .object({
            id: zod_1.z.string(),
            name: zod_1.z.string(),
        })
            .passthrough())
            .optional()
            .describe('Declared snippet parameters'),
    })
        .passthrough()
        .describe('Inline JavaScript snippet — expected shape: { body: string, parameters: [{id, name}] }'),
    parameterOverrides: zod_1.z.array(parameterOverrideSchema),
    actionCode: zod_1.z.string().optional(),
});
const legacyReusableSnippetSchema = zod_1.z.object({
    snippetInvariantId: zod_1.z.string(),
    actionCode: zod_1.z.string().optional(),
});
const legacyInlineJavaScriptSchema = zod_1.z.object({
    encodedJS: zod_1.z.string(),
    actionCode: zod_1.z.string().optional(),
});
exports.javaScriptDescriptorSchema = zod_1.z
    .union([
    reusableSnippetSchema,
    inlineSnippetSchema,
    legacyReusableSnippetSchema,
    legacyInlineJavaScriptSchema,
])
    .describe('JavaScript code reference — inline snippet, reusable snippet, or legacy encoded JS');
exports.snippetDescriptorSchema = zod_1.z.union([
    reusableSnippetSchema,
    inlineSnippetSchema,
]);
exports.textOrSymbolSchema = zod_1.z.union([zod_1.z.string(), common_1.mablSymbolObjectSchema]);
exports.aiPromptSchema = zod_1.z
    .object({
    userPrompt: zod_1.z
        .string()
        .describe('The natural language prompt for the AI to evaluate'),
    metaPrompt: zod_1.z.string().optional(),
    criteria: zod_1.z.array(zod_1.z.string()).optional(),
})
    .describe('AI evaluation prompt with optional criteria');
