import { safeCustomElement } from "../../shared/safe-custom-element.js";
import { TailwindStyledElement } from "../../shared/tailwind-element/index.js";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.127.0/helpers/decorate.js";
import { NO_HTML_TAGS_OPTIONS } from "../lukso-sanitize/index.js";
import style_default from "./style.css?inline.js";
import { html } from "lit";
import { property } from "lit/decorators.js";
import DOMPurify from "dompurify";
import { marked } from "marked";
//#region src/components/lukso-markdown/index.ts
var LuksoMarkdown = class LuksoMarkdown extends TailwindStyledElement(style_default) {
	constructor(..._args) {
		super(..._args);
		this.value = "";
		this.isPre = false;
		this.proseClasses = "prose prose-base prose-inherit";
		this.customStyle = "";
		this.stripHtmlTags = false;
	}
	/**
	* Convert markdown to HTML using marked library
	*/
	convertMarkdownToHtml() {
		if (!this.value) return "";
		try {
			let content = this.value;
			if (this.stripHtmlTags) content = DOMPurify.sanitize(this.value, NO_HTML_TAGS_OPTIONS);
			const processedMarkdown = this.preprocessNestedLists(content);
			marked.setOptions({
				gfm: true,
				breaks: false
			});
			return marked(processedMarkdown);
		} catch (error) {
			console.error("Error parsing markdown:", error);
			return "";
		}
	}
	/**
	* Preprocess markdown to handle nested lists with various indentation
	* by normalizing to CommonMark-compliant 4-space indentation
	*/
	preprocessNestedLists(markdown) {
		const lines = markdown.split("\n");
		const result = [];
		for (const line of lines) {
			const listMatch = line.match(/^(\s*)((?:\d+\.)|[-*+])\s+(.*)$/);
			if (listMatch) {
				const originalIndent = listMatch[1];
				const marker = listMatch[2];
				const content = listMatch[3];
				let nestingLevel = 0;
				if (originalIndent.length >= 2) {
					nestingLevel = Math.max(1, Math.floor(originalIndent.length / 4));
					if (originalIndent.length >= 2 && originalIndent.length < 4) nestingLevel = 1;
				}
				const newIndent = "    ".repeat(nestingLevel);
				result.push(`${newIndent}${marker} ${content}`);
			} else result.push(line);
		}
		return result.join("\n");
	}
	render() {
		const htmlContent = this.convertMarkdownToHtml();
		return html`
      <lukso-sanitize
        html-content="<div class='flex flex-col ${this.proseClasses}' style='${this.customStyle}'>${htmlContent}</div>"
        ?is-pre="${this.isPre}"
        class="w-full"
      ></lukso-sanitize>
    `;
	}
};
__decorate([property({ type: String })], LuksoMarkdown.prototype, "value", void 0);
__decorate([property({
	type: Boolean,
	attribute: "is-pre",
	reflect: true
})], LuksoMarkdown.prototype, "isPre", void 0);
__decorate([property({
	type: String,
	attribute: "prose-classes",
	reflect: true
})], LuksoMarkdown.prototype, "proseClasses", void 0);
__decorate([property({
	type: String,
	attribute: "custom-style",
	reflect: true
})], LuksoMarkdown.prototype, "customStyle", void 0);
__decorate([property({
	type: Boolean,
	attribute: "strip-html-tags"
})], LuksoMarkdown.prototype, "stripHtmlTags", void 0);
LuksoMarkdown = __decorate([safeCustomElement("lukso-markdown")], LuksoMarkdown);
//#endregion
export { LuksoMarkdown };
