import { Validator } from '../../../utilities/models/validator';
export const baseValidityFlagNames = [
    'duplicateMappingMentionHref',
    'missingMentionHrefValue',
    'mentionHrefNotValidUrl',
    'mentionHrefDoesNotMatchPattern',
    'missingPatternAttribute',
    'unsupportedPatternValue'
];
/**
 * Validator for RichTextMention
 */
export class RichTextMentionValidator extends Validator {
    constructor(configValidityKeys) {
        super(configValidityKeys);
    }
    validate(mappings, pattern) {
        this.untrackAll();
        const mentionHrefs = mappings.map(mapping => mapping.key);
        this.validateNoMissingMentionHref(mentionHrefs);
        this.validateUniqueMentionHref(mentionHrefs);
        this.validateMissingPattern(pattern);
        this.validatePattern(pattern);
        this.validateHrefForPattern(mentionHrefs, pattern);
        this.validateHrefForUrl(mentionHrefs);
    }
    /**
     * @returns an object containing flags for various ways the configuation can be invalid
     */
    getValidity() {
        return this.getValidationFlags();
    }
    /**
     * Sets a particular validity condition flag's value, e.g. "hasInvalidFooValue" = true
     */
    setConditionValue(name, isInvalid) {
        if (isInvalid) {
            this.track(name);
        }
        else {
            this.untrack(name);
        }
    }
    validateMissingPattern(pattern) {
        this.setConditionValue('missingPatternAttribute', pattern === undefined);
    }
    validatePattern(pattern) {
        this.setConditionValue('unsupportedPatternValue', pattern === undefined || this.isInvalidRegex(pattern));
    }
    validateUniqueMentionHref(mentionHref) {
        const invalid = new Set(mentionHref).size !== mentionHref.length;
        this.setConditionValue('duplicateMappingMentionHref', invalid);
    }
    validateNoMissingMentionHref(mentionHrefs) {
        const invalid = mentionHrefs.some(href => href === undefined);
        this.setConditionValue('missingMentionHrefValue', invalid);
    }
    validateHrefForUrl(mentionHrefs) {
        const invalid = mentionHrefs.some(href => {
            return (href === undefined
                || typeof href !== 'string'
                || this.isInvalidUrl(href));
        });
        this.setConditionValue('mentionHrefNotValidUrl', invalid);
    }
    validateHrefForPattern(mentionHrefs, pattern) {
        const invalid = pattern === undefined || this.isInvalidRegex(pattern)
            ? true
            : mentionHrefs.some(href => {
                return (href === undefined
                    || typeof href !== 'string'
                    || !new RegExp(pattern).test(href));
            });
        this.setConditionValue('mentionHrefDoesNotMatchPattern', invalid);
    }
    isInvalidUrl(url) {
        try {
            // Check whether the constructor throws an error for an Invalid URL.
            // eslint-disable-next-line no-new
            new URL(url);
            return false;
        }
        catch (_error) {
            return true;
        }
    }
    isInvalidRegex(pattern) {
        try {
            // Check whether the constructor throws an error when it cannot be parsed as a valid regular expression.
            // eslint-disable-next-line no-new
            new RegExp(pattern);
            return false;
        }
        catch (_error) {
            return true;
        }
    }
}
//# sourceMappingURL=mention-validator.js.map