import { html } from 'lit';
import { property } from 'lit/decorators.js';
import { marked } from 'marked';
import { p as purify, N as NO_HTML_TAGS_OPTIONS } from '../../chunks/index-CA_aKopU.js';
import { a as TailwindStyledElement, s as safeCustomElement } from '../../chunks/index-DluTRl1F.js';

const style = ":host {\n    display: flex\n}";

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
let LuksoMarkdown = class extends TailwindStyledElement(style) {
  constructor() {
    super(...arguments);
    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 = purify.sanitize(this.value, NO_HTML_TAGS_OPTIONS);
      }
      const processedMarkdown = this.preprocessNestedLists(content);
      marked.setOptions({
        gfm: true,
        // GitHub Flavored Markdown
        breaks: false
        // Don't convert line breaks to <br>
      });
      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>
    `;
  }
};
__decorateClass([
  property({ type: String })
], LuksoMarkdown.prototype, "value", 2);
__decorateClass([
  property({ type: Boolean, attribute: "is-pre", reflect: true })
], LuksoMarkdown.prototype, "isPre", 2);
__decorateClass([
  property({ type: String, attribute: "prose-classes", reflect: true })
], LuksoMarkdown.prototype, "proseClasses", 2);
__decorateClass([
  property({ type: String, attribute: "custom-style", reflect: true })
], LuksoMarkdown.prototype, "customStyle", 2);
__decorateClass([
  property({ type: Boolean, attribute: "strip-html-tags" })
], LuksoMarkdown.prototype, "stripHtmlTags", 2);
LuksoMarkdown = __decorateClass([
  safeCustomElement("lukso-markdown")
], LuksoMarkdown);

export { LuksoMarkdown };
