// Avokado Bar app redesign — shared primitives. No emoji; custom SVG icons only.
// Namespaced under window.AvbX. Depends on window.AvbX.{TILE_TONES, BADGES} (data.jsx).
(function(){
const { useState, useEffect, useRef, useMemo, useLayoutEffect, useCallback } = React;
const { TILE_TONES, BADGES } = window.AvbX;

// ── Brand palette shortcuts ──
const GREEN = '#CFD829';      // production lime (matches app accent)
const GREEN_SOFT = '#E6EF7B';
const INK = '#1A1A1A';
const OLIVE = '#6B6B59';
const CREAM = '#F5F0E8';
const PAPER = '#FFFFFF';

// ── Icons ──
const Icon = {
  search:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>),
  back:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="M15 6l-6 6 6 6"/></svg>),
  close:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><path d="M6 6l12 12M18 6L6 18"/></svg>),
  plus:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="2.1" strokeLinecap="round"><path d="M12 5v14M5 12h14"/></svg>),
  minus:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="2.1" strokeLinecap="round"><path d="M5 12h14"/></svg>),
  heart:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||20} height={p.size||20} fill={p.filled?(p.color||'currentColor'):'none'} stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M12 20s-7-4.35-7-10a4 4 0 0 1 7-2.65A4 4 0 0 1 19 10c0 5.65-7 10-7 10z"/></svg>),
  bag:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinejoin="round"><path d="M5 8h14l-1 12a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 8z"/><path d="M9 8V5a3 3 0 0 1 6 0v3" strokeLinecap="round"/></svg>),
  clock:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="8.5"/><path d="M12 7v5l3.5 2"/></svg>),
  user:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="8.5" r="3.5"/><path d="M5 20c1.5-3.5 4-5 7-5s5.5 1.5 7 5"/></svg>),
  home:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M4 11l8-7 8 7"/><path d="M6 10v9a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1v-9"/></svg>),
  gift:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||22} height={p.size||22} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><rect x="4" y="9" width="16" height="11" rx="1.5"/><path d="M4 13h16M12 9v11M12 9S10 4 7.5 5C5.8 5.7 6.5 9 9 9zM12 9s2-5 4.5-4c1.7.7 1 4-1.5 4z"/></svg>),
  chevron:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M9 6l6 6-6 6"/></svg>),
  flame:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3s1 3 3 5 3 3.5 3 6a6 6 0 0 1-12 0c0-2 1-3.5 2-4.5 1 1 2 1 2-1 0-2 2-5.5 2-5.5z"/></svg>),
  leaf:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M4 20c6-1 13-6 16-16-1 11-7 16-16 16z"/><path d="M4 20c2-4 5-7 9-9"/></svg>),
  wheat:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3v18M8 6l4 2M16 6l-4 2M8 11l4 2M16 11l-4 2M8 16l4 2M16 16l-4 2"/></svg>),
  protein:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M6 4l4 4-6 6 4 4 6-6 4 4 2-2-4-4 2-2-4-4-2 2-4-4z"/></svg>),
  check:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke={p.color||'currentColor'} strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12l5 5L20 7"/></svg>),
  pin:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M12 21s7-7 7-12a7 7 0 0 0-14 0c0 5 7 12 7 12z"/><circle cx="12" cy="9" r="2.5"/></svg>),
  qr:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinejoin="round"><rect x="4" y="4" width="6" height="6" rx="1"/><rect x="14" y="4" width="6" height="6" rx="1"/><rect x="4" y="14" width="6" height="6" rx="1"/><path d="M14 14h2v2M20 14v2M14 18v2M18 18h2v2" strokeLinecap="round"/></svg>),
  sparkle:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||14} height={p.size||14} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5 18 18M6 18l2.5-2.5M15.5 8.5 18 6"/></svg>),
  card:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinejoin="round"><rect x="3" y="5" width="18" height="14" rx="2.5"/><path d="M3 9.5h18" strokeLinecap="round"/></svg>),
  apple:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill={p.color||'currentColor'}><path d="M16.3 12.6c0-2 1.6-2.9 1.7-3-1-1.4-2.4-1.6-2.9-1.6-1.2-.1-2.4.7-3 .7-.6 0-1.6-.7-2.6-.7-1.3 0-2.6.8-3.2 2-1.4 2.4-.4 6 1 8 .6 1 1.4 2.1 2.4 2 .9 0 1.3-.6 2.4-.6s1.4.6 2.4.6 1.7-1 2.3-2c.7-1.1 1-2.2 1-2.3 0 0-2-.8-2-3.1zM14.6 6.3c.5-.6.9-1.5.8-2.4-.8 0-1.7.5-2.2 1.2-.5.5-.9 1.4-.8 2.3.9 0 1.8-.5 2.2-1.1z"/></svg>),
  walletPts:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||18} height={p.size||18} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinejoin="round"><path d="M3 7a2 2 0 0 1 2-2h12.5a1.5 1.5 0 0 1 0 3H5"/><path d="M3 7v10a2 2 0 0 0 2 2h13a1 1 0 0 0 1-1v-3"/><circle cx="17" cy="13" r="1.4" fill={p.color||'currentColor'} stroke="none"/></svg>),
  note:(p={})=>(<svg viewBox="0 0 24 24" width={p.size||16} height={p.size||16} fill="none" stroke={p.color||'currentColor'} strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round"><path d="M4 5h16M4 10h16M4 15h10"/></svg>),
};

// ── DishImage: real photo if present, else premium tonal tile ──
function DishImage({ photo, tone='cream', radius=14, style={}, className='', children, dim=false }) {
  const t = TILE_TONES[tone] || TILE_TONES.cream;
  const base = {
    position:'relative', overflow:'hidden', borderRadius:radius, flex:'none', ...style,
  };
  if (photo) {
    // photo formatları:
    //   "photos/x/y.webp" veya "photos/x/y.jpg" (Firestore imageUrl) → olduğu gibi
    //   "/photos/..." absolute path → olduğu gibi
    //   "http://..." veya "https://..." → olduğu gibi
    //   "x/y" extension'sız (legacy) → "photos/x/y.jpg" formatına çevir
    let src;
    if (/^(https?:)?\/\//.test(photo)) src = photo;
    else if (photo.startsWith('/')) src = photo;
    else if (photo.startsWith('photos/')) src = '/' + photo;
    else if (/\.(jpg|jpeg|png|webp|gif)$/i.test(photo)) src = '/' + photo;
    else src = '/photos/' + photo + '.jpg';
    return (
      <div className={className} style={{ ...base, backgroundImage:`url('${src}')`, backgroundSize:'cover', backgroundPosition:'center', backgroundColor:'#ddd' }}>
        {dim && <div style={{ position:'absolute', inset:0, background:'rgba(0,0,0,0.12)' }} />}
        {children}
      </div>
    );
  }
  return (
    <div className={className} style={{ ...base, background:`linear-gradient(135deg, ${t.a} 0%, ${t.b} 100%)`, boxShadow:'inset 0 0 0 1px rgba(255,255,255,0.35), inset 0 -18px 22px rgba(0,0,0,0.06)' }}>
      <div style={{ position:'absolute', top:'-15%', left:'-10%', width:'70%', height:'70%', borderRadius:'50%', background:'radial-gradient(circle, rgba(255,255,255,0.4) 0%, rgba(255,255,255,0) 70%)' }} />
      <svg viewBox="0 0 24 24" width="26%" height="26%" style={{ position:'absolute', bottom:'8%', right:'8%', opacity:0.3 }} fill="none" stroke={INK} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
        <path d="M12 3c5 0 8 3.5 8 8s-3.5 10-8 10-8-4.5-8-10 3-8 8-8z"/><circle cx="12" cy="14" r="2.2" fill={INK} stroke="none" opacity="0.5"/>
      </svg>
      {children}
    </div>
  );
}

// ── Badge ──
function Badge({ kind, lang='tr', size='md' }) {
  const m = BADGES[kind]; if (!m) return null;
  const tones = {
    pop:{ bg:GREEN, fg:INK, I:Icon.flame }, new:{ bg:INK, fg:GREEN, I:Icon.sparkle },
    veg:{ bg:'#E6F2DD', fg:'#3F6B2E', I:Icon.leaf }, gf:{ bg:'#F5ECD9', fg:'#8A6420', I:Icon.wheat },
    hp:{ bg:'#EDE4F5', fg:'#5B3E8A', I:Icon.protein },
  }[kind];
  const I = tones.I;
  const h = size==='sm'?20:22;
  return (
    <span style={{ display:'inline-flex', alignItems:'center', gap:4, height:h, padding:'0 8px 0 6px', borderRadius:999, background:tones.bg, color:tones.fg, fontSize:size==='sm'?10:11, fontWeight:700, lineHeight:1, whiteSpace:'nowrap' }}>
      <I size={size==='sm'?10:12} color={tones.fg} />{m[lang]}
    </span>
  );
}

// ── Tap wrapper (haptic + press) ──
function Tap({ as='button', onClick, style, scale=0.97, haptic='light', children, ...rest }) {
  const ref = useRef(null);
  const press=()=>{ if(ref.current) ref.current.style.transform=`scale(${scale})`; };
  const release=()=>{ if(ref.current) ref.current.style.transform='scale(1)'; };
  const handle=(e)=>{ try{ if(window.navigator&&window.navigator.vibrate) {} }catch(_){} onClick&&onClick(e); };
  const Comp = as;
  return (
    <Comp ref={ref} onClick={handle} onMouseDown={press} onMouseUp={release} onMouseLeave={release} onTouchStart={press} onTouchEnd={release}
      style={{ fontFamily:'var(--font-body)', transition:'transform 120ms var(--ease), background 160ms var(--ease), box-shadow 160ms var(--ease), opacity 160ms', ...style }} {...rest}>
      {children}
    </Comp>
  );
}

function PrimaryButton({ children, onClick, full, small, disabled, style }) {
  return (
    <Tap haptic="medium" onClick={disabled?undefined:onClick} style={{
      background:disabled?'#C9C9C0':OLIVE, color:disabled?'#fff':GREEN, border:'none', fontFamily:'var(--font-body)',
      fontWeight:700, fontSize:small?14:16, letterSpacing:'-0.005em', padding:small?'11px 18px':'15px 22px',
      borderRadius:14, width:full?'100%':undefined, cursor:disabled?'default':'pointer',
      display:'inline-flex', alignItems:'center', justifyContent:'center', gap:8, ...style,
    }}>{children}</Tap>
  );
}
function SecondaryButton({ children, onClick, full, small, style }) {
  return (
    <Tap onClick={onClick} style={{
      background:GREEN, color:INK, border:'none', fontFamily:'var(--font-body)', fontWeight:700,
      fontSize:small?14:16, padding:small?'11px 18px':'15px 22px', borderRadius:14, width:full?'100%':undefined,
      cursor:'pointer', display:'inline-flex', alignItems:'center', justifyContent:'center', gap:8, ...style,
    }}>{children}</Tap>
  );
}
function GhostButton({ children, onClick, full, small, style }) {
  return (
    <Tap onClick={onClick} style={{
      background:'#fff', color:INK, border:'none', boxShadow:'inset 0 0 0 1px rgba(26,26,26,0.12)',
      fontFamily:'var(--font-body)', fontWeight:700, fontSize:small?14:15, padding:small?'11px 18px':'14px 22px',
      borderRadius:14, width:full?'100%':undefined, cursor:'pointer',
      display:'inline-flex', alignItems:'center', justifyContent:'center', gap:8, ...style,
    }}>{children}</Tap>
  );
}
function IconButton({ children, onClick, style, size=40 }) {
  return (
    <Tap onClick={onClick} style={{
      width:size, height:size, borderRadius:999, background:'#fff', boxShadow:'inset 0 0 0 1px rgba(26,26,26,.08)',
      border:'none', cursor:'pointer', display:'inline-flex', alignItems:'center', justifyContent:'center',
      color:INK, flex:'none', ...style,
    }}>{children}</Tap>
  );
}

function Stepper({ value, onInc, onDec, size='md' }) {
  const h=size==='sm'?30:36, w=size==='sm'?28:32;
  return (
    <div style={{ display:'inline-flex', alignItems:'center', background:'#fff', border:'1px solid rgba(26,26,26,.12)', borderRadius:999, padding:3 }}>
      <Tap onClick={onDec} style={{ width:w, height:h, borderRadius:999, border:'none', background:'transparent', color:INK, cursor:'pointer', display:'inline-flex', alignItems:'center', justifyContent:'center' }}><Icon.minus size={16} color={INK} /></Tap>
      <span style={{ minWidth:22, textAlign:'center', fontWeight:700, fontVariantNumeric:'tabular-nums', color:INK, fontSize:15 }}>{value}</span>
      <Tap onClick={onInc} style={{ width:w, height:h, borderRadius:999, border:'none', background:OLIVE, color:GREEN, cursor:'pointer', display:'inline-flex', alignItems:'center', justifyContent:'center' }}><Icon.plus size={16} color={GREEN} /></Tap>
    </div>
  );
}

function Wordmark({ color=INK, size=1 }) {
  return (
    <span style={{ display:'inline-flex', alignItems:'baseline', gap:5*size, color, lineHeight:1 }}>
      <span style={{ fontWeight:400, fontSize:13*size }}>the</span>
      <span style={{ fontWeight:900, fontSize:17*size, letterSpacing:'-0.02em' }}>AVOKADO</span>
      <span style={{ fontWeight:400, fontSize:13*size }}>bar</span>
    </span>
  );
}

function useScrollY(ref){
  const [y,setY]=useState(0);
  useEffect(()=>{ const el=ref.current; if(!el) return; const h=()=>setY(el.scrollTop); el.addEventListener('scroll',h,{passive:true}); return ()=>el.removeEventListener('scroll',h); },[ref]);
  return y;
}

// money fmt
const fmt = (n) => '₺' + Number(n).toLocaleString('tr-TR');

Object.assign(window.AvbX = window.AvbX || {}, {
  GREEN, GREEN_SOFT, INK, OLIVE, CREAM, PAPER,
  Icon, DishImage, Badge, Tap, PrimaryButton, SecondaryButton, GhostButton, IconButton,
  Stepper, Wordmark, useScrollY, fmt,
});
})();
