File size: 4,723 Bytes
f56a29b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Stage API - Scene Management
 *
 * Factory function that creates the scene namespace of the Stage API.
 */

import type { Scene, SceneContent } from '@/lib/types/stage';
import type { StageStore, APIResult, CreateSceneParams } from './stage-api-types';
import { generateId, validateSceneId, getScene, createDefaultContent } from './stage-api-defaults';

/**
 * Create the scene management API
 *
 * @param store - Zustand store instance
 * @returns Scene namespace API
 */
export function createSceneAPI(store: StageStore) {
  return {
    /**
     * Create a new scene
     *
     * @param params - Scene parameters
     * @returns Scene ID
     *
     * @example
     * const sceneId = api.scene.create({
     *   type: 'slide',
     *   title: 'Introduction',
     *   // speech is now in actions
     * });
     */
    create(params: CreateSceneParams): APIResult<string> {
      try {
        const state = store.getState();

        if (!state.stage) {
          return {
            success: false,
            error: 'No stage set - cannot create scene without a stage',
          };
        }

        const sceneId = generateId('scene');

        // Determine order
        const order = params.order ?? state.scenes.length;

        // Create default content or use the provided content
        let content: SceneContent;
        if (params.content) {
          content = {
            ...createDefaultContent(params.type),
            ...params.content,
          } as SceneContent;
        } else {
          content = createDefaultContent(params.type);
        }

        const newScene: Scene = {
          id: sceneId,
          stageId: state.stage.id,
          type: params.type,
          title: params.title,
          order,
          content,
          actions: params.actions,
          createdAt: Date.now(),
          updatedAt: Date.now(),
        };

        const newScenes = [...state.scenes, newScene].sort((a, b) => a.order - b.order);

        store.setState({ scenes: newScenes });

        return { success: true, data: sceneId };
      } catch (error) {
        return { success: false, error: String(error) };
      }
    },

    /**
     * Delete a scene
     *
     * @param sceneId - Scene ID
     * @returns Whether successful
     */
    delete(sceneId: string): APIResult<boolean> {
      try {
        const state = store.getState();

        if (!validateSceneId(state.scenes, sceneId)) {
          return { success: false, error: `Scene not found: ${sceneId}` };
        }

        const newScenes = state.scenes.filter((s) => s.id !== sceneId);

        // If the deleted scene is the current one, switch to the next
        let newCurrentSceneId = state.currentSceneId;
        if (state.currentSceneId === sceneId) {
          newCurrentSceneId = newScenes.length > 0 ? newScenes[0].id : null;
        }

        store.setState({
          scenes: newScenes,
          currentSceneId: newCurrentSceneId,
        });

        return { success: true, data: true };
      } catch (error) {
        return { success: false, error: String(error) };
      }
    },

    /**
     * Update a scene
     *
     * @param sceneId - Scene ID
     * @param updates - Fields to update
     * @returns Whether successful
     */
    update(sceneId: string, updates: Partial<Scene>): APIResult<boolean> {
      try {
        const state = store.getState();

        if (!validateSceneId(state.scenes, sceneId)) {
          return { success: false, error: `Scene not found: ${sceneId}` };
        }

        const newScenes = state.scenes.map((scene) =>
          scene.id === sceneId ? { ...scene, ...updates, updatedAt: Date.now() } : scene,
        );

        store.setState({ scenes: newScenes });

        return { success: true, data: true };
      } catch (error) {
        return { success: false, error: String(error) };
      }
    },

    /**
     * Get all scenes
     *
     * @returns Scene list
     */
    list(): APIResult<Scene[]> {
      try {
        const state = store.getState();
        return { success: true, data: [...state.scenes] };
      } catch (error) {
        return { success: false, error: String(error) };
      }
    },

    /**
     * Get a specific scene
     *
     * @param sceneId - Scene ID
     * @returns Scene object
     */
    get(sceneId: string): APIResult<Scene> {
      try {
        const state = store.getState();
        const scene = getScene(state.scenes, sceneId);

        if (!scene) {
          return { success: false, error: `Scene not found: ${sceneId}` };
        }

        return { success: true, data: scene };
      } catch (error) {
        return { success: false, error: String(error) };
      }
    },
  };
}