/* shared.jsx — 图标、状态栏、浣熊头像、通用小组件 */
/* global React */

const RACCOON_SRC = (typeof window !== 'undefined' && window.__resources && window.__resources.raccoon) || 'assets/office-raccoon-logo.svg';

/* ---------- 线性图标 (lucide 风格, stroke 1.5) ---------- */
function Icon({ d, size = 22, sw = 1.6, fill, children, ...rest }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={fill || 'none'}
      stroke="currentColor" strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"
      {...rest}>
      {children || <path d={d} />}
    </svg>
  );
}
const Ic = {
  menu: (p) => <Icon {...p}><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="15" y2="18"/></Icon>,
  mic: (p) => <Icon {...p}><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 11a7 7 0 0 0 14 0"/><line x1="12" y1="18" x2="12" y2="22"/></Icon>,
  arrowUp: (p) => <Icon {...p}><line x1="12" y1="19" x2="12" y2="5"/><polyline points="6 12 12 6 18 12"/></Icon>,
  chevR: (p) => <Icon {...p}><polyline points="9 18 15 12 9 6"/></Icon>,
  chevL: (p) => <Icon {...p}><polyline points="15 18 9 12 15 6"/></Icon>,
  chevD: (p) => <Icon {...p}><polyline points="6 9 12 15 18 9"/></Icon>,
  back: (p) => <Icon {...p}><line x1="19" y1="12" x2="5" y2="12"/><polyline points="12 19 5 12 12 5"/></Icon>,
  search: (p) => <Icon {...p}><circle cx="11" cy="11" r="7"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></Icon>,
  gear: (p) => <Icon {...p}><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></Icon>,
  plus: (p) => <Icon {...p}><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></Icon>,
  x: (p) => <Icon {...p}><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></Icon>,
  cloud: (p) => <Icon {...p}><path d="M18 10h-1.26A8 8 0 1 0 9 20h9a5 5 0 0 0 0-10z"/></Icon>,
  laptop: (p) => <Icon {...p}><rect x="3" y="4" width="18" height="12" rx="2"/><line x1="2" y1="20" x2="22" y2="20"/></Icon>,
  chart: (p) => <Icon {...p}><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></Icon>,
  route: (p) => <Icon {...p}><circle cx="6" cy="19" r="3"/><circle cx="18" cy="5" r="3"/><path d="M9 19h5a4 4 0 0 0 4-4V8"/></Icon>,
  slides: (p) => <Icon {...p}><rect x="3" y="4" width="18" height="13" rx="2"/><line x1="12" y1="17" x2="12" y2="21"/><line x1="8" y1="21" x2="16" y2="21"/></Icon>,
  pen: (p) => <Icon {...p}><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4z"/></Icon>,
  doc: (p) => <Icon {...p}><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></Icon>,
  folder: (p) => <Icon {...p}><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"/></Icon>,
  books: (p) => <Icon {...p}><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"/></Icon>,
  cal: (p) => <Icon {...p}><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></Icon>,
  heart: (p) => <Icon {...p}><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.6l-1-1a5.5 5.5 0 0 0-7.8 7.8l1 1L12 21l7.8-7.6 1-1a5.5 5.5 0 0 0 0-7.8z"/></Icon>,
  mail: (p) => <Icon {...p}><rect x="2" y="4" width="20" height="16" rx="2"/><polyline points="2 6 12 13 22 6"/></Icon>,
  check: (p) => <Icon {...p}><polyline points="20 6 9 17 4 12"/></Icon>,
  checkCircle: (p) => <Icon {...p}><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></Icon>,
  clock: (p) => <Icon {...p}><circle cx="12" cy="12" r="9"/><polyline points="12 7 12 12 15 14"/></Icon>,
  upload: (p) => <Icon {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></Icon>,
  sparkle: (p) => <Icon {...p}><path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z"/></Icon>,
  plug: (p) => <Icon {...p}><path d="M12 22v-5"/><path d="M9 8V2"/><path d="M15 8V2"/><path d="M18 8v3a4 4 0 0 1-4 4h-4a4 4 0 0 1-4-4V8z"/></Icon>,
};

/* ---------- iOS 状态栏 ---------- */
function StatusBar({ time = '9:41' }) {
  return (
    <div className="statusbar">
      <div className="sb-time">{time}</div>
      <div className="sb-right">
        {/* 信号 */}
        <svg className="sb-icon" width="18" height="12" viewBox="0 0 18 12" fill="currentColor">
          <rect x="0" y="8" width="3" height="4" rx="1"/><rect x="5" y="5" width="3" height="7" rx="1"/>
          <rect x="10" y="2.5" width="3" height="9.5" rx="1"/><rect x="15" y="0" width="3" height="12" rx="1"/>
        </svg>
        {/* wifi */}
        <svg className="sb-icon" width="17" height="12" viewBox="0 0 17 12" fill="currentColor">
          <path d="M8.5 2.2c2.7 0 5.2 1 7 2.8l-1.5 1.6a7.8 7.8 0 0 0-11 0L1.5 5A10 10 0 0 1 8.5 2.2z"/>
          <path d="M8.5 6.1c1.6 0 3.1.6 4.2 1.7l-1.6 1.6a4.2 4.2 0 0 0-5.2 0L4.3 7.8A6 6 0 0 1 8.5 6.1z"/>
          <circle cx="8.5" cy="10.4" r="1.4"/>
        </svg>
        {/* 电池 */}
        <svg className="sb-icon" width="26" height="12" viewBox="0 0 26 12" fill="none">
          <rect x="0.5" y="0.5" width="22" height="11" rx="3" stroke="currentColor" opacity="0.4"/>
          <rect x="2" y="2" width="17" height="8" rx="1.6" fill="currentColor"/>
          <rect x="24" y="4" width="1.6" height="4" rx="0.8" fill="currentColor" opacity="0.5"/>
        </svg>
      </div>
    </div>
  );
}

/* ---------- 浣熊头像 ---------- */
function Raccoon({ size = 'sm', brand = false }) {
  return (
    <span className={`raccoon ${size} ${brand ? 'brand' : ''}`}>
      <img src={RACCOON_SRC} alt="小浣熊" />
    </span>
  );
}

/* ---------- 屏幕外壳 ---------- */
function Screen({ tinted = false, time, children }) {
  return (
    <div className={`screen ${tinted ? 'tinted' : ''}`}>
      <StatusBar time={time} />
      {children}
      <div className="home-indicator" />
    </div>
  );
}

/* ---------- 波形 (静态随机高度) ---------- */
function Wave({ n = 30 }) {
  const hs = React.useMemo(
    () => Array.from({ length: n }, (_, i) => {
      const base = Math.sin(i * 0.7) * 0.5 + 0.5;
      return 6 + Math.round(base * 18 + (i % 3) * 3);
    }),
    [n],
  );
  return (
    <div className="wave">
      {hs.map((h, i) => <i key={i} style={{ height: h }} />)}
    </div>
  );
}

Object.assign(window, { Icon, Ic, StatusBar, Raccoon, Screen, Wave, RACCOON_SRC });
