(function () {
  const base = 'assets/mock/';
  const media = {
    products: {
      mask: base + 'product-mask.png',
      earbuds: base + 'product-earbuds.png',
      noodle: base + 'product-noodle.png',
      sunscreen: base + 'product-sunscreen.png',
      coffee: base + 'product-coffee.png',
      airFryer: base + 'product-air-fryer.png',
      yoga: base + 'product-yoga-pants.png',
      storage: base + 'product-storage.png'
    },
    covers: {
      mask: base + 'cover-mask-v3.png',
      maskFinal: base + 'cover-mask-final.png',
      noodle: base + 'cover-noodle.png',
      sunscreen: base + 'cover-sunscreen.png',
      coffee: base + 'cover-coffee.png',
      earbuds: base + 'cover-earbuds.png',
      yoga: base + 'cover-yoga.png',
      airFryer: base + 'cover-air-fryer.png'
    },
    people: {
      linxi: base + 'person-linxi.png',
      ajie: base + 'person-ajie.png',
      aqiang: base + 'person-aqiang.png',
      xiaosu: base + 'person-xiaosu.png'
    },
    scenes: {
      bedroom: base + 'scene-bedroom.png',
      bathroom: base + 'scene-bathroom.png',
      living: base + 'scene-living.png',
      kitchen: base + 'scene-kitchen.png',
      office: base + 'scene-office.png',
      cafe: base + 'scene-cafe.png',
      street: base + 'scene-night-street.png',
      tabletop: base + 'scene-tabletop.png'
    }
  };

  function clean(text) {
    return String(text || '').replace(/\s+/g, '').toLowerCase();
  }

  function contextText(el) {
    const card = el.closest('[data-name], [data-product], [data-project], [data-cat], [data-asset-kind], [data-scene-type]');
    const bits = [
      el.textContent,
      card?.dataset.name,
      card?.dataset.product,
      card?.dataset.project,
      card?.dataset.cat,
      card?.dataset.assetKind,
      card?.dataset.sceneType,
      card?.querySelector('.product-name, .card-name, .asset-name, .prod-name, strong')?.textContent
    ];
    return clean(bits.filter(Boolean).join(' '));
  }

  function productFor(t) {
    if (/蓝牙|耳机|earbud|南卡/.test(t)) return media.products.earbuds;
    if (/速食|牛肉面|泡面|面条|noodle/.test(t)) return media.products.noodle;
    if (/防晒|sunscreen/.test(t)) return media.products.sunscreen;
    if (/咖啡|冻干|coffee/.test(t)) return media.products.coffee;
    if (/空气炸锅|airfryer|小熊/.test(t)) return media.products.airFryer;
    if (/瑜伽裤|露露|yoga/.test(t)) return media.products.yoga;
    if (/收纳|storage|北欧/.test(t)) return media.products.storage;
    if (/面膜|补水|玻尿酸|mask|透真/.test(t)) return media.products.mask;
    return '';
  }

  function coverFor(t) {
    if (/蓝牙|耳机|earbud|南卡/.test(t)) return media.covers.earbuds;
    if (/速食|牛肉面|泡面|面条|noodle/.test(t)) return media.covers.noodle;
    if (/防晒|sunscreen/.test(t)) return media.covers.sunscreen;
    if (/咖啡|冻干|coffee/.test(t)) return media.covers.coffee;
    if (/空气炸锅|airfryer|小熊/.test(t)) return media.covers.airFryer;
    if (/瑜伽裤|露露|yoga/.test(t)) return media.covers.yoga;
    if (/v1|final|已完成|5\/5|成片|敷面膜|化妆台/.test(t)) return media.covers.maskFinal;
    if (/面膜|补水|玻尿酸|mask|透真|场1|场2|场3/.test(t)) return media.covers.mask;
    return '';
  }

  function personFor(t) {
    if (/阿杰|通勤男|男青年/.test(t)) return media.people.ajie;
    if (/阿强|健身男|健身/.test(t)) return media.people.aqiang;
    if (/小苏|文艺女|短发|阿楠|同事|小七|学生女|闺蜜|妈妈|王姐|豆豆/.test(t)) return media.people.xiaosu;
    if (/林夕|主播|都市白领|主角|女性|女生/.test(t)) return media.people.linxi;
    if (/小宇|李爷爷|男性|男/.test(t)) return media.people.ajie;
    return '';
  }

  function sceneFor(t) {
    if (/卧室|床头|bedroom/.test(t)) return media.scenes.bedroom;
    if (/浴室|梳妆台|bathroom/.test(t)) return media.scenes.bathroom;
    if (/客厅|living/.test(t)) return media.scenes.living;
    if (/厨房|中岛|kitchen/.test(t)) return media.scenes.kitchen;
    if (/办公室|办公桌|会议室|office|深夜办公/.test(t)) return media.scenes.office;
    if (/咖啡店|窗边|cafe/.test(t)) return media.scenes.cafe;
    if (/街景|夜|street/.test(t)) return media.scenes.street;
    if (/平台|套图|布景|tabletop/.test(t)) return media.scenes.tabletop;
    return '';
  }

  function imageFor(el) {
    if (el.classList.contains('missing') || el.querySelector('.spinner, .fail-icon')) return '';
    const t = contextText(el);
    const card = el.closest('.asset-card, .asset-card-2, .proj-card, .product-card, .video-card');
    if (el.id === 'ed-canvas') return media.covers.maskFinal;
    if (el.id === 'sb-main-img' || el.id === 'vd-main-img') return media.covers.mask;
    if (el.matches('.video-thumb')) {
      if (card?.dataset.videoId === 'v2') return media.products.mask;
      if (card?.dataset.videoId === 'v3') return media.covers.maskFinal;
    }
    if (el.matches('.card-thumb, .proj-thumb, .video-thumb') || card?.classList.contains('video')) return coverFor(t);
    if (el.matches('.product-thumb, .prod-thumb, .pl-thumb')) return productFor(t);
    if (el.matches('.m-thumb')) return personFor(t);
    if (el.matches('.thumb-2, .asset-thumb')) {
      return personFor(t) || sceneFor(t) || productFor(t) || coverFor(t);
    }
    if (el.matches('.sb-scene-thumb, .sb-history-thumb, .vd-history-thumb')) return coverFor(t) || media.covers.mask;
    return productFor(t) || personFor(t) || sceneFor(t) || coverFor(t);
  }

  function applyOne(el) {
    if (!el || el.dataset.mockMediaApplied === '1') return;
    const src = imageFor(el);
    if (!src) return;
    el.dataset.mockMediaApplied = '1';
    el.classList.add('has-mock-media');
    el.style.setProperty('--mock-media-url', `url("${src}")`);
    el.style.backgroundImage = `url("${src}")`;
  }

  function apply() {
    document.querySelectorAll('.placeholder, #ed-canvas').forEach(applyOne);
  }

  function boot() {
    apply();
    const mo = new MutationObserver(() => apply());
    mo.observe(document.body, { childList: true, subtree: true });
    window.MockMedia = { apply, media };
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', boot, { once: true });
  } else {
    boot();
  }
})();
