← back to Dw Slideshow Gen

lib/plan.js

36 lines

// lib/plan.js — assemble queue items into SlideshowSpec[].
// A spec is the render-ready contract: 6 slides (1 hook + 4 body + 1 CTA),
// caption + hashtags, plus provenance. Pure data; no canvas here.
import { slugify, stamp, titleCase } from './util.js'
import { generateCarousel } from './hooks.js'

export function buildSpec(item, design, opts = {}) {
  const c = generateCarousel(item, design, opts)
  const topic = titleCase(item.topic || 'Wallcovering')
  const bg = item.backgrounds || {}
  const slides = [
    { index: 1, kind: 'hook', title: c.hook, subtitle: c.tagline, background: bg['1'] || null },
    ...c.body.map((b, i) => ({ index: i + 2, kind: 'body', title: b.title, subtitle: b.subtitle, background: bg[String(i + 2)] || null })),
    { index: 6, kind: 'cta', title: c.cta.label, subtitle: c.cta.sub, background: bg['6'] || null },
  ]
  return {
    id: slugify(item.id || item.topic || 'slideshow'),
    project: design.brand,
    source_topic: item.topic || null,
    angle: item.angle || null,
    hook: c.hook,
    slides,
    caption: c.caption,
    hashtags: c.hashtags,
    copy_source: c.copy_source,
    background: item.background || null, // optional bg image hint per spec
    generated_at: stamp(),
  }
}

export function buildPlan(queue, design, opts = {}) {
  const items = Array.isArray(queue) ? queue : (queue.items || [])
  if (!items.length) throw new Error('queue has no items')
  return items.map(it => buildSpec(it, design, opts))
}