// #region Imports
// #endregion
const patternAttribute = 'pattern';
/**
 * Validator that requires the control's value to match a regex pattern.
 *
 * State:
 * * patternMismatch
 *
 * @public
 * @param element
 * @returns null if the validation check passes, otherwise an object containing information.
 */
export function patternValidator(element) {
    if (!element.hasAttribute(patternAttribute)) {
        console.warn(`[PatternValidator]: The '${patternAttribute}' attribute of '${element.tagName.toLowerCase()}' is missing in the inner form field element.`);
    }
    if (!element.pattern) {
        return null;
    }
    let regex = null;
    let regexStr = null;
    if (typeof element.pattern === 'string') {
        regexStr = '';
        if (!element.pattern.startsWith('^')) {
            regexStr += '^';
        }
        regexStr += element.pattern;
        if (!element.pattern.endsWith('$')) {
            regexStr += '$';
        }
        regex = new RegExp(regexStr);
    }
    else {
        regexStr = element.pattern.toString();
        regex = element.pattern;
    }
    if (isEmptyInputValue(element.value)) {
        return null; // don't validate empty values to allow optional controls
    }
    const value = element.value;
    return regex.test(value)
        ? null
        : {
            patternMismatch: [true, {
                    actualValue: value,
                    requiredPattern: regexStr
                }]
        };
}
function isEmptyInputValue(value) {
    return value == null || ((typeof value === 'string' || Array.isArray(value)) && value.length === 0);
}
//# sourceMappingURL=PatternElementValidatorFn.js.map