"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageHandler = exports.TableHandler = exports.TableRowHandler = exports.BrHandler = exports.DivHandler = exports.UnorderedListHandler = exports.OrderedListHandler = exports.GenericHandler = exports.CodeHandler = exports.FormattingHandler = exports.ParagraphHandler = exports.HeaderHandler = exports.LinkHandler = exports.TextNodeHandler = exports.NodeHandler = void 0;
/**
 * Base handler class implementing the Chain of Responsibility pattern for HTML node processing.
 * Each concrete handler decides whether it can process a node or should pass it to the next handler.
 */
class NodeHandler {
    next = null;
    /**
     * Sets up the next handler in the chain
     */
    setNext(handler) {
        this.next = handler;
        return handler;
    }
    /**
     * Delegates processing to the next handler in the chain
     */
    handleNext(node, visitor) {
        if (this.next) {
            return this.next.handle(node, visitor);
        }
        return '';
    }
}
exports.NodeHandler = NodeHandler;
/**
 * Handles text nodes, converting them to markdown text
 */
class TextNodeHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.TEXT_NODE) {
            return visitor.visitText(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.TextNodeHandler = TextNodeHandler;
/**
 * Handles anchor elements, converting them to markdown links
 */
class LinkHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'a') {
            return visitor.visitLink(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.LinkHandler = LinkHandler;
/**
 * Handles header elements (h1-h6), converting them to markdown headers
 */
class HeaderHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType !== Node.ELEMENT_NODE) {
            return this.handleNext(node, visitor);
        }
        const tagName = node.tagName.toLowerCase();
        const headerMatch = tagName.match(/^h([1-6])$/);
        if (headerMatch) {
            const headerLevel = parseInt(headerMatch[1], 10);
            return visitor.visitHeader(node, headerLevel);
        }
        return this.handleNext(node, visitor);
    }
}
exports.HeaderHandler = HeaderHandler;
/**
 * Handles paragraph elements, converting them to markdown paragraphs
 */
class ParagraphHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType !== Node.ELEMENT_NODE) {
            return this.handleNext(node, visitor);
        }
        if (node.tagName.toLowerCase() === 'p') {
            return visitor.visitParagraph(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.ParagraphHandler = ParagraphHandler;
/**
 * Handles text formatting elements (b, strong, i, em), converting them to markdown formatting
 */
class FormattingHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType !== Node.ELEMENT_NODE) {
            return this.handleNext(node, visitor);
        }
        const element = node;
        const tagName = element.tagName.toLowerCase();
        const formattingTags = ['b', 'strong', 'i', 'em', 'span'];
        if (formattingTags.includes(tagName) &&
            !['a', 'code', 'pre'].includes(element.parentElement?.tagName.toLowerCase() || '')) {
            return visitor.visitFormatting(element);
        }
        return this.handleNext(node, visitor);
    }
}
exports.FormattingHandler = FormattingHandler;
/**
 * Handles code elements, converting them to markdown code blocks or inline code
 */
class CodeHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType !== Node.ELEMENT_NODE) {
            return this.handleNext(node, visitor);
        }
        if (node.tagName.toLowerCase() === 'code' ||
            node.tagName.toLowerCase() === 'pre') {
            return visitor.visitCode(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.CodeHandler = CodeHandler;
/**
 * Fallback handler for any HTML elements not handled by other specific handlers
 */
class GenericHandler extends NodeHandler {
    handle(node, visitor) {
        return visitor.visitGeneric(node);
    }
}
exports.GenericHandler = GenericHandler;
/**
 * Handles ordered list elements, converting them to markdown ordered lists
 */
class OrderedListHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'ol') {
            const items = Array.from(node.childNodes)
                .filter((child) => child.nodeType === Node.ELEMENT_NODE &&
                child.tagName.toLowerCase() === 'li')
                .map((item, index) => `${index + 1}. ${visitor.visitGeneric(item)}`)
                .join('\n');
            return items + '\n';
        }
        return this.handleNext(node, visitor);
    }
}
exports.OrderedListHandler = OrderedListHandler;
/**
 * Handles unordered list elements, converting them to markdown unordered lists
 */
class UnorderedListHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'ul') {
            const items = Array.from(node.childNodes)
                .filter((child) => child.nodeType === Node.ELEMENT_NODE &&
                child.tagName.toLowerCase() === 'li')
                .map((item) => `- ${visitor.visitGeneric(item)}`)
                .join('\n');
            return items + '\n';
        }
        return this.handleNext(node, visitor);
    }
}
exports.UnorderedListHandler = UnorderedListHandler;
/**
 * Handles div elements, converting them to markdown paragraphs
 */
class DivHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'div') {
            return visitor.visitDiv(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.DivHandler = DivHandler;
/**
 * Handles br elements, converting them to markdown newlines
 */
class BrHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'br') {
            return visitor.visitBr();
        }
        return this.handleNext(node, visitor);
    }
}
exports.BrHandler = BrHandler;
/**
 * Handles table row elements, converting them to markdown table rows
 */
class TableRowHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'tr') {
            return visitor.visitTableRow(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.TableRowHandler = TableRowHandler;
/**
 * Handles table elements, converting them to markdown tables
 */
class TableHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'table') {
            return visitor.visitTable(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.TableHandler = TableHandler;
/**
 * Handles image elements, converting them to markdown images
 */
class ImageHandler extends NodeHandler {
    handle(node, visitor) {
        if (node.nodeType === Node.ELEMENT_NODE &&
            node.tagName.toLowerCase() === 'img') {
            return visitor.visitImage(node);
        }
        return this.handleNext(node, visitor);
    }
}
exports.ImageHandler = ImageHandler;
//# sourceMappingURL=handlers.js.map
