"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManifestDto = exports.PluginManifestMethodSchemaSchema = void 0;
const openapi = require("@nestjs/swagger");
const nestjs_zod_1 = require("nestjs-zod");
const json_schema_dto_1 = require("./json-schema.dto");
const enum_1 = require("../enum");
const zod_1 = __importDefault(require("zod"));
const pluginNameRegex = /^[a-z0-9-]+[a-z0-9]$/;
const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
exports.PluginManifestMethodSchemaSchema = json_schema_dto_1.JsonSchemaSchema.nullable()
    .optional()
    .transform((value) => (value && Object.keys(value).length === 0 ? null : value));
const PluginManifestMethodSchema = zod_1.default
    .object({
    name: zod_1.default.string().min(1).describe('Method name'),
    title: zod_1.default.string().min(1).describe('Method title'),
    description: zod_1.default.string().min(1).describe('Method description'),
    types: zod_1.default.array(enum_1.WorkflowTypeSchema).min(1).describe('Workflow type'),
    hostFunctions: zod_1.default.boolean().optional().default(false).describe('Method uses host functions'),
    allowedHosts: zod_1.default
        .array(zod_1.default.string())
        .optional()
        .default([])
        .describe('Hostnames the method can access (use * for wildcards)'),
    schema: exports.PluginManifestMethodSchemaSchema.describe('Schema'),
    uiHints: zod_1.default.array(zod_1.default.string()).optional().describe('Ui hints, for example "filter"'),
})
    .meta({ id: 'PluginManifestMethodDto' });
const PluginManifestTemplateStepSchema = zod_1.default
    .object({
    method: zod_1.default.string().min(1).describe('Step plugin method (pluginName#methodName)'),
    config: zod_1.default.record(zod_1.default.string(), zod_1.default.unknown()).nullable().optional().describe('Step configuration'),
    enabled: zod_1.default.boolean().optional().describe('Whether the step is enabled'),
})
    .meta({ id: 'PluginManifestTemplateStepDto' });
const PluginManifestTemplateSchema = zod_1.default
    .object({
    name: zod_1.default.string().min(1).describe('Template name (must be unique within the manifest)'),
    title: zod_1.default.string().min(1).describe('Template title'),
    description: zod_1.default.string().min(1).describe('Template description'),
    trigger: enum_1.WorkflowTriggerSchema.describe('Workflow trigger'),
    steps: zod_1.default.array(PluginManifestTemplateStepSchema).describe('Workflow steps'),
    uiHints: zod_1.default.array(zod_1.default.string()).optional().default([]).describe('Ui hints, for example "smart-album"'),
})
    .meta({ id: 'PluginManifestTemplateDto' });
const PluginManifestSchema = zod_1.default
    .object({
    name: zod_1.default
        .string()
        .min(1)
        .regex(pluginNameRegex, 'Plugin name must contain only lowercase letters, numbers, and hyphens, and cannot end with a hyphen')
        .describe('Plugin name (lowercase, numbers, hyphens only)'),
    version: zod_1.default.string().regex(semverRegex).describe('Plugin version (semver)'),
    title: zod_1.default.string().min(1).describe('Plugin title'),
    description: zod_1.default.string().min(1).describe('Plugin description'),
    wasmPath: zod_1.default.string().min(1).describe('WASM file path'),
    author: zod_1.default.string().min(1).describe('Plugin author'),
    methods: zod_1.default.array(PluginManifestMethodSchema).optional().default([]).describe('Plugin methods'),
    templates: zod_1.default
        .array(PluginManifestTemplateSchema)
        .optional()
        .default([])
        .refine((templates) => new Set(templates.map((t) => t.name)).size === templates.length, {
        error: 'Template names must be unique within the manifest',
    })
        .describe('Workflow templates'),
})
    .meta({ id: 'PluginManifestDto' });
class PluginManifestDto extends (0, nestjs_zod_1.createZodDto)(PluginManifestSchema) {
    static _OPENAPI_METADATA_FACTORY() {
        return {};
    }
}
exports.PluginManifestDto = PluginManifestDto;
//# sourceMappingURL=plugin-manifest.dto.js.map