
struct SvgCommand: CommandProcessor {

    static let commandType: CommandType = .svg

    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
    }

    func process(_ arguments: [String], markdown: Substring) -> String {
        guard arguments.count == 5 else {
            invalid(markdown)
            return ""
        }

        guard let x = Int(arguments[1]),
              let y = Int(arguments[2]),
              let partWidth = Int(arguments[3]),
              let partHeight = Int(arguments[4]) else {
            invalid(markdown)
            return ""
        }

        let imageId = arguments[0]

        guard let image = content.image(imageId) else {
            results.missing(file: imageId, source: "SVG command")
            return ""
        }
        guard image.type == .svg else {
            invalid(markdown)
            return ""
        }

        return PartialSvgImage(
            imagePath: image.absoluteUrl,
            altText: image.localized(in: language),
            x: x,
            y: y,
            width: partWidth,
            height: partHeight)
        .content
    }
}
