/**
 * Role Type Definitions
 *
 * Types for the Role system that powers agent personas with default prompts and assigned skills.
 *
 * @module types/role.types
 */
import { randomUUID } from 'crypto';
/**
 * Array of all valid role categories
 */
export const ROLE_CATEGORIES = [
    'development',
    'management',
    'quality',
    'design',
    'sales',
    'support',
    'automation',
];
// ============================================================================
// Utility Functions
// ============================================================================
/**
 * Type guard to check if a string is a valid RoleCategory
 *
 * @param value - The string to check
 * @returns True if the value is a valid RoleCategory
 *
 * @example
 * ```typescript
 * if (isValidRoleCategory(input)) {
 *   // input is typed as RoleCategory
 * }
 * ```
 */
export function isValidRoleCategory(value) {
    return ROLE_CATEGORIES.includes(value);
}
/**
 * Create a new Role with default values
 *
 * @param input - Required name and displayName, with optional overrides for other fields
 * @returns A complete Role object with generated ID and timestamps
 *
 * @example
 * ```typescript
 * const role = createDefaultRole({
 *   name: 'my-role',
 *   displayName: 'My Role',
 *   category: 'development',
 * });
 * ```
 */
export function createDefaultRole(input) {
    const now = new Date().toISOString();
    return {
        id: input.id ?? randomUUID(),
        name: input.name,
        displayName: input.displayName,
        description: input.description ?? '',
        category: input.category ?? 'development',
        systemPromptFile: input.systemPromptFile ?? '',
        assignedSkills: input.assignedSkills ?? [],
        isDefault: input.isDefault ?? false,
        isHidden: input.isHidden ?? false,
        isBuiltin: input.isBuiltin ?? false,
        createdAt: input.createdAt ?? now,
        updatedAt: input.updatedAt ?? now,
    };
}
/**
 * Convert a Role to a RoleSummary (for list views)
 *
 * Strips out detailed information like timestamps and prompt file path,
 * and replaces assignedSkills array with a count.
 *
 * @param role - The full Role object
 * @returns A RoleSummary with essential fields only
 *
 * @example
 * ```typescript
 * const roles = await roleService.getAll();
 * const summaries = roles.map(roleToSummary);
 * ```
 */
export function roleToSummary(role) {
    return {
        id: role.id,
        name: role.name,
        displayName: role.displayName,
        description: role.description,
        category: role.category,
        skillCount: role.assignedSkills.length,
        isDefault: role.isDefault,
        isHidden: role.isHidden,
        hasOverride: role.hasOverride,
        isBuiltin: role.isBuiltin,
    };
}
/**
 * Validate CreateRoleInput and return an array of error messages
 *
 * @param input - The CreateRoleInput to validate
 * @returns Array of error messages (empty if valid)
 *
 * @example
 * ```typescript
 * const errors = validateCreateRoleInput(input);
 * if (errors.length > 0) {
 *   throw new ValidationError(errors.join(', '));
 * }
 * ```
 */
export function validateCreateRoleInput(input) {
    const errors = [];
    if (!input.name || input.name.trim().length === 0) {
        errors.push('Name is required');
    }
    else if (!/^[a-z0-9-]+$/.test(input.name)) {
        errors.push('Name must contain only lowercase letters, numbers, and hyphens');
    }
    if (!input.displayName || input.displayName.trim().length === 0) {
        errors.push('Display name is required');
    }
    if (!input.description || input.description.trim().length === 0) {
        errors.push('Description is required');
    }
    if (!isValidRoleCategory(input.category)) {
        errors.push(`Invalid category: ${input.category}`);
    }
    if (!input.systemPromptContent || input.systemPromptContent.trim().length === 0) {
        errors.push('System prompt content is required');
    }
    if (input.assignedSkills && !Array.isArray(input.assignedSkills)) {
        errors.push('Assigned skills must be an array');
    }
    return errors;
}
/**
 * Validate UpdateRoleInput and return an array of error messages
 *
 * @param input - The UpdateRoleInput to validate
 * @returns Array of error messages (empty if valid)
 *
 * @example
 * ```typescript
 * const errors = validateUpdateRoleInput(input);
 * if (errors.length > 0) {
 *   throw new ValidationError(errors.join(', '));
 * }
 * ```
 */
export function validateUpdateRoleInput(input) {
    const errors = [];
    if (input.displayName !== undefined && input.displayName.trim().length === 0) {
        errors.push('Display name cannot be empty');
    }
    if (input.category !== undefined && !isValidRoleCategory(input.category)) {
        errors.push(`Invalid category: ${input.category}`);
    }
    if (input.systemPromptContent !== undefined && input.systemPromptContent.trim().length === 0) {
        errors.push('System prompt content cannot be empty');
    }
    if (input.assignedSkills !== undefined && !Array.isArray(input.assignedSkills)) {
        errors.push('Assigned skills must be an array');
    }
    return errors;
}
/**
 * Check if a Role matches the given filter criteria
 *
 * @param role - The role to check
 * @param filter - The filter criteria
 * @returns True if the role matches all filter criteria
 *
 * @example
 * ```typescript
 * const filteredRoles = roles.filter(role => matchesRoleFilter(role, filter));
 * ```
 */
export function matchesRoleFilter(role, filter) {
    if (filter.category !== undefined && role.category !== filter.category) {
        return false;
    }
    if (filter.isBuiltin !== undefined && role.isBuiltin !== filter.isBuiltin) {
        return false;
    }
    if (filter.hasSkill !== undefined && !role.assignedSkills.includes(filter.hasSkill)) {
        return false;
    }
    if (filter.search !== undefined && filter.search.trim().length > 0) {
        const searchLower = filter.search.toLowerCase();
        const matchesName = role.name.toLowerCase().includes(searchLower);
        const matchesDisplayName = role.displayName.toLowerCase().includes(searchLower);
        const matchesDescription = role.description.toLowerCase().includes(searchLower);
        if (!matchesName && !matchesDisplayName && !matchesDescription) {
            return false;
        }
    }
    return true;
}
/**
 * Convert a Role to RoleStorageFormat for persistence
 *
 * @param role - The role to convert
 * @returns A RoleStorageFormat object ready for JSON serialization
 */
export function roleToStorageFormat(role) {
    return {
        id: role.id,
        name: role.name,
        displayName: role.displayName,
        description: role.description,
        category: role.category,
        systemPromptFile: role.systemPromptFile,
        assignedSkills: role.assignedSkills,
        isDefault: role.isDefault,
        isHidden: role.isHidden,
        createdAt: role.createdAt,
        updatedAt: role.updatedAt,
    };
}
/**
 * Convert a RoleStorageFormat back to a Role
 *
 * @param stored - The stored role data
 * @param isBuiltin - Whether this role is built-in
 * @returns A complete Role object
 */
export function storageFormatToRole(stored, isBuiltin) {
    return {
        ...stored,
        isHidden: stored.isHidden ?? false,
        isBuiltin,
    };
}
//# sourceMappingURL=role.types.js.map