/**
 * pres/shapes — shape tokens.
 *
 * One knob ('sharp' | 'soft' | 'round' | 'pill') decides corner radius,
 * shadow weight, and stroke style for every shape primitive used in
 * the deck. The agent sets it once at the theme level (or overrides
 * per slide); archetypes call `resolveShape(shape)` and read tokens.
 *
 * pptxgenjs corner-radius units are inches. PPTX shadow units we
 * package as a plain object that pptxgenjs accepts on shape options.
 */

const SHAPE_PRESETS = {
  sharp: {
    cornerRadius: 0,
    shadow: null,
    strokeWeight: 0.5,
    dividerWeight: 0.5,
  },
  soft: {
    cornerRadius: 0.05,
    shadow: { type: 'outer', blur: 6, offset: 1, angle: 90, color: '000000', opacity: 0.08 },
    strokeWeight: 0,
    dividerWeight: 0.5,
  },
  round: {
    cornerRadius: 0.18,
    shadow: { type: 'outer', blur: 12, offset: 3, angle: 90, color: '000000', opacity: 0.12 },
    strokeWeight: 0,
    dividerWeight: 0,
  },
  pill: {
    // Used for "pill" buttons and tags. The actual corner is rounded
    // to whichever value pptxgenjs accepts as "fully rounded" — using
    // half the typical shape height as a safe approximation.
    cornerRadius: 0.5,
    shadow: { type: 'outer', blur: 16, offset: 4, angle: 90, color: '000000', opacity: 0.14 },
    strokeWeight: 0,
    dividerWeight: 0,
  },
};

export function resolveShape(preset = 'soft') {
  const base = SHAPE_PRESETS[preset] || SHAPE_PRESETS.soft;
  return { preset, ...base };
}

/**
 * Convert a logical shape token into pptxgenjs `addShape` options.
 * `kind`: 'card' | 'pill' | 'divider' | 'dot'. The caller still
 * supplies position + fill colour; this fills in the styling.
 */
export function shapeOpts(tokens, kind, fillHex, opts = {}) {
  const out = { fill: { color: fillHex } };
  if (kind === 'card' || kind === 'pill') {
    if (tokens.cornerRadius > 0) {
      out.rectRadius = tokens.cornerRadius;
    }
    if (tokens.shadow) {
      out.shadow = tokens.shadow;
    }
    if (tokens.strokeWeight > 0 && opts.stroke) {
      out.line = { color: opts.stroke, width: tokens.strokeWeight };
    } else {
      out.line = { type: 'none' };
    }
  } else if (kind === 'divider') {
    out.line = { color: fillHex, width: opts.width ?? tokens.dividerWeight ?? 0.5 };
  }
  return out;
}

/**
 * pptxgenjs has two ways to draw a rectangle:
 *   - `addShape('rect', opts)` — sharp corners
 *   - `addShape('roundRect', opts)` — corners use opts.rectRadius
 * `cardShape(tokens)` returns whichever one the current shape token
 * wants, so archetypes call `slide.addShape(cardShape(tokens), …)`
 * without branching.
 */
export function cardShape(tokens) {
  return tokens.cornerRadius > 0 ? 'roundRect' : 'rect';
}
