import type { FlatTemplatable } from "tempstream";
import { TempstreamJSX } from "tempstream";
import type {
	ComponentToHTMLArgs,
	ExtractStructuredComponentArgumentsParsed,
	JDDContext,
} from "@sealcode/jdd";
import { Component, ComponentArguments } from "@sealcode/jdd";

const component_arguments = {
	content: new ComponentArguments.Markdown(),
} as const;

export class Tekst extends Component<typeof component_arguments> {
	getArguments() {
		return component_arguments;
	}

	getTitle(
		_: JDDContext,
		args: ExtractStructuredComponentArgumentsParsed<typeof component_arguments>
	) {
		return args.content.slice(0, 20) + "..." || null;
	}

	toHTML({
		args: { content },
		classes,
		jdd_context: { render_markdown, language },
		index,
	}: ComponentToHTMLArgs<typeof component_arguments>): FlatTemplatable {
		return (
			<div class={["tekst markdown", ...classes]} style={`--jdd-index: ${index}`}>
				{render_markdown(language, content)}
			</div>
		);
	}

	getHeadings(
		_context: JDDContext,
		args: ExtractStructuredComponentArgumentsParsed<{
			readonly content: ComponentArguments.Markdown;
		}>
	) {
		return this.getArguments().content.extractHeadings(args.content);
	}
}
