"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.openEmailStepSchema = exports.databaseQueryStepSchema = exports.sendHttpRequestStepSchema = void 0;
const zod_1 = require("zod");
const common_1 = require("../common");
const find_descriptors_1 = require("../find-descriptors");
const shared_1 = require("./shared");
const condition_descriptor_1 = require("../condition-descriptor");
const httpMethodEnum = zod_1.z.enum([
    'GET',
    'DELETE',
    'HEAD',
    'OPTIONS',
    'POST',
    'PUT',
    'PATCH',
    'LINK',
    'UNLINK',
]);
const httpHeaderSchema = zod_1.z.object({
    name: zod_1.z.string(),
    value: zod_1.z.string(),
});
const httpAuthBasicSchema = zod_1.z.object({
    type: zod_1.z.literal('basic'),
    properties: zod_1.z.object({
        username: zod_1.z.string().optional(),
        password: zod_1.z.string().optional(),
    }),
});
const httpAuthBearerSchema = zod_1.z.object({
    type: zod_1.z.literal('bearer'),
    properties: zod_1.z.object({
        token: zod_1.z.string(),
    }),
});
const httpAuthorizationSchema = zod_1.z.discriminatedUnion('type', [
    httpAuthBasicSchema,
    httpAuthBearerSchema,
]);
const httpRequestSchema = zod_1.z.object({
    url: zod_1.z.string(),
    method: httpMethodEnum,
    authorization: httpAuthorizationSchema.optional(),
    headers: zod_1.z.array(httpHeaderSchema).optional(),
    body: zod_1.z.string().optional(),
});
const sendHttpRequestVariableSourceEnum = zod_1.z.enum([
    'body',
    'header',
    'json',
    'response',
    'status',
]);
const sendHttpRequestAssertionSchema = zod_1.z.object({
    source: sendHttpRequestVariableSourceEnum,
    target: zod_1.z.string().optional(),
    assertion: condition_descriptor_1.comparisonTypeEnum,
    value: zod_1.z.string(),
});
const sendHttpRequestVariableSchema = zod_1.z.object({
    source: sendHttpRequestVariableSourceEnum,
    target: zod_1.z.string().optional(),
    name: zod_1.z.string(),
});
const sendHttpRequestOptionsSchema = zod_1.z
    .object({
    onFailure: shared_1.onFailureEnum.optional(),
    loadResponseCookies: zod_1.z.boolean().optional(),
})
    .passthrough();
exports.sendHttpRequestStepSchema = common_1.baseStepDescriptorSchema
    .extend({
    request: httpRequestSchema.describe('The HTTP request to send'),
    assertions: zod_1.z
        .array(sendHttpRequestAssertionSchema)
        .optional()
        .describe('Assertions to validate on the response'),
    variables: zod_1.z
        .array(sendHttpRequestVariableSchema)
        .optional()
        .describe('Variables to extract from the response'),
    options: sendHttpRequestOptionsSchema.optional(),
})
    .passthrough()
    .describe('Send an HTTP request and optionally assert on the response');
const databaseQueryVariableSourceEnum = zod_1.z.enum(['metadata', 'result']);
const databaseQueryAssertionSchema = zod_1.z.object({
    source: databaseQueryVariableSourceEnum,
    target: zod_1.z.string().optional(),
    assertion: condition_descriptor_1.comparisonTypeEnum,
    value: zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()]),
});
const databaseQueryVariableSchema = zod_1.z.object({
    source: databaseQueryVariableSourceEnum,
    target: zod_1.z.string().optional(),
    name: zod_1.z.string(),
});
exports.databaseQueryStepSchema = common_1.baseStepDescriptorSchema
    .extend({
    snippet: shared_1.snippetDescriptorSchema.describe('The JavaScript snippet that runs the query'),
    assertions: zod_1.z.array(databaseQueryAssertionSchema).optional(),
    variables: zod_1.z.array(databaseQueryVariableSchema).optional(),
    options: zod_1.z
        .object({
        onFailure: shared_1.onFailureEnum.optional(),
    })
        .passthrough()
        .optional(),
})
    .passthrough()
    .describe('Execute a database query via a JavaScript snippet');
const emailSelectorPreferenceSchema = zod_1.z.object({
    comparator: zod_1.z.enum(['equals', 'contains']),
    value: zod_1.z.string().optional(),
});
const emailSelectorConfigurationSchema = zod_1.z
    .object({
    emailPreferences: zod_1.z
        .record(zod_1.z.string(), zod_1.z.array(emailSelectorPreferenceSchema))
        .optional(),
    externalEmailPreferences: zod_1.z
        .object({
        lookbackTimeMs: zod_1.z.number().optional(),
    })
        .optional(),
})
    .passthrough();
exports.openEmailStepSchema = common_1.baseStepDescriptorSchema
    .extend({
    find: find_descriptors_1.findEmailDescriptorSchema,
    tab: zod_1.z.string().describe('Tab identifier for the opened email'),
    configuration: emailSelectorConfigurationSchema.optional(),
})
    .passthrough()
    .describe('Open a received email in a new browser tab');
