/* global window */
// ============================================================
// PandaKit · 资源 manifest + 配饰 + 字体 + 背景 + DEMO_COLLECTION + STORY
// 重写（Sprint 2.J，2026-05-10）：
//   - 删 LOFI_FILTERS（用户：风格滤镜太难用）
//   - BG_PRESETS：删 glow 系，加 12 种纯色（湖绿/婴儿蓝/牛皮等）
//   - 新增 ACCESSORIES + ACCESSORY_REGISTRY（汗滴/腮红/泪水/怒气/青筋/闪光）
//   - STORY 用用户提供考据文章重写（含传播仪式观分析）
// ============================================================

window.SHELLS = []
window.FACES = []

// 精简到 4 个真可读字体（删毛笔楷/硬笔草/龙藏体——用户："丑+难读"）
// 默认 sans（思源黑 Bold） — meme 标准字体；其余 3 个为风格替代
const FONT_OPTIONS = [
	{ id: 'sans',   label_zh: '黑体',     label_en: 'Sans'   },  // Noto Sans SC 900（思源黑 Bold）— meme 标准
	{ id: 'yahei',  label_zh: '微软雅黑', label_en: 'YaHei'  },  // 系统字体兜底
	{ id: 'qingke', label_zh: '庞门正道', label_en: 'Qingke' },  // 厚重萌系
	{ id: 'kuaile', label_zh: '快乐体',   label_en: 'Kuaile' },  // 圆润可爱
]

// 已删 BG_PRESETS（用户：背景跟 shell 关系做不好，实在不行不要背景色）
// bg 永远 #FFFFFF 白色（让 panda 黑白美感纯粹）
const BG_PRESETS = []

// 已删配饰（用户：太丑，先移除）
const ACCESSORIES = []

// 中英文随机文本池（用户：分中英让随机时按语言抽）
const RANDOM_TEXTS_ZH = [
	// 用户提供
	'我勒个豆', '做大    做强', '区块链革命', '感谢大哥', '别哭，哭也没用',
	'犹豫就会白给', '拿去挥霍吧，别烦我', '管理员，麻烦把群主踢了',
	'对不起兄弟，太几把搞笑了', '冲！', '我emo了', '愣住', '卧槽？', '还得是你',
	// CT / 加密
	'all in 之后', '割完就跑', '到点了，上车', '真不能再多了',
	'这把肯定能赢', '别问，问就是 hodl', 'gm 兄弟', '兄弟稳住别慌',
	// TikTok / Z 世代中文
	'尊嘟假嘟', '我哭死', '真不熟', '真的栓 Q',
]
const RANDOM_TEXTS_EN = [
	// 用户提供
	'Pump it', 'We Are So Back', 'LFG',
	// CT
	'ngmi', 'wagmi', "it's so over", 'gm', '100x soon', 'cope harder',
	'this is the way', 'no cap', 'wen moon', "couldn't be me",
	'down bad', "let him cook",
	// TikTok / Gen Z
	'fr fr', 'mid', 'based', 'delulu', "it's giving...", 'main character',
	'absolute cinema', 'big if true', 'skill issue', 'touch grass',
	'rent free', 'the rizz', 'aura points', 'chef\'s kiss', 'no thoughts',
]
// 兼容旧 RANDOM_TEXTS（merged 兜底）
const RANDOM_TEXTS = [...RANDOM_TEXTS_ZH, ...RANDOM_TEXTS_EN]

// --- helpers ----------------------------------------------

async function loadManifests() {
	if (window.SHELLS?.length && window.FACES?.length) {
		return { shells: window.SHELLS, faces: window.FACES }
	}
	try {
		const [shells, faces] = await Promise.all([
			fetch('shells/manifest.json').then((r) => r.json()),
			fetch('faces/manifest.json').then((r) => r.json()),
		])
		window.SHELLS = shells
		window.FACES = faces
		return { shells, faces }
	} catch (e) {
		console.error('[loadManifests]', e)
		return { shells: [], faces: [] }
	}
}

const findShell = (id) => (window.SHELLS || []).find((s) => s.id === id)
const findFace  = (id) => (window.FACES  || []).find((f) => f.id === id)

const fontLabel = (lang, id) => {
	const f = FONT_OPTIONS.find((x) => x.id === id)
	return f ? (lang === 'en' ? f.label_en : f.label_zh) : id
}
const bgLabel = (lang, id) => {
	const b = BG_PRESETS.find((x) => x.id === id)
	return b ? (lang === 'en' ? b.label_en : b.label_zh) : id
}
const accessoryLabel = (lang, id) => {
	const a = ACCESSORIES.find((x) => x.id === id)
	return a ? (lang === 'en' ? a.label_en : a.label_zh) : id
}

// 用户：每次首次进入固定显示 Hearts shell + face-53 + "Love 4 U"（必带 4 的英文）
const DEFAULT_TRAITS = {
	shellId: 'shell-image_2026-05-10_06-58-16',  // Hearts (抱心款)
	faceId: 'face-53',
	text: 'Love 4 U',
	font: 'sans',
	bg: '#FFFFFF',
	faceRotation: 0,
	faceFlipX: false,
	faceFlipY: false,
	accessories: [],
}

// DEMO_COLLECTION — 12 个新 trait demo
const DEMO_COLLECTION = [
	{ id: 'c1',  caption: '我嘞个去',     tilt: -3.2, traits: { shellId: 'shell-bust',     faceId: 'face-3', text: '我嘞个去',     font: 'kuaile', bg: '#FFFFFF',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: [] } },
	{ id: 'c2',  caption: '不想动',       tilt:  2.5, traits: { shellId: 'shell-head-cry', faceId: 'face-5', text: '不想动',       font: 'sans',   bg: '#FAF7F2',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: [] } },
	{ id: 'c3',  caption: '哎呀压力大',   tilt: -1.8, traits: { shellId: 'shell-bust',     faceId: 'face-3', text: '哎呀压力大',   font: 'qingke', bg: '#B3E5FC',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['sweat-drop'] } },
	{ id: 'c4',  caption: '禁止内卷',     tilt:  3.7, traits: { shellId: 'shell-bust',     faceId: 'face-1', text: '禁止内卷',     font: 'kuaile', bg: '#F8D9D9',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['anger-bolt', 'veins'] } },
	{ id: 'c5',  caption: '哈哈',         tilt: -2.4, traits: { shellId: 'shell-bust',     faceId: 'face-3', text: '哈哈哈哈',     font: 'qingke', bg: '#FBE8BB',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['blush'] } },
	{ id: 'c6',  caption: '求求了',       tilt:  1.5, traits: { shellId: 'shell-head-cry', faceId: 'face-4', text: '求求你了',     font: 'hand',   bg: '#FFFFFF',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['tears'] } },
	{ id: 'c7',  caption: '你说得对',     tilt: -3.5, traits: { shellId: 'shell-bust',     faceId: 'face-5', text: '你说得对',     font: 'sans',   bg: '#D9E5E1',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: [] } },
	{ id: 'c8',  caption: '甲方又来了',   tilt:  2.8, traits: { shellId: 'shell-bust',     faceId: 'face-2', text: '再改一版',     font: 'qingke', bg: '#FFD0E0',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['sweat-drop'] } },
	{ id: 'c9',  caption: '宝贝晚安',     tilt: -1.2, traits: { shellId: 'shell-bust',     faceId: 'face-3', text: '宝贝晚安',     font: 'qingke', bg: '#FFD0E0',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['blush', 'sparkle'] } },
	{ id: 'c10', caption: '兄弟过分了',   tilt:  3.0, traits: { shellId: 'shell-bust',     faceId: 'face-2', text: '兄弟你这话过分了', font: 'sans', bg: '#E8E3F1', faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['veins'] } },
	{ id: 'c11', caption: '上班使我快乐', tilt: -2.0, traits: { shellId: 'shell-head-cry', faceId: 'face-3', text: '上班使我快乐', font: 'kuaile', bg: '#FFE4B5',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['blush'] } },
	{ id: 'c12', caption: '去你的',       tilt:  1.8, traits: { shellId: 'shell-bust',     faceId: 'face-1', text: '去你的',       font: 'kuaile', bg: '#C8E6C9',  faceRotation: 0, faceFlipX: false, faceFlipY: false, accessories: ['anger-bolt'] } },
]

// STORY 中英双版本（节点对象同时含 zh + en，story.jsx 按 lang 取）
const STORY = [
	{
		year: '2007',
		zh: { title: '崔成国《三金》定格', body: '韩国演员崔成国在电影《三金》中打赢游戏后站起来大笑——这张定格日后被网友扒出，成为熊猫头的"灵魂面孔"，网络昵称"金馆长"。' },
		en: { title: 'The Choi Sung-kook Laugh', body: 'In the Korean film "Three Kim Sisters," actor Choi Sung-kook stands up after winning a game and lets out a wide, uninhibited laugh. Years later, that single freeze-frame becomes the soul of every panda head meme — known online as "Director Kim."' },
	},
	{
		year: '2010s',
		zh: { title: '埃及芝士广告', body: '一位厨师为好友带了"熊猫"芝士菜，好友说不需要芝士。熊猫主角登场——可怜兮兮地盯着他，配上洗脑 BGM 一夜成神。这只埃及广告里的熊猫，后来成为表情包的视觉模板。' },
		en: { title: 'The Egyptian Cheese Commercial', body: 'In an Egyptian "Panda Cheese" ad, a deeply offended panda silently stares down anyone who refuses cheese. The pathetic-yet-menacing vibe + earworm music makes it viral. This panda becomes the visual template for everything that follows.' },
	},
	{
		year: '~2014',
		zh: { title: '暗黑破坏神吧 · 创世', body: '贴吧用户「忏悔恶魔」最早把芝士广告里的熊猫头扣下来；「毕须博须A」P 上金馆长的脸，奠定"上熊猫 + 下字幕（最好带个离谱翻译）"经典格式。与暴漫合流后一夜爆红。' },
		en: { title: 'The Diablo Forum Genesis', body: 'On Baidu Tieba\'s Diablo II forum, user "Repenting Demon" first cuts the panda silhouette out of the cheese ad. Then "Bixu Boxu A" Photoshops Director Kim\'s face onto it. Boom: the canonical format is born — panda body, real face, an absurd caption underneath.' },
	},
	{
		year: '2017',
		zh: { title: '微信表情商店化', body: '微信开放表情商店让熊猫头进入大众视野，替代了过去的「兔斯基」「张全蛋」，成为通用情绪货币。' },
		en: { title: 'WeChat Sticker Store', body: 'WeChat opens its sticker marketplace and panda head goes mainstream. It replaces older mascots like Tuzki and Zhang Quan-dan and becomes the default emotional currency for an entire generation of Chinese chatters.' },
	},
	{
		year: '2019',
		zh: { title: 'B 站沙雕动画兴起', body: '以「阿幕降临」为开创者，熊猫头从静态表情升级为动画主角——剧情完整、风格鲜明，开启二创动画新世代。' },
		en: { title: 'Bilibili "Sand Sculpture" Animation', body: 'Pioneered by creator "Amu Jianglin," panda heads jump from static stickers into full animated shorts. The genre is gleefully called "sand sculpture animation" — cheap, absurd, viral. A whole new content format is born.' },
	},
	{
		year: '2021',
		zh: { title: '抖音 + 长篇文学改编', body: '抖音崛起带动熊猫头转向网络文学改编，《蛊真人》《绝命回溯》等热门 IP 被搬上屏幕。' },
		en: { title: 'TikTok-Era Long-Form', body: 'As Douyin (Chinese TikTok) explodes, creators start using panda heads to act out entire web novels — episode after episode of "Gu Real Person" and other epic IPs, all narrated through cropped faces with dramatic captions.' },
	},
	{
		year: '2023',
		zh: { title: '虾仁宇宙 · 大梦千年', body: '虾仁饭动画《大梦千年》让熊猫头热度达到新高峰。矩阵账号 50 万粉以上超 20 个，破亿播放短剧 18+ 部，抖音累计播放 679 亿次。' },
		en: { title: 'The Shrimp-Rice Universe', body: 'Animation studio "Shrimp Rice" releases "Dream of a Thousand Years" — peak panda head. Their 20+ accounts each pass 500K followers; 18 series cross a billion views; cumulative Douyin plays hit 67.9 billion.' },
	},
	{
		year: '2024+',
		zh: { title: '跨语言传播', body: '随着中文互联网的输出，熊猫头开始出现在 Discord、Telegram、X 等非中文社区，成为一种 universal vibe。' },
		en: { title: 'It Goes Global', body: 'Panda head crosses the language barrier. Discord servers, Telegram groups, crypto Twitter — it\'s used by people who don\'t speak a word of Mandarin. The vibe just translates.' },
	},
]

const STORY_INTERLUDES = {
	whyHot: {
		zh: { title: '为什么火 · 仪式观', body: '传播学的「仪式观」（Carey 1989）解释了熊猫头的生命力：好的传播不只是单向传递信息，而是邀请人参与、维持氛围与参与感。「忏悔恶魔」贡献了原始素材，「毕须博须A」让普通人发现"我也能 P 一张"——从此每个发表情包的人都成了仪式的一部分。' },
		en: { title: 'Why It Stuck · The Ritual View', body: 'James Carey\'s "ritual view of communication" (1989) explains panda head\'s longevity: great communication isn\'t one-way broadcasting — it invites participation. "Repenting Demon" provided the raw material; "Bixu Boxu A" showed regular users they could Photoshop their own. Once everyone could remix, the meme stopped being a meme and became a ritual.' },
	},
	culture: {
		zh: { title: '反精致 · 真诚的信号', body: '熊猫头是一种**反精致**的视觉语言。在所有平台都越来越像高级杂志的今天，它故意保留 2014 年的描边、抠图边缘、像素感——这种"粗糙"反而成了真诚的信号。表情中立、语境万能：自嘲、愤怒、狂欢、强颜欢笑，一张图全包。' },
		en: { title: 'Anti-Aesthetic · Signal of Sincerity', body: 'Panda head is anti-design. While every other platform is sliding toward magazine-grade polish, panda head deliberately keeps the 2014-era pixelation, jagged edges, and JPEG compression — and that **roughness is the point**. It signals "I am being sincere, not branded." It carries self-deprecation, rage, joy, fake smiles — one image, every emotion.' },
	},
	endless: {
		zh: { title: '电子包浆 · 长青之道', body: '一则埃及广告 × 一个韩国演员的笑脸 × 中文互联网的集体创造 = 亚文化的 Meme 之神。或许它的画质会越来越糊（电子包浆），但作为中国表情包之王的地位短时间内依然无可撼动。毕竟，没有什么是发一个熊猫头解决不了的，如果有，那就发两个。' },
		en: { title: 'Digital Patina · Why It Will Outlive Us', body: 'One Egyptian commercial × one Korean actor\'s laugh × the entire Chinese internet remixing it = a meme deity. Each compression cycle adds "digital patina" — the image gets blurrier and the legend gets stronger. Nothing solves a Chinese internet conversation faster than a panda head. And if one doesn\'t work, send two.' },
	},
}

window.FONT_OPTIONS = FONT_OPTIONS
window.BG_PRESETS = BG_PRESETS
window.ACCESSORIES = ACCESSORIES
window.RANDOM_TEXTS = RANDOM_TEXTS
window.RANDOM_TEXTS_ZH = RANDOM_TEXTS_ZH
window.RANDOM_TEXTS_EN = RANDOM_TEXTS_EN
window.DEFAULT_TRAITS = DEFAULT_TRAITS
window.loadManifests = loadManifests
window.findShell = findShell
window.findFace = findFace
window.fontLabel = fontLabel
window.bgLabel = bgLabel
window.accessoryLabel = accessoryLabel
window.DEMO_COLLECTION = DEMO_COLLECTION
window.STORY = STORY
window.STORY_INTERLUDES = STORY_INTERLUDES
