import type { Signal } from '@preact/signals'
import { render, RenderOptions } from ':src/patch_gfm.ts'

export type MarkdownTheme = 'light' | 'dark' | 'auto'
export function Markdown(
	{ children, theme, options }: {
		children?: Signal<string> | string
		theme?: Signal<MarkdownTheme> | MarkdownTheme
		options?: RenderOptions
	},
) {
	return (
		<div
			class='markdown-body'
			data-color-mode={typeof theme === 'string'
				? theme
				: theme?.value ?? 'auto'}
			data-light-theme='light'
			data-dark-theme='dark'
			dangerouslySetInnerHTML={{
				__html: render(
					typeof children === 'string' ? children : children?.value ?? '',
					options,
				),
			}}
		>
		</div>
	)
}
