function parseInline(text: string) {
  const parts = text.split(/(\*\*[^*]+\*\*)/g);
  return parts.map((part, i) => {
    if (part.startsWith("**") && part.endsWith("**")) {
      return (
        <strong key={i}>{part.slice(2, -2)}</strong>
      );
    }
    return part;
  });
}

export function ChapterContent({ content }: { content: string }) {
  const blocks = content.trim().split(/\n\n+/);

  return (
    <>
      {blocks.map((block, index) => {
        const trimmed = block.trim();

        if (trimmed.startsWith("> ")) {
          const quote = trimmed.replace(/^>\s?/gm, "").trim();
          return (
            <blockquote key={index}>
              <p>{parseInline(quote)}</p>
            </blockquote>
          );
        }

        if (trimmed.startsWith("**") && trimmed.endsWith("**") && !trimmed.includes("\n")) {
          return (
            <p key={index} className="!mb-4 font-medium text-[var(--foreground)]">
              {parseInline(trimmed)}
            </p>
          );
        }

        if (trimmed.startsWith("- ")) {
          const items = trimmed.split("\n").map((line) => line.replace(/^- /, ""));
          return (
            <ul key={index}>
              {items.map((item, i) => (
                <li key={i}>{parseInline(item)}</li>
              ))}
            </ul>
          );
        }

        if (/^\d+\.\s/.test(trimmed)) {
          const items = trimmed.split("\n").map((line) => line.replace(/^\d+\.\s/, ""));
          return (
            <ol key={index}>
              {items.map((item, i) => (
                <li key={i}>{parseInline(item)}</li>
              ))}
            </ol>
          );
        }

        return <p key={index}>{parseInline(trimmed)}</p>;
      })}
    </>
  );
}
