/* global React, Sparkle, Heart, Star, Diamond, useLang */

const FOOTER_BRAND_PATH_LANGS = new Set([
  'en', 'ko', 'ja', 'de', 'fr', 'es', 'pt', 'hi', 'id', 'vi', 'th', 'tr', 'it', 'pl', 'nl',
]);

function readWcsBusinessInfo(lang) {
  const fallback = lang === 'ko'
    ? {
        title: '사업자 정보',
        description: '국세청 사업자등록증 기준 공개 사업자 정보',
        fields: [
          { label: '상호', value: '우친소' },
          { label: '사업자등록번호', value: '203-16-65151' },
          { label: '대표자', value: '윤다민' },
          { label: '사업장 소재지', value: '경기 수원시 영통구 삼성로 253' },
          { label: '업태', value: '도매 및 소매업 / 정보통신업' },
          { label: '종목', value: '전자상거래 소매업, 포털 및 기타 인터넷 정보 매개 서비스업, 컴퓨터 프로그래밍 서비스업, 미디어콘텐츠창작업' },
          { label: '이메일', value: 'luvwcs@gmail.com' },
        ],
      }
    : {
        title: 'Business Information',
        description: 'Public business information based on the Certificate of Business Registration',
        fields: [
          { label: 'Name of Business', value: 'WCS (Woochinso)' },
          { label: 'Business Taxpayer ID Number', value: '203-16-65151' },
          { label: 'Representative', value: 'Yoon Damin' },
          { label: 'Business Address', value: '253 Samsung-ro, Yeongtong-gu, Suwon-si, Gyeonggi-do, Republic of Korea' },
          { label: 'Business Type', value: 'Wholesale and retail trade / Information and communication' },
          { label: 'Business Item', value: 'Electronic commerce via internet; Portals and other internet information media service activities' },
          { label: 'Email', value: 'luvwcs@gmail.com' },
        ],
      };

  const el = document.getElementById('wcs-business-info');
  if (!el) return fallback;

  try {
    const parsed = JSON.parse(el.textContent || '{}');
    if (!parsed || !Array.isArray(parsed.fields)) return fallback;
    return parsed;
  } catch (_) {
    return fallback;
  }
}

function BusinessInfoPanel({ info }) {
  return (
    <section className="business-footer" aria-labelledby="business-footer-title">
      <div className="business-footer-head">
        <h2 className="business-footer-title" id="business-footer-title">{info.title}</h2>
        <p className="business-footer-desc">{info.description}</p>
      </div>
      <dl className="business-footer-grid">
        {info.fields.map((field) => (
          <div key={field.label}>
            <dt>{field.label}</dt>
            <dd>{field.value}</dd>
          </div>
        ))}
      </dl>
    </section>
  );
}

function Footer() {
  const { t, lang } = useLang();
  const info = readWcsBusinessInfo(lang);
  const moaiLocale = FOOTER_BRAND_PATH_LANGS.has(lang) ? lang : 'en';
  const profileLocale = ['en', 'ko', 'ja'].includes(lang) ? lang : 'en';
  const officialLinks = [
    { href: 'https://backendwcs.com/', label: 'WCS | Woochinso (우친소)' },
    { href: `https://moai.page/${moaiLocale}/moai`, label: 'MOAI (Moai / 모아이)' },
    { href: `https://backendwcs.com/${profileLocale}/yoon-damin`, label: 'Yoon Damin (윤다민)' },
    { href: 'https://blog.backendwcs.com/', label: 'Korea Guide' },
  ];

  return (
    <footer className="footer">
      <div className="footer-mark">{t('footer_tagline')}</div>
      <div>© 2024 WOOCHINSO · MADE IN KOREA</div>
      <nav aria-label="Official WCS properties" style={{ margin: '12px 0 16px', display: 'flex', flexWrap: 'wrap', gap: 12, justifyContent: 'center' }}>
        {officialLinks.map((link) => (
          <a key={link.href} href={link.href} target="_blank" rel="noopener noreferrer">
            {link.label}
          </a>
        ))}
      </nav>
      <BusinessInfoPanel info={info} />
      <div style={{ marginTop: 14, display: 'flex', gap: 8, justifyContent: 'center' }}>
        <Sparkle size={3} className="twinkle" />
        <Heart size={3} />
        <Star size={3} />
        <Diamond size={3} />
        <Sparkle size={3} className="twinkle" style={{ animationDelay: '-1s' }} />
      </div>
    </footer>
  );
}

Object.assign(window, { Footer });
