/* global React, window */
// ─────────────────────────────────────────────────────────────
// ActionsRow
// Lifted from ref/mastermind-v2/components/response-ui.jsx:415–454
//
// Footer action bar for a Mastermind turn: Copy · Regenerate · Branch · Speak · Pin · Share.
//
// Departures from v2 source:
//   • `variant`: "bar" (default, top-border separator) | "floating" (pill cluster)
//              | "minimal" (icons only, no labels)
//   • `actions`: array of string IDs to control which show & order them.
//                e.g. ["copy", "regen", "branch"]. Missing handlers just skip.
//   • `extra`: slot for custom trailing actions (e.g. "Promote to plan").
//   • active-state for toggles (Pin after pin click).
//   • Copy button does the clipboard write internally when `copyText` is passed
//     (with "Copied" confirmation state).
// ─────────────────────────────────────────────────────────────

const AR_FONT = '"Inter", -apple-system, BlinkMacSystemFont, sans-serif';

const ARIcon = {
  copy:    () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"/></svg>,
  check:   () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>,
  regen:   () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.5 15A9 9 0 116.6 5.3L23 10"/></svg>,
  branch:  () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="6" cy="6" r="3"/><circle cx="18" cy="18" r="3"/><path d="M6 9v6a3 3 0 003 3h6"/></svg>,
  pin:     () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 2l3 6 6 .9-4.5 4.3 1.1 6.3L12 16.8 6.4 19.5l1.1-6.3L3 8.9 9 8z"/></svg>,
  pinFill: () => <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" strokeWidth="1" strokeLinejoin="round"><path d="M12 2l3 6 6 .9-4.5 4.3 1.1 6.3L12 16.8 6.4 19.5l1.1-6.3L3 8.9 9 8z"/></svg>,
  share:   () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.6" y1="13.5" x2="15.4" y2="17.5"/><line x1="15.4" y1="6.5" x2="8.6" y2="10.5"/></svg>,
  speak:   () => <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5"/><path d="M15.54 8.46a5 5 0 010 7.07M19.07 4.93a10 10 0 010 14.14"/></svg>,
};

const ACTION_DEFAULTS = {
  copy:   { label: 'Copy',       icon: 'copy'   },
  regen:  { label: 'Regenerate', icon: 'regen'  },
  branch: { label: 'Branch',     icon: 'branch' },
  speak:  { label: 'Speak',      icon: 'speak'  },
  pin:    { label: 'Pin',        icon: 'pin'    },
  share:  { label: 'Share',      icon: 'share'  },
};

function ActionsRow({
  actions = ['copy', 'regen', 'branch', 'speak', 'pin', 'share'],
  onCopy, onRegenerate, onBranch, onSpeak, onPin, onShare,
  copyText,
  pinned = false,
  variant = 'bar',
  extra,
  style,
}) {
  const [hover, setHover]   = React.useState(null);
  const [copied, setCopied] = React.useState(false);

  const handlers = {
    copy:   () => {
      if (copyText && navigator.clipboard?.writeText) {
        navigator.clipboard.writeText(copyText).catch(() => {});
        setCopied(true);
        setTimeout(() => setCopied(false), 1200);
      }
      onCopy && onCopy();
    },
    regen:  onRegenerate,
    branch: onBranch,
    speak:  onSpeak,
    pin:    onPin,
    share:  onShare,
  };

  const items = actions
    .filter(id => ACTION_DEFAULTS[id])
    .map(id => {
      const cfg = ACTION_DEFAULTS[id];
      const isCopy = id === 'copy' && copied;
      const isPinActive = id === 'pin' && pinned;
      return {
        id,
        label: isCopy ? 'Copied' : cfg.label,
        iconKey: isCopy ? 'check' : (isPinActive ? 'pinFill' : cfg.icon),
        active: isCopy || isPinActive,
        on: handlers[id],
      };
    });

  // shared button style
  const btnStyle = (it, isHover) => {
    const minimal = variant === 'minimal';
    const floating = variant === 'floating';
    return {
      display: 'inline-flex', alignItems: 'center', gap: minimal ? 0 : 5,
      padding: minimal ? '6px 7px' : '5px 9px',
      border: 'none',
      borderRadius: floating ? 999 : 6,
      cursor: 'pointer',
      background: it.active ? 'rgba(255, 45, 180, 0.10)'
                : isHover  ? 'rgba(0,0,0,0.05)' : 'transparent',
      color: it.active ? '#C01A7E'
           : isHover ? 'rgba(0,0,0,0.85)' : 'rgba(0,0,0,0.55)',
      fontFamily: AR_FONT, fontSize: 11.5, fontWeight: 500,
      transition: 'background 120ms, color 120ms',
    };
  };

  const rowStyle = {
    bar: {
      display: 'flex', alignItems: 'center', gap: 2, marginTop: 10,
      padding: '4px 0', borderTop: '1px solid rgba(0,0,0,0.05)',
    },
    floating: {
      display: 'inline-flex', alignItems: 'center', gap: 2,
      padding: 4, borderRadius: 999,
      background: 'rgba(255,255,255,0.9)',
      boxShadow: '0 2px 12px rgba(0,0,0,0.08), 0 1px 3px rgba(0,0,0,0.04)',
      border: '1px solid rgba(0,0,0,0.05)',
      backdropFilter: 'blur(8px)',
    },
    minimal: {
      display: 'inline-flex', alignItems: 'center', gap: 0,
    },
  };

  return (
    <div style={{ ...rowStyle[variant], ...style }}>
      {items.map(it => (
        <button key={it.id}
          onMouseEnter={() => setHover(it.id)}
          onMouseLeave={() => setHover(null)}
          onClick={it.on}
          title={it.label}
          style={btnStyle(it, hover === it.id)}
        >
          {ARIcon[it.iconKey] && ARIcon[it.iconKey]()}
          {variant !== 'minimal' && <span>{it.label}</span>}
        </button>
      ))}
      {variant === 'bar' && <span style={{ flex: 1 }} />}
      {extra}
    </div>
  );
}

Object.assign(window, { ActionsRow });
