// mocks.jsx — deterministic AI-image-look generator + mock catalogue

// ──────────────────────────────────────────────────────────────────────────
// Seeded RNG
// ──────────────────────────────────────────────────────────────────────────
function hashStr(str) {
  let h = 2166136261;
  for (let i = 0; i < str.length; i++) {
    h ^= str.charCodeAt(i);
    h = Math.imul(h, 16777619);
  }
  return h >>> 0;
}
function mulberry32(seed) {
  let s = seed >>> 0;
  return function () {
    s = (s + 0x6d2b79f5) >>> 0;
    let t = s;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

// ──────────────────────────────────────────────────────────────────────────
// Curated palettes (4 colours each, last is the deep background)
// ──────────────────────────────────────────────────────────────────────────
const PALETTES = {
  cinematic: ['#ff6b35', '#f7c59f', '#2a1b3d', '#0d0a14'],
  ocean:     ['#48cae4', '#0077b6', '#caf0f8', '#03045e'],
  sunset:    ['#ff006e', '#fb5607', '#ffbe0b', '#1a1033'],
  violet:    ['#c77dff', '#7209b7', '#f72585', '#10002b'],
  forest:    ['#74c69d', '#52b788', '#d8f3dc', '#1b4332'],
  neon:      ['#ff10f0', '#00f5ff', '#fdfd00', '#0a0a2e'],
  pastel:    ['#ffb3c1', '#a0c4ff', '#bdb2ff', '#fdffb6'],
  noir:      ['#d4af37', '#8b7355', '#3d3d3d', '#0a0a0a'],
  rose:      ['#ffd6a5', '#ffadad', '#9c6644', '#3a1f0a'],
  arctic:    ['#e0fbfc', '#98c1d9', '#3d5a80', '#0b132b'],
  amber:     ['#ffd60a', '#ff9500', '#bf4904', '#1a0a00'],
  sage:      ['#cad2c5', '#84a98c', '#52796f', '#2f3e46'],
};
const PALETTE_KEYS = Object.keys(PALETTES);

function pickPalette(prompt, seed) {
  const p = (prompt || '').toLowerCase();
  if (/sunset|altın|gün[\s-]?doğumu|gold/.test(p)) return PALETTES.sunset;
  if (/ocean|sea|water|deniz|göl|wave|mavi/.test(p)) return PALETTES.ocean;
  if (/forest|tree|orman|ağaç|yeşil|nature/.test(p)) return PALETTES.forest;
  if (/neon|cyber|future|sci[\s-]?fi|gelecek/.test(p)) return PALETTES.neon;
  if (/dream|fantasy|rüya|fantastik|magic/.test(p)) return PALETTES.violet;
  if (/pastel|soft|gentle|cute|yumuşak/.test(p)) return PALETTES.pastel;
  if (/noir|dark|black|siyah/.test(p)) return PALETTES.noir;
  if (/sinema|cinema|film|movie/.test(p)) return PALETTES.cinematic;
  if (/winter|snow|kar|buz|arctic/.test(p)) return PALETTES.arctic;
  if (/desert|sand|çöl|kum|amber/.test(p)) return PALETTES.amber;
  if (/sage|herb|olive|zeytin/.test(p)) return PALETTES.sage;
  if (/rose|flower|çiçek|pembe/.test(p)) return PALETTES.rose;
  return PALETTES[PALETTE_KEYS[seed % PALETTE_KEYS.length]];
}

// ──────────────────────────────────────────────────────────────────────────
// SVG composition — looks like an AI-generated dreamscape
// ──────────────────────────────────────────────────────────────────────────
function GenSVG({ prompt, seed: seedOverride, style, className, w = 100, h = 100 }) {
  const seed = seedOverride != null ? seedOverride : hashStr(prompt || 'untitled');
  const rand = React.useMemo(() => mulberry32(seed), [seed]);
  const data = React.useMemo(() => {
    const palette = pickPalette(prompt, seed);
    const bg = palette[palette.length - 1];
    const blobs = [];
    const count = 5 + Math.floor(rand() * 3);
    for (let i = 0; i < count; i++) {
      blobs.push({
        cx: rand() * 120 - 10,
        cy: rand() * 120 - 10,
        r: 25 + rand() * 55,
        color: palette[i % (palette.length - 1)],
        opacity: 0.5 + rand() * 0.45,
      });
    }
    const horizon = rand() > 0.5 ? 40 + rand() * 30 : null;
    const sunY = rand() * 35 + 10;
    const sunX = rand() * 100;
    const sunR = 6 + rand() * 14;
    const sunColor = palette[Math.floor(rand() * (palette.length - 1))];
    return { palette, bg, blobs, horizon, sunY, sunX, sunR, sunColor };
  }, [seed]);
  const uid = `g${seed}`;

  return (
    <svg
      className={className}
      style={style}
      viewBox={`0 0 ${w} ${h}`}
      preserveAspectRatio="xMidYMid slice"
      xmlns="http://www.w3.org/2000/svg"
    >
      <defs>
        <filter id={`blur-${uid}`} x="-20%" y="-20%" width="140%" height="140%">
          <feGaussianBlur stdDeviation="9" />
        </filter>
        <filter id={`grain-${uid}`}>
          <feTurbulence type="fractalNoise" baseFrequency="1.6" numOctaves="2" seed={seed % 999} />
          <feColorMatrix values="0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.18 0" />
          <feComposite operator="in" in2="SourceGraphic" />
        </filter>
        <radialGradient id={`vig-${uid}`} cx="50%" cy="50%" r="70%">
          <stop offset="55%" stopColor="#000" stopOpacity="0" />
          <stop offset="100%" stopColor="#000" stopOpacity="0.55" />
        </radialGradient>
        <linearGradient id={`hz-${uid}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={data.palette[0]} stopOpacity="0.5" />
          <stop offset="1" stopColor={data.bg} stopOpacity="0" />
        </linearGradient>
      </defs>

      <rect width={w} height={h} fill={data.bg} />
      <g filter={`url(#blur-${uid})`}>
        {data.blobs.map((b, i) => (
          <circle key={i} cx={b.cx} cy={b.cy} r={b.r} fill={b.color} opacity={b.opacity} />
        ))}
      </g>

      {/* faint sun / focal */}
      <circle cx={data.sunX} cy={data.sunY} r={data.sunR}
              fill={data.sunColor} opacity="0.55"
              filter={`url(#blur-${uid})`} />

      {/* optional horizon shimmer */}
      {data.horizon != null && (
        <rect x="0" y={data.horizon} width={w} height={h - data.horizon} fill={`url(#hz-${uid})`} />
      )}

      {/* vignette */}
      <rect width={w} height={h} fill={`url(#vig-${uid})`} />

      {/* grain overlay */}
      <rect width={w} height={h} fill="#fff" opacity="0.5" filter={`url(#grain-${uid})`} />
    </svg>
  );
}

// ──────────────────────────────────────────────────────────────────────────
// Mock catalogue — models, presets, sample prompts, history seeds
// ──────────────────────────────────────────────────────────────────────────
const MODELS = [
  { id: 'aurora-x',    name: 'Aurora X',    tag: 'Photoreal', speed: 'Fast',     price: '$0.012' },
  { id: 'aurora-x-hd', name: 'Aurora X HD', tag: 'Photoreal', speed: 'Standart', price: '$0.024' },
  { id: 'lumen-3',     name: 'Lumen 3',     tag: 'Cinema',    speed: 'Standart', price: '$0.018' },
  { id: 'kana-v2',     name: 'Kana v2',     tag: 'Anime',     speed: 'Hızlı',    price: '$0.008' },
  { id: 'craft-2',     name: 'Craft 2',     tag: '3D / CGI',  speed: 'Yavaş',    price: '$0.030' },
  { id: 'sketchy',     name: 'Sketchy',     tag: 'Illustration', speed: 'Hızlı', price: '$0.006' },
  { id: 'noir-1',      name: 'Noir 1',      tag: 'Mono',      speed: 'Hızlı',    price: '$0.010' },
  { id: 'free-mini',   name: 'Free Mini',   tag: ':free',     speed: 'Hızlı',    price: 'ücretsiz' },
];

const STYLE_PRESETS = [
  'Sinematik', 'Foto-gerçek', 'Anime', '3D render',
  'İllüstrasyon', 'Yağlı boya', 'Mürekkep eskiz', 'Polaroid',
];

const STARTER_PROMPTS = [
  'İstanbul Boğazı üstünde gün doğumu, sinematik ışık, anamorfik lens',
  'çay bardağında küçük bir orman, fotoğrafik, makro, soft bokeh',
  'cyberpunk Kapadokya, neon balonlar, yağmurlu gece',
  'pastel renklerde uzayda yüzen bir kütüphane, art-direction by Wes Anderson',
  'eski bir İtalyan sokağı, yağlı boya, altın saat',
  'minimal beyaz seramik vazoda kuru otlar, stüdyo ışığı',
];

const SAMPLE_HISTORY = [
  { prompt: 'gece pazarı, neon ışıklar, yağmur', model: 'lumen-3', t: '14:02' },
  { prompt: 'İskandinav kabin, kar, sıcak ışık', model: 'aurora-x', t: '13:48' },
  { prompt: 'tilki portresi, akvarel', model: 'sketchy', t: '13:31' },
  { prompt: 'retro araba, çöl yolu, gün batımı', model: 'lumen-3', t: '12:55' },
  { prompt: 'su altı şehri, mavi tonlar', model: 'aurora-x-hd', t: '12:20' },
  { prompt: 'kahve fincanında galaksi', model: 'craft-2', t: '11:47' },
];

// Mock "AI prompt improver"
function improvePrompt(input) {
  const base = (input || '').trim();
  if (!base) return base;
  const additions = [
    'sinematik aydınlatma',
    'yüksek detay',
    '8k, sharp focus',
    'soft bokeh arka plan',
    'doğal renk paleti',
    'kompozisyon: rule of thirds',
  ];
  const hash = hashStr(base) % additions.length;
  const picked = [additions[hash], additions[(hash + 2) % additions.length], additions[(hash + 4) % additions.length]];
  return `${base}, ${picked.join(', ')}`;
}

// Export to window so other Babel scripts can use them
Object.assign(window, {
  GenSVG, MODELS, STYLE_PRESETS, STARTER_PROMPTS, SAMPLE_HISTORY,
  hashStr, mulberry32, improvePrompt, PALETTES,
});
