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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// ══════════════════════════════════════════════════════════════════════════════
// Chahua Markdown Presenter - Slide Renderer
// Render parsed slides as HTML presentation
// ══════════════════════════════════════════════════════════════════════════════
// Company: Chahua Development Co., Ltd.
// Version: 2.0.0
// License: MIT
// ══════════════════════════════════════════════════════════════════════════════

export class SlideRenderer {
  constructor(container, options = {}) {
    this.container = container;
    this.options = {
      transition: options.transition || 'fade', // fade, slide, zoom
      transitionDuration: options.transitionDuration || 600,
      autoAnimate: options.autoAnimate !== false,
      ...options
    };
    
    this.slides = [];
    this.currentIndex = 0;
    this.isAnimating = false;
  }

  /**

   * Load slides into renderer

   */
  loadSlides(slides) {
    this.slides = slides;
    this.currentIndex = 0;
    this.renderAllSlides();
  }

  /**

   * Render all slides as HTML

   */
  renderAllSlides() {
    this.container.innerHTML = '';
    
    this.slides.forEach((slide, index) => {
      const slideElement = this.createSlideElement(slide, index);
      this.container.appendChild(slideElement);
    });

    // Show first slide
    if (this.slides.length > 0) {
      this.showSlide(0);
    }
  }

  /**

   * Create HTML element for a slide

   */
  createSlideElement(slide, index) {
    const slideDiv = document.createElement('div');
    slideDiv.className = 'slide';
    slideDiv.dataset.index = index;
    slideDiv.style.display = 'none';
    
    // Create slide content container
    const contentDiv = document.createElement('div');
    contentDiv.className = 'slide-content';

    // Render each content item
    slide.content.forEach((item, itemIndex) => {
      const element = this.renderContentItem(item, itemIndex);
      if (element) {
        contentDiv.appendChild(element);
      }
    });

    slideDiv.appendChild(contentDiv);
    
    // Add slide number
    const slideNumber = document.createElement('div');
    slideNumber.className = 'slide-number';
    slideNumber.textContent = `${index + 1} / ${this.slides.length}`;
    slideDiv.appendChild(slideNumber);

    return slideDiv;
  }

  /**

   * Render individual content item

   */
  renderContentItem(item, index) {
    const element = document.createElement('div');
    element.className = `content-item content-${item.type}`;
    
    if (this.options.autoAnimate) {
      element.style.opacity = '0';
      element.style.transform = 'translateY(20px)';
      element.dataset.animationDelay = index * 150;
    }

    switch (item.type) {
      case 'h1':
        element.innerHTML = `<h1>${this.parseInline(item.content)}</h1>`;
        break;

      case 'h2':
        element.innerHTML = `<h2>${this.parseInline(item.content)}</h2>`;
        break;

      case 'h3':
        element.innerHTML = `<h3>${this.parseInline(item.content)}</h3>`;
        break;

      case 'h4':
        element.innerHTML = `<h4>${this.parseInline(item.content)}</h4>`;
        break;

      case 'text':
        element.innerHTML = `<p>${this.parseInline(item.content)}</p>`;
        break;

      case 'list-item':
        const li = document.createElement('li');
        li.innerHTML = this.parseInline(item.content);
        const ul = document.createElement('ul');
        ul.appendChild(li);
        element.appendChild(ul);
        break;

      case 'numbered-item':
        const nli = document.createElement('li');
        nli.innerHTML = this.parseInline(item.content);
        const ol = document.createElement('ol');
        ol.appendChild(nli);
        element.appendChild(ol);
        break;

      case 'code':
        const pre = document.createElement('pre');
        const code = document.createElement('code');
        code.className = `language-${item.language}`;
        code.textContent = item.content;
        pre.appendChild(code);
        element.appendChild(pre);
        break;

      case 'blockquote':
        element.innerHTML = `<blockquote>${this.parseInline(item.content)}</blockquote>`;
        break;

      case 'image':
        element.innerHTML = `<img src="${item.src}" alt="${item.alt || ''}" />`;
        break;

      case 'link':
        element.innerHTML = `<a href="${item.url}" target="_blank">${item.text}</a>`;
        break;

      default:
        return null;
    }

    return element;
  }

  /**

   * Parse inline markdown

   */
  parseInline(text) {
    if (!text) return '';

    // Bold
    text = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
    text = text.replace(/__([^_]+)__/g, '<strong>$1</strong>');

    // Italic
    text = text.replace(/\*([^*]+)\*/g, '<em>$1</em>');
    text = text.replace(/_([^_]+)_/g, '<em>$1</em>');

    // Inline code
    text = text.replace(/`([^`]+)`/g, '<code>$1</code>');

    return text;
  }

  /**

   * Show specific slide

   */
  async showSlide(index, direction = 'next') {
    if (this.isAnimating || index < 0 || index >= this.slides.length) {
      return;
    }

    this.isAnimating = true;

    const currentSlide = this.container.querySelector(`[data-index="${this.currentIndex}"]`);
    const nextSlide = this.container.querySelector(`[data-index="${index}"]`);

    if (!nextSlide) {
      this.isAnimating = false;
      return;
    }

    // Apply transition
    await this.applyTransition(currentSlide, nextSlide, direction);

    this.currentIndex = index;
    this.isAnimating = false;

    // Animate content items
    if (this.options.autoAnimate) {
      this.animateSlideContent(nextSlide);
    }
  }

  /**

   * Apply transition between slides

   */
  async applyTransition(currentSlide, nextSlide, direction) {
    const duration = this.options.transitionDuration;

    switch (this.options.transition) {
      case 'fade':
        await this.fadeTransition(currentSlide, nextSlide, duration);
        break;

      case 'slide':
        await this.slideTransition(currentSlide, nextSlide, direction, duration);
        break;

      case 'zoom':
        await this.zoomTransition(currentSlide, nextSlide, duration);
        break;

      case 'glitch':
        await this.glitchTransition(currentSlide, nextSlide, duration);
        break;

      case 'matrix':
        await this.matrixTransition(currentSlide, nextSlide, duration);
        break;

      case 'cube':
        await this.cubeTransition(currentSlide, nextSlide, direction, duration);
        break;

      case 'pixelate':
        await this.pixelateTransition(currentSlide, nextSlide, duration);
        break;

      default:
        // Instant transition
        if (currentSlide) currentSlide.style.display = 'none';
        nextSlide.style.display = 'flex';
    }
  }

  /**

   * Fade transition

   */
  async fadeTransition(currentSlide, nextSlide, duration) {
    nextSlide.style.display = 'flex';
    nextSlide.style.opacity = '0';

    await this.wait(50);

    if (currentSlide) {
      currentSlide.style.transition = `opacity ${duration}ms ease`;
      currentSlide.style.opacity = '0';
    }

    nextSlide.style.transition = `opacity ${duration}ms ease`;
    nextSlide.style.opacity = '1';

    await this.wait(duration);

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.opacity = '1';
    }
  }

  /**

   * Slide transition

   */
  async slideTransition(currentSlide, nextSlide, direction, duration) {
    const offset = direction === 'next' ? '100%' : '-100%';

    nextSlide.style.display = 'flex';
    nextSlide.style.transform = `translateX(${offset})`;

    await this.wait(50);

    if (currentSlide) {
      currentSlide.style.transition = `transform ${duration}ms ease`;
      currentSlide.style.transform = `translateX(${direction === 'next' ? '-100%' : '100%'})`;
    }

    nextSlide.style.transition = `transform ${duration}ms ease`;
    nextSlide.style.transform = 'translateX(0)';

    await this.wait(duration);

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.transform = 'translateX(0)';
    }
  }

  /**

   * Zoom transition

   */
  async zoomTransition(currentSlide, nextSlide, duration) {
    nextSlide.style.display = 'flex';
    nextSlide.style.transform = 'scale(0.8)';
    nextSlide.style.opacity = '0';

    await this.wait(50);

    if (currentSlide) {
      currentSlide.style.transition = `all ${duration}ms ease`;
      currentSlide.style.transform = 'scale(1.2)';
      currentSlide.style.opacity = '0';
    }

    nextSlide.style.transition = `all ${duration}ms ease`;
    nextSlide.style.transform = 'scale(1)';
    nextSlide.style.opacity = '1';

    await this.wait(duration);

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.transform = 'scale(1)';
      currentSlide.style.opacity = '1';
    }
  }

  /**

   * Animate slide content items

   */
  animateSlideContent(slideElement) {
    const items = slideElement.querySelectorAll('.content-item');
    
    items.forEach((item, index) => {
      const delay = parseInt(item.dataset.animationDelay) || 0;
      
      setTimeout(() => {
        item.style.transition = 'all 400ms ease';
        item.style.opacity = '1';
        item.style.transform = 'translateY(0)';
      }, delay);
    });
  }

  /**

   * Navigate to next slide

   */
  async next() {
    if (this.currentIndex < this.slides.length - 1) {
      await this.showSlide(this.currentIndex + 1, 'next');
    }
  }

  /**

   * Navigate to previous slide

   */
  async previous() {
    if (this.currentIndex > 0) {
      await this.showSlide(this.currentIndex - 1, 'prev');
    }
  }

  /**

   * Go to first slide

   */
  async goToFirst() {
    await this.showSlide(0, 'next');
  }

  /**

   * Go to last slide

   */
  async goToLast() {
    await this.showSlide(this.slides.length - 1, 'prev');
  }

  /**

   * Glitch transition - Digital glitch effect

   */
  async glitchTransition(currentSlide, nextSlide, duration) {
    nextSlide.style.display = 'flex';
    nextSlide.style.opacity = '0';

    const glitchSteps = 8;
    const stepDuration = duration / glitchSteps;

    for (let i = 0; i < glitchSteps; i++) {
      const offset = Math.random() * 20 - 10;
      const opacity = i / glitchSteps;

      if (currentSlide) {
        currentSlide.style.transform = `translateX(${offset}px)`;
        currentSlide.style.opacity = String(1 - opacity);
        currentSlide.style.filter = `hue-rotate(${Math.random() * 360}deg)`;
      }

      nextSlide.style.transform = `translateX(${-offset}px)`;
      nextSlide.style.opacity = String(opacity);
      nextSlide.style.filter = `hue-rotate(${Math.random() * 360}deg)`;

      await this.wait(stepDuration);
    }

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.transform = '';
      currentSlide.style.filter = '';
    }
    nextSlide.style.transform = '';
    nextSlide.style.filter = '';
    nextSlide.style.opacity = '1';
  }

  /**

   * Matrix transition - Digital rain effect simulation

   */
  async matrixTransition(currentSlide, nextSlide, duration) {
    nextSlide.style.display = 'flex';
    
    // Create matrix overlay
    const matrix = document.createElement('div');
    matrix.style.position = 'absolute';
    matrix.style.inset = '0';
    matrix.style.background = 'linear-gradient(0deg, transparent 0%, rgba(0, 255, 0, 0.8) 50%, transparent 100%)';
    matrix.style.backgroundSize = '100% 200%';
    matrix.style.animation = `matrixScroll ${duration}ms linear`;
    matrix.style.zIndex = '1000';
    matrix.style.pointerEvents = 'none';
    
    this.container.appendChild(matrix);

    // Fade out current, fade in next
    if (currentSlide) {
      currentSlide.style.transition = `opacity ${duration * 0.7}ms ease`;
      currentSlide.style.opacity = '0';
    }

    await this.wait(duration * 0.3);

    nextSlide.style.opacity = '0';
    nextSlide.style.transition = `opacity ${duration * 0.7}ms ease`;
    nextSlide.style.opacity = '1';

    await this.wait(duration * 0.7);

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.opacity = '1';
    }
    
    matrix.remove();
  }

  /**

   * Cube transition - 3D cube rotation

   */
  async cubeTransition(currentSlide, nextSlide, direction, duration) {
    const perspective = '1200px';
    const rotateY = direction === 'next' ? '-90deg' : '90deg';

    this.container.style.perspective = perspective;
    this.container.style.perspectiveOrigin = 'center center';

    nextSlide.style.display = 'flex';
    nextSlide.style.transform = `rotateY(${direction === 'next' ? '90deg' : '-90deg'})`;
    nextSlide.style.transformOrigin = direction === 'next' ? 'right center' : 'left center';

    if (currentSlide) {
      currentSlide.style.transformOrigin = direction === 'next' ? 'left center' : 'right center';
    }

    await this.wait(50);

    if (currentSlide) {
      currentSlide.style.transition = `transform ${duration}ms cubic-bezier(0.645, 0.045, 0.355, 1)`;
      currentSlide.style.transform = `rotateY(${rotateY})`;
    }

    nextSlide.style.transition = `transform ${duration}ms cubic-bezier(0.645, 0.045, 0.355, 1)`;
    nextSlide.style.transform = 'rotateY(0deg)';

    await this.wait(duration);

    if (currentSlide) {
      currentSlide.style.display = 'none';
      currentSlide.style.transform = '';
    }
    nextSlide.style.transform = '';
    this.container.style.perspective = '';
  }

  /**

   * Pixelate transition - Pixelation effect

   */
  async pixelateTransition(currentSlide, nextSlide, duration) {
    nextSlide.style.display = 'flex';
    nextSlide.style.opacity = '0';

    const steps = 10;
    const stepDuration = duration / steps;

    // Pixelate out
    for (let i = 0; i <= steps / 2; i++) {
      const pixelSize = i * 2;
      if (currentSlide) {
        currentSlide.style.filter = `blur(${pixelSize}px)`;
        currentSlide.style.opacity = String(1 - (i / (steps / 2)));
      }
      await this.wait(stepDuration);
    }

    if (currentSlide) {
      currentSlide.style.display = 'none';
    }

    // Pixelate in
    for (let i = steps / 2; i >= 0; i--) {
      const pixelSize = i * 2;
      nextSlide.style.filter = `blur(${pixelSize}px)`;
      nextSlide.style.opacity = String(1 - (i / (steps / 2)));
      await this.wait(stepDuration);
    }

    if (currentSlide) {
      currentSlide.style.filter = '';
      currentSlide.style.opacity = '1';
    }
    nextSlide.style.filter = '';
    nextSlide.style.opacity = '1';
  }

  /**

   * Utility: wait for milliseconds

   */
  wait(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  /**

   * Get current slide info

   */
  getCurrentSlide() {
    return this.slides[this.currentIndex];
  }

  /**

   * Get slide count

   */
  getSlideCount() {
    return this.slides.length;
  }
}

// Export helper function
export function createSlideRenderer(container, options = {}) {
  return new SlideRenderer(container, options);
}