
struct GalleryBlock: BlockLineProcessor {

    static let blockId: ContentBlock = .gallery

    let content: Content

    let results: PageGenerationResults

    let language: ContentLanguage

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

    private var imageWidth: Int {
        content.settings.pages.contentWidth
    }

    func process(_ lines: [String], markdown: Substring) -> String {
        var images = [FileResource]()

        for line in lines {
            let imageId = line.trimmed
            guard !imageId.isEmpty else { continue }
            guard let image = content.image(imageId) else {
                results.missing(file: imageId, source: "Route block")
                continue
            }
            images.append(image)
        }
        guard let firstImage = images.first else { return "" }

        let imageSets = images.map {
            $0.imageSet(width: imageWidth, height: imageWidth, language: language)
        }
        imageSets.forEach(results.require)
        let id = firstImage.identifier.replacingOccurrences(of: ".", with: "-")
        let gallery = ImageGallery(id: id, images: imageSets, standalone: true)
        results.require(footer: gallery.standaloneFooter)
        results.require(headers: .swiperJs, .swiperCss)
        return gallery.content
    }
}
