import { SkillLevel, SKILL_LEVEL_VALUES, CategoryEvaluation, RadarChartData, UserEvaluation, SkillCategory, } from "./types"; import { apiClient } from "../services/api-client"; export function calculateCategoryScore( categoryEvaluation: CategoryEvaluation ): number { if (categoryEvaluation.skills.length === 0) return 0; const evaluatedSkills = categoryEvaluation.skills.filter( (skill) => skill.level !== null ); if (evaluatedSkills.length === 0) return 0; const totalScore = evaluatedSkills.reduce((sum, skill) => { return sum + SKILL_LEVEL_VALUES[skill.level!]; }, 0); return totalScore / evaluatedSkills.length; } export function generateRadarData( evaluations: CategoryEvaluation[], categories: SkillCategory[] ): RadarChartData[] { const maxScore = 3; // Expert level return categories.map((category) => { const evaluation = evaluations.find( (e) => e.category === category.category ); const score = evaluation ? calculateCategoryScore(evaluation) : 0; return { category: category.category, score: Math.round(score * 10) / 10, // Round to 1 decimal maxScore, }; }); } export async function saveUserEvaluation( evaluation: UserEvaluation ): Promise { try { await apiClient.saveUserEvaluation(evaluation); } catch (error) { console.error("Failed to save user evaluation:", error); throw error; } } export async function loadUserEvaluation( profile: UserEvaluation["profile"] ): Promise { try { return await apiClient.loadUserEvaluation(profile); } catch (error) { console.error("Failed to load user evaluation:", error); return null; } } export function createEmptyEvaluation( categories: SkillCategory[] ): CategoryEvaluation[] { return categories.map((category) => ({ category: category.category, skills: [], selectedSkillIds: [], })); }