
struct TagLinkCommand: CommandProcessor {

    static let commandType: CommandType = .tagLink

    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
    }

    /**
     Format: `![tag](<tagId>)`
     */
    func process(_ arguments: [String], markdown: Substring) -> String {
        guard arguments.count == 1 else {
            invalid(markdown)
            return ""
        }
        let tagId = arguments[0]

        guard let tag = content.tag(tagId) else {
            results.missing(tag: tagId, source: "Tag link command")
            return ""
        }

        let localized = tag.localized(in: language)
        let url = tag.absoluteUrl(in: language)
        let title = localized.name
        let description = localized.linkPreview.description ?? ""
        let image = localized.linkPreview.image.map {
            let size = content.settings.pages.pageLinkImageSize
            let imageSet = $0.imageSet(width: size, height: size, language: language)
            results.require(imageSet: imageSet)
            return imageSet
        }

        return RelatedPageLink(
            title: title,
            description: description,
            url: url,
            image: image)
            .content
    }
}
