import { Validator } from "../Validator"
import { Utils } from "../../../Utils"
import { eliCategory } from "../../../Models/RasterLayerProperties"

export default class DistanceValidator extends Validator {
    private readonly docs: string = [
        "#### Helper-arguments",
        "Options are:",
        [
            "````json",
            '  "background": "some_background_id or category, e.g. \'map\'"',
            '  "zoom": 20 # initial zoom level of the map',
            "}",
            "```",
        ].join("\n"),
    ].join("\n\n")

    constructor() {
        super(
            "distance",
            'A geographical distance in meters (rounded at two points). Will give an extra minimap with a measurement tool. Arguments: [ zoomlevel, preferredBackgroundMapType (comma separated) ], e.g. `["21", "map,photo"]',
            "decimal"
        )
    }

    isValid = (str) => {
        const t = Number(str)
        return !isNaN(t)
    }

    validateArguments(args: any): undefined | string {
        if (args === undefined) {
            return undefined
        }
        if (typeof args !== "object" || Array.isArray(args)) {
            return "Expected an object of type `{background?: string, zoom?: number}`"
        }

        const optionalKeys = ["background", "zoom"]
        const keys = Object.keys(args).filter((k) => optionalKeys.indexOf(k) < 0)
        if (keys.length > 0) {
            return (
                "Unknown key " + keys.join("; ") + "; use " + optionalKeys.join("; ") + " instead"
            )
        }
        const bg = args["background"]
        if (bg && eliCategory.indexOf(bg) < 0) {
            return (
                "The given background layer is not a recognized ELI-type. Perhaps you meant one of " +
                Utils.sortedByLevenshteinDistance(bg, eliCategory, (x) => x).slice(0, 5)
            )
        }
        if (typeof args["zoom"] !== "number") {
            return "zoom must be a number, got a " + typeof args["zoom"]
        }
        if (typeof args["zoom"] !== "number" || args["zoom"] <= 1 || args["zoom"] > 25) {
            return "zoom must be a number between 2 and 25, got " + args["zoom"]
        }
        return undefined
    }
}
