import { Component, createSignal, onMount } from 'solid-js';
import { useLocation } from '@solidjs/router';

// 导入组件以注册自定义元素
import './components';
import { Article } from './components';

const App: Component = () => {
  const location = useLocation();
  const [currentPath, setCurrentPath] = createSignal('');

  onMount(() => {
    // 根据路由加载对应的 markdown 文件
    let path = decodeURIComponent(location.pathname.slice(1));
    if (!path) {
      path = '/content/index.md';
    } else if (!path.startsWith('/content/')) {
      path = `/content/${path}`;
    }
    // 确保有 .md 扩展名
    if (!path.endsWith('.md')) {
      path = `${path}.md`;
    }
    setCurrentPath(path);
  });

  return (
    <div class="min-h-screen bg-gray-50">
      <header class="bg-white shadow">
        <div class="max-w-4xl mx-auto px-4 py-4">
          <h1 class="text-2xl font-bold text-gray-900">TTRPG Tools</h1>
        </div>
      </header>
      <main class="max-w-4xl mx-auto px-4 py-8">
        <Article src={currentPath()} />
      </main>
    </div>
  );
};

export default App;
