// pantry-icons.jsx — icon set + food chips. Two icon styles supported:
//   'line'   — outlined stroke
//   'filled' — solid filled
// Used in nav, actions, buttons, status indicators.
// For food items, the option is line/filled/emoji — see <FoodChip>.

const IconWrap = ({ size = 22, children }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" style={{ display: 'block' }}>
    {children}
  </svg>
);

// ─── Generic icons ───────────────────────────────────────────
function Icon({ name, size = 22, style = 'line', color = 'currentColor', stroke = 1.75 }) {
  const sw = stroke;
  const c = color;
  const filled = style === 'filled';

  const paths = {
    home: filled
      ? <path d="M11.3 2.6 3.5 9.1c-.3.3-.5.7-.5 1.1V20c0 .6.4 1 1 1h5v-7h6v7h5c.6 0 1-.4 1-1v-9.8c0-.4-.2-.8-.5-1.1L12.7 2.6c-.4-.3-1-.3-1.4 0Z" fill={c}/>
      : <path d="m3 10.5 9-7.5 9 7.5V20a1 1 0 0 1-1 1h-5v-7h-6v7H4a1 1 0 0 1-1-1v-9.5Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    box: filled
      ? <><path d="M3 7.4 12 3l9 4.4v9.2L12 21l-9-4.4V7.4Z" fill={c}/><path d="m3 7.5 9 4.5 9-4.5M12 12v9" stroke="#fff" strokeWidth={sw} strokeLinejoin="round" opacity=".55"/></>
      : <><path d="M3 7.4 12 3l9 4.4v9.2L12 21l-9-4.4V7.4Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/><path d="m3 7.5 9 4.5 9-4.5M12 12v9" stroke={c} strokeWidth={sw} strokeLinejoin="round"/></>,
    chef: filled
      ? <><path d="M6 9.5A4.5 4.5 0 0 1 12 6a4.5 4.5 0 0 1 6 3.5c1.7.4 3 1.9 3 3.7 0 1.7-1.1 3-2.7 3.5H6.7C5.1 16.2 4 14.9 4 13.2c0-1.8 1.3-3.3 3-3.7Z" fill={c}/><path d="M7 17h10v2a2 2 0 0 1-2 2H9a2 2 0 0 1-2-2v-2Z" fill={c} opacity=".75"/></>
      : <><path d="M6.5 16.7A3.5 3.5 0 0 1 4 13.2c0-1.9 1.4-3.4 3.3-3.6A4.5 4.5 0 0 1 12 6a4.5 4.5 0 0 1 4.7 3.6c1.9.2 3.3 1.7 3.3 3.6 0 1.5-1 2.9-2.5 3.5" stroke={c} strokeWidth={sw} strokeLinecap="round"/><path d="M7 17v2a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2v-2H7Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/></>,
    cart: filled
      ? <><path d="M3 4h2.2l2.3 11.5a2 2 0 0 0 2 1.6h7.8a2 2 0 0 0 2-1.5L21 8H6" fill={c}/><circle cx="10" cy="20" r="1.5" fill={c}/><circle cx="17" cy="20" r="1.5" fill={c}/></>
      : <><path d="M3 4h2.2l2.3 11.5a2 2 0 0 0 2 1.6h7.8a2 2 0 0 0 2-1.5L21 8H6" stroke={c} strokeWidth={sw} strokeLinejoin="round" strokeLinecap="round"/><circle cx="10" cy="20" r="1.4" stroke={c} strokeWidth={sw}/><circle cx="17" cy="20" r="1.4" stroke={c} strokeWidth={sw}/></>,
    calendar: filled
      ? <><rect x="3" y="5" width="18" height="16" rx="2.5" fill={c}/><rect x="3" y="5" width="18" height="5" rx="2.5" fill={c} opacity=".7"/><rect x="7" y="3" width="2" height="4" rx="1" fill={c}/><rect x="15" y="3" width="2" height="4" rx="1" fill={c}/></>
      : <><rect x="3" y="5" width="18" height="16" rx="2.5" stroke={c} strokeWidth={sw}/><path d="M3 10h18M8 3v4M16 3v4" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    user: filled
      ? <><circle cx="12" cy="8.5" r="3.5" fill={c}/><path d="M4.5 20a7.5 7.5 0 0 1 15 0H4.5Z" fill={c}/></>
      : <><circle cx="12" cy="8.5" r="3.5" stroke={c} strokeWidth={sw}/><path d="M4.5 20a7.5 7.5 0 0 1 15 0" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    users: filled
      ? <><circle cx="9" cy="8.5" r="3.2" fill={c}/><path d="M2.5 19a6.5 6.5 0 0 1 13 0H2.5Z" fill={c}/><circle cx="17" cy="9" r="2.6" fill={c} opacity=".7"/><path d="M15 14.4A5.5 5.5 0 0 1 22 19h-4.5" fill={c} opacity=".7"/></>
      : <><circle cx="9" cy="8.5" r="3.2" stroke={c} strokeWidth={sw}/><path d="M2.5 19a6.5 6.5 0 0 1 13 0" stroke={c} strokeWidth={sw} strokeLinecap="round"/><circle cx="17.5" cy="9" r="2.4" stroke={c} strokeWidth={sw}/><path d="M16 14.5A5.5 5.5 0 0 1 21.5 19" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    plus: <path d="M12 5v14M5 12h14" stroke={c} strokeWidth={2.2} strokeLinecap="round"/>,
    search: <><circle cx="11" cy="11" r="6.5" stroke={c} strokeWidth={sw}/><path d="m20 20-3.6-3.6" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    chevR: <path d="m9 6 6 6-6 6" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"/>,
    chevL: <path d="m15 6-6 6 6 6" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"/>,
    chevD: <path d="m6 9 6 6 6-6" stroke={c} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"/>,
    close: <path d="M6 6l12 12M18 6 6 18" stroke={c} strokeWidth={2.2} strokeLinecap="round"/>,
    bell: filled
      ? <><path d="M6 16V11a6 6 0 1 1 12 0v5l1.4 2H4.6L6 16Z" fill={c}/><path d="M10 19a2 2 0 0 0 4 0" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>
      : <><path d="M6 16V11a6 6 0 1 1 12 0v5l1.4 2H4.6L6 16Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/><path d="M10 19a2 2 0 0 0 4 0" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    camera: filled
      ? <><path d="M9 5l1.2-2h3.6L15 5h4a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4Z" fill={c}/><circle cx="12" cy="13" r="3.6" fill="#fff" opacity=".9"/></>
      : <><path d="M9 5l1.2-2h3.6L15 5h4a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/><circle cx="12" cy="13" r="3.6" stroke={c} strokeWidth={sw}/></>,
    barcode: <><path d="M4 6v12M7 6v12M10 6v12M14 6v12M17 6v12M20 6v12" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    pen: <path d="M4 20l4-1L19.5 7.5a2.1 2.1 0 0 0-3-3L5 16l-1 4Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    clock: <><circle cx="12" cy="12" r="8.5" stroke={c} strokeWidth={sw}/><path d="M12 8v4l3 2" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
    fire: filled
      ? <path d="M12 3s5 4 5 9a5 5 0 0 1-10 0c0-1.5.6-2.6 1.5-3.5C8.5 9 8 7 9 5c1 .5 2 1 2.5 2 .5-1.5 0-3 .5-4Z" fill={c}/>
      : <path d="M12 3s5 4 5 9a5 5 0 0 1-10 0c0-1.5.6-2.6 1.5-3.5C8.5 9 8 7 9 5c1 .5 2 1 2.5 2 .5-1.5 0-3 .5-4Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    leaf: filled
      ? <path d="M20 4S8 4 5 10c-2 4-1 8 1 9 0-3 3-7 8-9 0 0-4 4-4 9 6 0 11-5 11-11l-1-4Z" fill={c}/>
      : <path d="M20 4S8 4 5 10c-2 4-1 8 1 9 0-3 3-7 8-9 0 0-4 4-4 9 6 0 11-5 11-11l-1-4Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    check: <path d="m5 12.5 4.5 4.5L19 7.5" stroke={c} strokeWidth={2.2} strokeLinecap="round" strokeLinejoin="round"/>,
    spark: filled
      ? <path d="M12 3l1.6 4.7L18 9l-4.4 1.3L12 15l-1.6-4.7L6 9l4.4-1.3L12 3Z" fill={c}/>
      : <path d="M12 3l1.6 4.7L18 9l-4.4 1.3L12 15l-1.6-4.7L6 9l4.4-1.3L12 3Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    settings: <><circle cx="12" cy="12" r="3" stroke={c} strokeWidth={sw}/><path d="M19.4 13.5a7.7 7.7 0 0 0 0-3l2-1.5-2-3.4-2.4.8a8 8 0 0 0-2.6-1.5L14 2h-4l-.4 2.9a8 8 0 0 0-2.6 1.5l-2.4-.8-2 3.4 2 1.5a7.7 7.7 0 0 0 0 3l-2 1.5 2 3.4 2.4-.8a8 8 0 0 0 2.6 1.5L10 22h4l.4-2.9a8 8 0 0 0 2.6-1.5l2.4.8 2-3.4-2-1.5Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/></>,
    flame: filled
      ? <path d="M12 22a6 6 0 0 0 5-9c-1 1-2 1-3 0 0-4-2-7-5-9 1 4-1 5-3 8a6 6 0 0 0 6 10Z" fill={c}/>
      : <path d="M12 22a6 6 0 0 0 5-9c-1 1-2 1-3 0 0-4-2-7-5-9 1 4-1 5-3 8a6 6 0 0 0 6 10Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/>,
    sparkle: <path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8" stroke={c} strokeWidth={sw} strokeLinecap="round"/>,
    dots: <><circle cx="5" cy="12" r="1.6" fill={c}/><circle cx="12" cy="12" r="1.6" fill={c}/><circle cx="19" cy="12" r="1.6" fill={c}/></>,
    trash: filled
      ? <><path d="M5 6h14l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6Z" fill={c}/><path d="M9 4h6v2H9zM3 6h18" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>
      : <><path d="M5 6h14l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6Z" stroke={c} strokeWidth={sw} strokeLinejoin="round"/><path d="M9 4h6v2H9zM3 6h18M10 11v6M14 11v6" stroke={c} strokeWidth={sw} strokeLinecap="round"/></>,
  };

  return <IconWrap size={size}>{paths[name] || null}</IconWrap>;
}

// ─── FoodChip — renders a food item icon in 3 styles ────────
function FoodChip({ item, style = 'emoji', size = 44, palette }) {
  const radius = size / 2;
  const fontSize = Math.round(size * 0.46);
  const monoSize = Math.round(size * 0.38);

  if (style === 'emoji') {
    return (
      <div style={{
        width: size, height: size, borderRadius: radius,
        background: hexToSoft(item.tone),
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
        fontSize, lineHeight: 1,
      }}>
        <span style={{ filter: palette.isDark ? 'saturate(0.85)' : 'none' }}>{item.emoji}</span>
      </div>
    );
  }
  if (style === 'filled') {
    return (
      <div style={{
        width: size, height: size, borderRadius: radius,
        background: item.tone, color: '#fff',
        display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
        fontFamily: 'Geist Mono, ui-monospace, monospace',
        fontSize: monoSize, fontWeight: 600,
        boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.18), 0 1px 2px rgba(0,0,0,0.08)',
      }}>{item.mono}</div>
    );
  }
  // line
  return (
    <div style={{
      width: size, height: size, borderRadius: radius,
      background: hexToSoft(item.tone),
      border: `1.5px solid ${item.tone}`,
      color: deepTone(item.tone, palette),
      display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
      fontFamily: 'Geist Mono, ui-monospace, monospace',
      fontSize: monoSize, fontWeight: 600,
    }}>{item.mono}</div>
  );
}

function hexToSoft(hex) {
  // simple soft tint — overlay 80% white
  const { r, g, b } = parseHex(hex);
  const mix = (c) => Math.round(c * 0.18 + 255 * 0.82);
  return `rgb(${mix(r)}, ${mix(g)}, ${mix(b)})`;
}
function deepTone(hex, palette) {
  const { r, g, b } = parseHex(hex);
  const dim = (c) => Math.round(c * 0.55);
  return palette.isDark ? hex : `rgb(${dim(r)}, ${dim(g)}, ${dim(b)})`;
}
function parseHex(hex) {
  const h = hex.replace('#', '');
  return { r: parseInt(h.substring(0, 2), 16), g: parseInt(h.substring(2, 4), 16), b: parseInt(h.substring(4, 6), 16) };
}

// ─────────────────────────────────────────────────────────────
// Profile photos — stored locally per user id (data URL). Persists on the
// device; the initial (derived from the name) is the universal fallback.
// ─────────────────────────────────────────────────────────────
const PHOTO_LS_PREFIX = 'pantrywise.photo.';
function getProfilePhoto(userId) {
  if (!userId) return null;
  try { return localStorage.getItem(PHOTO_LS_PREFIX + userId) || null; } catch (e) { return null; }
}
function setProfilePhoto(userId, dataUrl) {
  if (!userId) return;
  try {
    if (dataUrl) localStorage.setItem(PHOTO_LS_PREFIX + userId, dataUrl);
    else localStorage.removeItem(PHOTO_LS_PREFIX + userId);
  } catch (e) {}
}
function initialFromName(name) {
  const n = (name || '').trim();
  return n ? n.charAt(0).toUpperCase() : '?';
}

// UserAvatar — round avatar showing the user's photo if set, else their
// first-name initial on a colored background. Used in headers and member lists.
function UserAvatar({ palette, name, userId, photo, tone, size = 38, onClick, gradient }) {
  const pic = photo || getProfilePhoto(userId);
  const bg = gradient
    ? `linear-gradient(135deg, ${palette.primary}, ${palette.amber})`
    : (tone || palette.primary);
  const base = {
    width: size, height: size, borderRadius: size / 2,
    display: 'flex', alignItems: 'center', justifyContent: 'center',
    cursor: onClick ? 'pointer' : 'default', flexShrink: 0, overflow: 'hidden',
    color: '#fff', fontWeight: 600, fontFamily: 'Geist, sans-serif',
    fontSize: Math.round(size * 0.38),
  };
  if (pic) {
    return (
      <div onClick={onClick} style={{ ...base, background: palette.cardSub }}>
        <img src={pic} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
      </div>
    );
  }
  return <div onClick={onClick} style={{ ...base, background: bg }}>{initialFromName(name)}</div>;
}

Object.assign(window, {
  Icon, FoodChip, hexToSoft, deepTone,
  UserAvatar, getProfilePhoto, setProfilePhoto, initialFromName,
});
