/**
 * 根据 config.js 应用站点名称到页面标题、meta、标题元素，并动态生成 PWA manifest
 * 需在 config.js 之后加载
 */
(function () {
  var name = window.SITE_NAME || '萌芽导航';
  var shortName = window.SITE_SHORT_NAME || '萌芽';
  var desc = window.SITE_DESCRIPTION || (name + ' - 轻量好用的网址导航');

  document.title = name;
  var metaDesc = document.querySelector('meta[name="description"]');
  if (metaDesc) metaDesc.setAttribute('content', desc);
  var metaApp = document.querySelector('meta[name="apple-mobile-web-app-title"]');
  if (metaApp) metaApp.setAttribute('content', name);

  var siteTitle = document.getElementById('site-title');
  if (siteTitle) siteTitle.textContent = '\u2728 ' + name;
  var adminTitle = document.getElementById('admin-title');
  if (adminTitle) adminTitle.textContent = name + '-管理后台';
  var offlineTitle = document.getElementById('offline-title');
  if (offlineTitle) {
    document.title = name + ' - 离线';
    offlineTitle.textContent = name + ' - 离线';
  }
  var offlineLogo = document.getElementById('offline-logo');
  if (offlineLogo) offlineLogo.textContent = shortName.charAt(0);

  var glassOpacity = window.SITE_GLASS_OPACITY;
  if (typeof glassOpacity === 'number' && glassOpacity >= 0 && glassOpacity <= 1) {
    document.documentElement.style.setProperty('--site-glass-opacity', String(glassOpacity));
  }

  var isMobile = window.matchMedia('(max-width: 767px)').matches;
  var bgImages = isMobile
    ? window.SITE_MOBILE_BACKGROUND_IMAGES
    : window.SITE_DESKTOP_BACKGROUND_IMAGES;
  if (!Array.isArray(bgImages) || bgImages.length === 0) {
    bgImages = isMobile ? window.SITE_DESKTOP_BACKGROUND_IMAGES : window.SITE_MOBILE_BACKGROUND_IMAGES;
  }
  if (Array.isArray(bgImages) && bgImages.length > 0) {
    var imgUrl = bgImages[Math.floor(Math.random() * bgImages.length)];
    var applyBg = function () {
      if (!document.body) return;
      var bgEl = document.createElement('div');
      bgEl.className = 'site-bg';
      bgEl.setAttribute('aria-hidden', 'true');
      bgEl.style.backgroundImage = 'url(' + imgUrl + ')';
      document.body.insertBefore(bgEl, document.body.firstChild);
    };
    if (document.body) {
      applyBg();
    } else {
      document.addEventListener('DOMContentLoaded', applyBg);
    }
  }

  var manifest = {
    name: name,
    short_name: shortName,
    description: desc,
    lang: 'zh-CN',
    dir: 'ltr',
    id: '/',
    start_url: '/?source=pwa',
    scope: '/',
    display: 'standalone',
    display_override: ['standalone', 'minimal-ui', 'browser'],
    orientation: 'portrait-primary',
    theme_color: '#10b981',
    background_color: '#f8fafc',
    categories: ['productivity', 'utilities'],
    prefer_related_applications: false,
    icons: [
      { src: '/logo.png', type: 'image/png', sizes: '2048x2048', purpose: 'any' },
      { src: '/logo.png', type: 'image/png', sizes: '2048x2048', purpose: 'maskable' },
      { src: '/favicon.ico', type: 'image/x-icon', sizes: '16x16 24x24 32x32 48x48 64x64', purpose: 'any' }
    ]
  };
  var blob = new Blob([JSON.stringify(manifest)], { type: 'application/json' });
  var url = URL.createObjectURL(blob);
  var link = document.querySelector('link[rel="manifest"]');
  if (link) link.href = url;
  else {
    link = document.createElement('link');
    link.rel = 'manifest';
    link.href = url;
    document.head.appendChild(link);
  }
})();
