
/* global wp */

// get post title and content from Gutenberg blocks
function getPostContentFromGutenberg() {
   
    if (typeof wp === "undefined" || !wp.data)
        return { postHeading: "", postContent: "" };

    const { select } = wp.data;

    const postHeading = select("core/editor").getEditedPostAttribute("title") || "";
    const blocks = select("core/block-editor").getBlocks();

    // Recursive function to handle nested blocks (e.g., inside quotes)

    const postContent = blocks.map(renderBlock).filter(Boolean).join("\n\n");

    return { postHeading, postContent };
}
/**
 * Get Classic block content from Gutenberg editor iframe
 */
function getClassicBlockFromGutenbergIframe() {
    
    let postContent = "";
    // Access the iframe
    const iframe = document.querySelector('iframe[name="editor-canvas"]');
    if (iframe && iframe.contentDocument) {
        // Look for the classic/freeform block
        const classicDiv = iframe.contentDocument.querySelector('.wp-block-freeform, .wp-block-classic');
        if (classicDiv) {
            postContent = classicDiv.innerHTML.trim();
            }
    }

    // Get the post title from main DOM

    return { postContent };
}


function renderBlock(block) {
    switch (block.name) {
        case "core/paragraph":
            return `<p>${block.attributes.content || ""}</p>`;

        case "core/heading":
            const level = block.attributes.level || 2;
            return `<h${level}>${block.attributes.content || ""}</h${level}>`;

        case "core/list":
            // Ordered = true → <ol>, else <ul>
            const tag = block.attributes.ordered ? "ol" : "ul";
            const listItems = (block.innerBlocks || [])
                .map(renderBlock)
                .join("");
            return `<${tag}>${listItems}</${tag}>`;

        case "core/list-item":
            // List item may have nested content (paragraphs or another list)
            const listInner = (block.innerBlocks || [])
                .map(renderBlock)
                .join("");
            return `<li>${block.attributes.content || ""}${listInner}</li>`;

        case "core/quote":
            const inner =
                (block.innerBlocks || [])
                    .map(renderBlock)
                    .join("") ||
                block.attributes.value ||
                block.attributes.content ||
                "";
            const citation = block.attributes.citation
                ? `<cite>${block.attributes.citation}</cite>`
                : "";
            return `<blockquote>${inner}${citation}</blockquote>`;

        case "core/image":
            const { url, alt } = block.attributes;
            return `<img src="${url}" alt="${alt || ""}" />`;

        default:
            if (block.innerBlocks && block.innerBlocks.length > 0) {
                return block.innerBlocks.map(renderBlock).join("");
            }
            return "";
    }
}


// get post content from classic editor
function getPostContentFromClassic() {
 
    let postContent = "";
    let postHeading = "";

    // Get post heading safely
    const heading = document.getElementById("title");
    postHeading = heading?.value || "";

    // Check if TinyMCE is initialized
    if (window.tinymce && window.tinymce.activeEditor) {
        try {
            postContent = window.tinymce.activeEditor.getContent() || "";
        } catch (e) {
            postContent = "";
        }
    } else {
        // Fallback: read directly from textarea
        const textarea = document.getElementById("content");
        postContent = textarea?.value || "";
    }

    return { postHeading, postContent };
}


function isClassicEditorActive() {
    return (
        document.body.classList.contains('wp-editor') ||
        document.getElementById('content') !== null
    );
}
export { getPostContentFromClassic, getPostContentFromGutenberg, getClassicBlockFromGutenbergIframe, isClassicEditorActive };