// retoor <retoor@molodetz.nl>

import { contentRenderer } from "./ContentRenderer.js";
import { CodeBlock } from "./CodeBlock.js";
import { MentionInput } from "./MentionInput.js";
import { EmojiPicker } from "./EmojiPicker.js";

export class ContentEnhancer {
    constructor() {
        this.initContentRenderer();
        this.initEmojiPickers();
        this.initMentionInputs();
        this.initImageFallbacks();
    }

    initContentRenderer() {
        contentRenderer.ready.then(() => {
            document.querySelectorAll("[data-render]").forEach((el) => {
                contentRenderer.applyTo(el);
                el.querySelectorAll("img:not(.avatar-img)").forEach((img) => {
                    img.dataset.lightbox = "";
                });
            });
            contentRenderer.highlightAll();
            this.enhanceCodeBlocks();
        });
    }

    enhanceCodeBlocks(root = document) {
        root.querySelectorAll(".rendered-content pre").forEach((pre) => {
            if (pre.querySelector("code")) {
                CodeBlock.enhance(pre, { lineNumbers: false });
            }
        });
    }

    initEmojiPickers() {
        document.querySelectorAll(".comment-form textarea, .emoji-picker-target").forEach((textarea) => {
            if (textarea.dataset.emojiInitialized) return;
            textarea.dataset.emojiInitialized = "true";

            const btn = document.createElement("button");
            btn.type = "button";
            btn.className = "emoji-toggle-btn";
            btn.title = "Add emoji";
            btn.innerHTML = "\u{1F600}";

            const picker = new EmojiPicker(textarea);
            btn.addEventListener("click", () => picker.toggle());

            const form = textarea.closest(".comment-form, .messages-input-area");
            const actions = form && form.querySelector(".comment-form-actions");
            if (actions) {
                actions.style.position = "relative";
                actions.appendChild(picker.wrapper);
                actions.insertBefore(btn, actions.firstChild);
                return;
            }
            if (form && form.classList.contains("messages-input-area")) {
                const sendBtn = form.querySelector(".messages-send-btn");
                if (sendBtn) form.insertBefore(btn, sendBtn);
                else form.appendChild(btn);
            }
        });
    }

    initMentionInputs() {
        document.querySelectorAll("[data-mention]").forEach((el) => {
            if (el.dataset.mentionInitialized) return;
            el.dataset.mentionInitialized = "true";
            new MentionInput(el);
        });
    }

    initImageFallbacks() {
        document.querySelectorAll(".image-fallback").forEach((img) => {
            img.addEventListener("error", () => {
                const container = img.closest(".news-card-image, .news-detail-image") || img.parentElement;
                if (container) container.style.display = "none";
            });
        });
    }
}
