/**
 * Credential template management
 */
import { query } from './client';
import { z } from 'zod';
export const CredentialTemplateSchema = z.object({
    id: z.string().uuid(),
    name: z.string(),
    description: z.string().optional(),
    credential_type: z.array(z.string()),
    template_data: z.record(z.unknown()),
    version: z.number().int().positive(),
    is_active: z.boolean(),
    created_by: z.string().uuid().nullable(),
    created_at: z.date(),
    updated_at: z.date(),
});
/**
 * Create a credential template
 */
export async function createCredentialTemplate(template) {
    const result = await query(`INSERT INTO credential_templates 
     (name, description, credential_type, template_data, version, is_active, created_by)
     VALUES ($1, $2, $3, $4, $5, $6, $7)
     RETURNING *`, [
        template.name,
        template.description || null,
        template.credential_type,
        JSON.stringify(template.template_data),
        template.version,
        template.is_active,
        template.created_by || null,
    ]);
    return result.rows[0];
}
/**
 * Get credential template by ID
 */
export async function getCredentialTemplate(id) {
    const result = await query(`SELECT * FROM credential_templates WHERE id = $1`, [id]);
    return result.rows[0] || null;
}
/**
 * Get credential template by name and version
 */
export async function getCredentialTemplateByName(name, version) {
    if (version) {
        const result = await query(`SELECT * FROM credential_templates WHERE name = $1 AND version = $2`, [name, version]);
        return result.rows[0] || null;
    }
    else {
        // Get latest active version
        const result = await query(`SELECT * FROM credential_templates 
       WHERE name = $1 AND is_active = TRUE 
       ORDER BY version DESC 
       LIMIT 1`, [name]);
        return result.rows[0] || null;
    }
}
/**
 * List all credential templates
 */
export async function listCredentialTemplates(activeOnly = true, limit = 100, offset = 0) {
    const whereClause = activeOnly ? 'WHERE is_active = TRUE' : '';
    const result = await query(`SELECT * FROM credential_templates 
     ${whereClause}
     ORDER BY name, version DESC 
     LIMIT $1 OFFSET $2`, [limit, offset]);
    return result.rows;
}
/**
 * Update credential template
 */
export async function updateCredentialTemplate(id, updates) {
    const fields = [];
    const values = [];
    let paramIndex = 1;
    if (updates.description !== undefined) {
        fields.push(`description = $${paramIndex++}`);
        values.push(updates.description);
    }
    if (updates.template_data !== undefined) {
        fields.push(`template_data = $${paramIndex++}`);
        values.push(JSON.stringify(updates.template_data));
    }
    if (updates.is_active !== undefined) {
        fields.push(`is_active = $${paramIndex++}`);
        values.push(updates.is_active);
    }
    if (fields.length === 0) {
        return getCredentialTemplate(id);
    }
    fields.push(`updated_at = NOW()`);
    values.push(id);
    const result = await query(`UPDATE credential_templates 
     SET ${fields.join(', ')}
     WHERE id = $${paramIndex}
     RETURNING *`, values);
    return result.rows[0] || null;
}
/**
 * Create new version of credential template
 */
export async function createTemplateVersion(templateId, updates) {
    const original = await getCredentialTemplate(templateId);
    if (!original) {
        throw new Error(`Template ${templateId} not found`);
    }
    // Get next version number
    const versionResult = await query(`SELECT MAX(version) as max_version FROM credential_templates WHERE name = $1`, [original.name]);
    const nextVersion = (versionResult.rows[0]?.max_version || 0) + 1;
    return createCredentialTemplate({
        name: original.name,
        description: updates.description || original.description,
        credential_type: original.credential_type,
        template_data: updates.template_data || original.template_data,
        version: nextVersion,
        is_active: true,
        created_by: original.created_by,
    });
}
/**
 * Render credential from template with variable substitution
 */
export function renderCredentialFromTemplate(template, variables) {
    const rendered = JSON.parse(JSON.stringify(template.template_data));
    function substitute(obj) {
        if (typeof obj === 'string') {
            // Replace {{variable}} patterns
            return obj.replace(/\{\{(\w+)\}\}/g, (match, varName) => {
                return variables[varName] !== undefined ? String(variables[varName]) : match;
            });
        }
        else if (Array.isArray(obj)) {
            return obj.map(substitute);
        }
        else if (obj && typeof obj === 'object') {
            const result = {};
            for (const [key, value] of Object.entries(obj)) {
                result[key] = substitute(value);
            }
            return result;
        }
        return obj;
    }
    return substitute(rendered);
}
//# sourceMappingURL=credential-templates.js.map