import type { PptTemplateConfig } from '../types/pptTemplate'
import type { PptSectionSlide } from '../utils/articlePptSlides'

/** 后台/预览用示例幻灯片 */
export const PPT_TEMPLATE_PREVIEW_SLIDE: PptSectionSlide = {
  kind: 'section',
  title: '示例章节 · 演示内容',
  continued: false,
  blocks: [
    { type: 'h2', text: '小节标题' },
    { type: 'paragraph', text: '这是模板预览示例段落，修改配色与动画可即时看到效果。' },
    { type: 'list', ordered: true, items: ['步骤一：打开应用', '步骤二：完成操作'] },
  ],
}

export const SYSTEM_DEFAULT_PPT_CONFIG: PptTemplateConfig = {
  colors: {
    bg: '#141414',
    text: '#f5f5f5',
    muted: '#a0a0a0',
    accent: '#c9a962',
    codeBg: '#1e1e1e',
    codeText: '#eeeeee',
    border: 'rgba(255, 255, 255, 0.1)',
  },
  animation: {
    revealDuration: '0.95s',
    revealLeaveDuration: '0.65s',
    slideDuration: '0.85s',
    layoutDuration: '0.75s',
    revealOffset: '10px',
    easing: 'cubic-bezier(0.22, 1, 0.36, 1)',
  },
  transitions: {
    slide: 'crossfade',
    reveal: 'fadeUp',
  },
  ui: {
    showThumbnailsDefault: true,
    canvasBorderRadius: '0.5rem',
  },
}

export const SYSTEM_DEFAULT_PPT_CONFIG_JSON = JSON.stringify(SYSTEM_DEFAULT_PPT_CONFIG)

export function clonePptTemplateConfig(config: PptTemplateConfig): PptTemplateConfig {
  return JSON.parse(JSON.stringify(config)) as PptTemplateConfig
}

export function createEmptyPptTemplateForm() {
  return {
    name: '',
    slug: '',
    description: '',
    config: clonePptTemplateConfig(SYSTEM_DEFAULT_PPT_CONFIG),
    isDefault: false,
    sortOrder: 0,
    isActive: true,
  }
}

export function parsePptTemplateConfig(raw: unknown): PptTemplateConfig {
  const base = clonePptTemplateConfig(SYSTEM_DEFAULT_PPT_CONFIG)
  if (!raw || typeof raw !== 'object') return base

  const input = raw as Partial<PptTemplateConfig>
  if (input.colors) Object.assign(base.colors, input.colors)
  if (input.animation) Object.assign(base.animation, input.animation)
  if (input.transitions) Object.assign(base.transitions, input.transitions)
  if (input.ui) Object.assign(base.ui, input.ui)
  return base
}
