import StringValidator from "./StringValidator"
import { Translation } from "../../i18n/Translation"
import Translations from "../../i18n/Translations"

export default class RegexValidator extends StringValidator {
    constructor() {
        super(
            "regex",
            "Only used when your input should be a valid regex. This validator is only used as helper for Studio and should not be used in a mapcomplete-theme."
        )
    }

    getFeedback(s: string): Translation | undefined {
        try {
            new RegExp(s)
        } catch (e) {
            return Translations.T("Not a valid Regex: " + e)
        }
    }

    isValid(s: string): boolean {
        return this.getFeedback(s) === undefined
    }
}
