"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MABL_AUTHORING_TOOLS = exports.authoringGuideResourceSchema = exports.getMablAuthoringGuideArgsSchema = exports.getMablExampleArgsSchema = exports.listMablExamplesArgsSchema = exports.getMablSchemaResourceArgsSchema = exports.validateMablTestArgsSchema = void 0;
exports.normalizeStepsArg = normalizeStepsArg;
const zod_1 = require("zod");
const testTypeSchema = zod_1.z.enum(['browser', 'mobile']);
const validationErrorSchema = zod_1.z
    .object({
    path: zod_1.z.array(zod_1.z.union([zod_1.z.string(), zod_1.z.number()])),
    message: zod_1.z.string(),
    code: zod_1.z.string(),
    suggestion: zod_1.z.string().optional(),
    example: zod_1.z.string().optional(),
})
    .describe('A single validation error with an optional educational suggestion.');
const validationWarningSchema = zod_1.z.object({
    path: zod_1.z.array(zod_1.z.union([zod_1.z.string(), zod_1.z.number()])),
    message: zod_1.z.string(),
    code: zod_1.z.string(),
});
const validationResultSchema = zod_1.z.object({
    valid: zod_1.z.boolean(),
    errors: zod_1.z.array(validationErrorSchema),
    warnings: zod_1.z.array(validationWarningSchema),
});
const exampleSummarySchema = zod_1.z.object({
    id: zod_1.z.string(),
    title: zod_1.z.string(),
    tags: zod_1.z.array(zod_1.z.string()),
    appliesTo: zod_1.z.array(testTypeSchema),
});
const exampleSchema = exampleSummarySchema.extend({
    snippet: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()),
    validatedAgainst: zod_1.z.string(),
    seeAlso: zod_1.z.array(zod_1.z.string()),
});
const jsonSchemaSchema = zod_1.z
    .record(zod_1.z.string(), zod_1.z.unknown())
    .describe('Open-ended JSON Schema object. The shape depends on the resource.');
function normalizeStepsArg(value) {
    if (Array.isArray(value))
        return value;
    if (typeof value === 'string') {
        let parsed;
        try {
            parsed = JSON.parse(value);
        }
        catch (error) {
            throw new Error(`steps was a string but did not parse as JSON: ${error instanceof Error ? error.message : 'unknown'}`);
        }
        if (!Array.isArray(parsed)) {
            throw new Error('steps was a string that JSON-parsed to a non-array; expected an array of steps');
        }
        return parsed;
    }
    throw new Error(`steps must be an array (or a JSON-encoded string of an array); got ${typeof value}`);
}
exports.validateMablTestArgsSchema = zod_1.z.object({
    steps: zod_1.z
        .array(zod_1.z.unknown())
        .describe('The test steps to validate, e.g. [{ "Click": { "find": {...} } }]. Each step is a singleton map. May also be passed as a JSON-encoded string of the array.'),
    options: zod_1.z
        .object({
        testType: testTypeSchema
            .optional()
            .describe('Pin the validator to browser or mobile semantics.'),
        strict: zod_1.z
            .boolean()
            .optional()
            .describe('Treat warnings as errors when true.'),
    })
        .optional(),
});
exports.getMablSchemaResourceArgsSchema = zod_1.z.object({
    uri: zod_1.z
        .string()
        .describe('A mabl-schema:// URI. Three layers: mabl-schema://envelope, mabl-schema://step, mabl-schema://step/<TypeName>.'),
    testType: testTypeSchema
        .optional()
        .describe('Pin the schema universe (default browser).'),
});
exports.listMablExamplesArgsSchema = zod_1.z.object({
    search: zod_1.z
        .string()
        .optional()
        .describe('Free-text match against id/title/tags.'),
    tags: zod_1.z
        .array(zod_1.z.string())
        .optional()
        .describe('Filter to examples that include any of these tags.'),
    appliesTo: testTypeSchema
        .optional()
        .describe('Filter by test type the example applies to.'),
});
exports.getMablExampleArgsSchema = zod_1.z.object({
    id: zod_1.z
        .string()
        .describe('The example id, e.g. "click-by-text". Suffix of an examples://<id> URI.'),
});
exports.getMablAuthoringGuideArgsSchema = zod_1.z.object({
    uri: zod_1.z
        .string()
        .describe('A mabl-author:// URI. Four chunks: mabl-author://overview (entry point), mabl-author://authoring-patterns, mabl-author://selector-fields, mabl-author://save-tool-mechanics.'),
});
exports.authoringGuideResourceSchema = zod_1.z.object({
    uri: zod_1.z.string(),
    description: zod_1.z.string(),
    content: zod_1.z.string().describe('Markdown body of the authoring-guide chunk.'),
});
exports.MABL_AUTHORING_TOOLS = {
    mablValidate: {
        name: 'mabl_validate',
        title: 'Validate a mabl test',
        description: 'Validate a mabl json_steps payload. Returns valid:true or a list of errors. Errors include a `suggestion` (plain-English next step) and an `example` (an examples://<id> URI you can fetch via mabl_get_example).',
        argsSchema: exports.validateMablTestArgsSchema,
        responseSchema: validationResultSchema,
    },
    mablGetSchemaResource: {
        name: 'mabl_get_schema_resource',
        title: 'Fetch a mabl schema resource',
        description: 'Fetch a layered schema resource. Start with mabl-schema://envelope, then mabl-schema://step for the registry of step types, then mabl-schema://step/<TypeName> for one step at a time.',
        argsSchema: exports.getMablSchemaResourceArgsSchema,
        responseSchema: jsonSchemaSchema,
    },
    mablListExamples: {
        name: 'mabl_list_examples',
        title: 'List canonical mabl example snippets',
        description: 'List canonical example snippets covering common patterns (click-by-text, enter-text-into-input, if-element-present, assert-viewport-ai). Optional filters by tag/test-type/search.',
        argsSchema: exports.listMablExamplesArgsSchema,
        responseSchema: zod_1.z.array(exampleSummarySchema),
    },
    mablGetExample: {
        name: 'mabl_get_example',
        title: 'Fetch a mabl example snippet',
        description: 'Fetch the full payload for a canonical example by id. The snippet is a real, validated steps fragment ready to adapt.',
        argsSchema: exports.getMablExampleArgsSchema,
        responseSchema: exampleSchema,
    },
    mablGetAuthoringGuide: {
        name: 'mabl_get_authoring_guide',
        title: 'Fetch a hand-authoring guide chunk',
        description: 'Fetch reference material for hand-authoring a mabl test directly via mabl_save_flow_steps / mabl_save_test_steps. Four chunks: mabl-author://overview (entry point — when to hand-author, findTarget shape decision, variable conventions, validate -> save -> run loop, failure triage), mabl-author://authoring-patterns (patterns for structuring a test well, starting with step-group + intent grouping), mabl-author://selector-fields (locator / description / uuid field details), mabl-author://save-tool-mechanics (which save tool to use, scaffolding shortcut, save-error codes). Start with the overview URI; it links the others.',
        argsSchema: exports.getMablAuthoringGuideArgsSchema,
        responseSchema: exports.authoringGuideResourceSchema,
    },
};
