import Foundation
import Ink

final class PageContentParser {

    private let language: ContentLanguage

    private let content: Content

    private let results: PageGenerationResults

    // MARK: Markdown handlers

    private let code: MarkdownCodeProcessor

    private let headlines: MarkdownHeadlineProcessor

    private let image: MarkdownImageProcessor

    private let link: MarkdownLinkProcessor

    init(content: Content, language: ContentLanguage, results: PageGenerationResults) {
        self.content = content
        self.results = results
        self.language = language

        self.code = .init(content: content, results: results, language: language)
        self.headlines = .init(content: content, results: results, language: language)
        self.image = .init(content: content, results: results, language: language)
        self.link = .init(content: content, results: results, language: language)
    }

    func generatePage(from content: String) -> String {
        let parser = MarkdownParser(modifiers: [
            Modifier(target: .images, closure: image.process),
            Modifier(target: .codeBlocks, closure: code.process),
            Modifier(target: .links, closure: link.process),
            Modifier(target: .html, closure: image.html.process),
            Modifier(target: .headings, closure: headlines.process)
        ])
        return parser.html(from: content)
    }
}
