File size: 3,117 Bytes
59bd45e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * Data transformation utilities
 * Convert backend API responses to frontend types
 */

import {
  RecordItem,
  RecordSource,
  MoodItem,
  MoodType,
  InspirationItem,
  TodoItem,
  TodoCategory
} from '../types';

/**
 * Convert backend record to frontend RecordItem
 */
export function transformRecord(record: any): RecordItem {
  return {
    id: record.record_id,
    content: record.original_text,
    createdAt: new Date(record.timestamp).getTime(),
    sourceType: record.input_type === 'audio' ? RecordSource.VOICE : RecordSource.MANUAL
  };
}

/**
 * Convert backend mood type to frontend MoodType
 */
export function transformMoodType(type?: string): MoodType {
  if (!type) return MoodType.CALM;
  
  const typeMap: Record<string, MoodType> = {
    '开心': MoodType.HAPPY,
    '快乐': MoodType.HAPPY,
    '平静': MoodType.CALM,
    '冷静': MoodType.CALM,
    '疲惫': MoodType.TIRED,
    '累': MoodType.TIRED,
    '焦虑': MoodType.ANXIOUS,
    '紧张': MoodType.ANXIOUS,
    '希望': MoodType.HOPEFUL,
    '期待': MoodType.HOPEFUL,
  };

  return typeMap[type] || MoodType.CALM;
}

/**
 * Convert backend mood to frontend MoodItem
 */
export function transformMood(mood: any, index: number): MoodItem {
  // Generate pseudo-random position based on timestamp
  const timestamp = new Date(mood.timestamp).getTime();
  const x = 20 + ((timestamp + index * 13) % 60);
  const y = 20 + ((timestamp + index * 17) % 60);
  
  return {
    id: mood.record_id,
    type: transformMoodType(mood.type),
    date: new Date(mood.timestamp).getTime(),
    intensity: mood.intensity ? mood.intensity / 10 : 0.5,
    x,
    y
  };
}

/**
 * Convert backend inspiration to frontend InspirationItem
 */
export function transformInspiration(inspiration: any): InspirationItem {
  return {
    id: inspiration.record_id,
    content: inspiration.core_idea,
    createdAt: new Date(inspiration.timestamp).getTime(),
    tags: inspiration.tags || []
  };
}

/**
 * Convert backend category to frontend TodoCategory
 */
export function transformTodoCategory(category?: string): TodoCategory {
  const categoryMap: Record<string, TodoCategory> = {
    '工作': 'work',
    '生活': 'life',
    '学习': 'study',
    '健康': 'health'
  };

  return (categoryMap[category || ''] || 'life') as TodoCategory;
}

/**
 * Convert backend todo to frontend TodoItem
 */
export function transformTodo(todo: any): TodoItem {
  const createdAt = new Date(todo.timestamp).getTime();
  
  // Try to parse time if available
  let scheduledAt: number | undefined;
  if (todo.time) {
    // Simple heuristic: if time contains "明天", add 1 day
    if (todo.time.includes('明天')) {
      scheduledAt = createdAt + 24 * 60 * 60 * 1000;
    } else if (todo.time.includes('今天')) {
      scheduledAt = createdAt;
    }
  }

  return {
    id: todo.record_id,
    title: todo.task,
    createdAt,
    scheduledAt,
    isDone: todo.status === 'completed' || todo.status === 'done',
    category: transformTodoCategory(todo.category),
    location: todo.location || undefined,
    time: todo.time || undefined
  };
}