// 公共组件加载器
(function() {
    // 章节数据 - 只需要在这里维护
    const chaptersData = [
        { id: 1, title: "第一章：洛兰" },
        { id: 2, title: "第二章：洛兰深处" },
        { id: 3, title: "第三章：幽暗密林" },
        { id: 4, title: "第四章：幽暗密林深处" },
        { id: 5, title: "第五章：雷鸣废墟" },
        { id: 6, title: "第六章：格拉卡" },
        { id: 7, title: "第七章：烈焰格拉卡" },
        { id: 8, title: "第八章：冰霜幽暗密林" },
        { id: 9, title: "第九章：转职之路" },
        { id: 10, title: "第十章：暗黑雷鸣废墟" },
        { id: 11, title: "第十一章：剑魂转职仪式" },
        { id: 12, title: "第十二章：西海岸" },
        { id: 13, title: "第十三章：龙人之塔" },
        { id: 14, title: "第十四章：人偶玄关" },
        { id: 15, title: "第十五章：石巨人塔" },
        { id: 16, title: "第十六章：黑暗玄廊" },
        { id: 17, title: "第十七章：城主宫殿" },
        { id: 18, title: "第十八章：番外·悬空城" },
        { id: 19, title: "第十九章：天帷巨兽·神殿外围" },
        { id: 20, title: "第二十章：树精丛林" },
        { id: 21, title: "第二十一章：炼狱" },
        { id: 22, title: "第二十二章：西海岸的闲暇" },
        { id: 23, title: "第二十三章：极昼" },
        { id: 24, title: "第二十四章：第一脊椎" },
        { id: 25, title: "第二十五章：赫顿玛尔的准备" },
        { id: 26, title: "第二十六章：第二脊椎" },
        { id: 27, title: "第二十七章：重逢的温柔" },
        { id: 28, title: "第二十八章：暗精灵的委托" },
        { id: 29, title: "第二十九章：阿法利亚营地" },
        { id: 30, title: "第三十章：浅栖之地" },
        { id: 31, title: "第三十一章：蜘蛛洞穴" },
        { id: 32, title: "第三十二章：克伦特的委托" },
        { id: 33, title: "第三十三章：暗精灵墓地·左翼守卫" }
    ];

    // 获取当前章节ID
    function getCurrentChapterId() {
        const path = window.location.pathname;
        const match = path.match(/chapter-(\d+)\.html/);
        return match ? parseInt(match[1]) : 0;
    }

    // 渲染侧边栏
    function renderSidebar() {
        const sidebarContent = document.getElementById('sidebarChapters');
        if (!sidebarContent) return;

        const currentId = getCurrentChapterId();

        sidebarContent.innerHTML = chaptersData.map(chapter => {
            const isActive = chapter.id === currentId ? 'active' : '';
            return `
                <a href="chapter-${chapter.id}.html" class="sidebar-chapter ${isActive}">
                    <span class="chapter-num">${chapter.id}</span>
                    <span class="chapter-name">${chapter.title}</span>
                </a>
            `;
        }).join('');
    }

    // 页面加载完成后渲染
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', renderSidebar);
    } else {
        renderSidebar();
    }
})();