// #region Imports
// #endregion
/**
 * A class that computes and caches `ValidityStateFlags` for a component with a given `State` interface.
 *
 * Cached performance before computing validity is important since constraint validation must be
 * checked frequently and synchronously when properties change.
 *
 * @template TState The expected interface of properties relevant to constraint  validation.
 */
export class ControlValidatorBase {
    // #region Fields
    _control;
    _getCurrentState;
    _prevState;
    _currentValidity;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `Validator` class.
     *
     * @protected
     * @param control - The control that the validator is attached to.
     * @param stateFn - A callback that returns the current state of constraint validation-related properties.
     */
    constructor(control, stateFn) {
        this._control = control;
        this._getCurrentState = stateFn;
        this._prevState = undefined;
        this._currentValidity = {
            validity: {
                badInput: false,
                customError: false,
                patternMismatch: false,
                rangeOverflow: false,
                rangeUnderflow: false,
                stepMismatch: false,
                tooLong: false,
                tooShort: false,
                typeMismatch: false,
                valueMissing: false
            },
            validationMessage: ''
        };
    }
    // #endregion
    // #region Properties
    /**
     * Returns the control that the validator is attached to.
     *
     * @public
     * @readonly
     */
    get control() {
        return this._control;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    getValidity() {
        const state = this._getCurrentState();
        const hasStateChanged = !this._prevState || !this.equals(this._prevState, state);
        if (!hasStateChanged) {
            return this._currentValidity;
        }
        const result = this.computeValidity(state);
        this._prevState = this.copy(state);
        this._currentValidity = {
            validationMessage: result.validationMessage,
            validity: {
                // Change any `ValidityState` instances into `ValidityStateFlags` since
                // `ValidityState` cannot be easily `{...spread}`.
                badInput: result.validity.badInput,
                customError: result.validity.customError,
                patternMismatch: result.validity.patternMismatch,
                rangeOverflow: result.validity.rangeOverflow,
                rangeUnderflow: result.validity.rangeUnderflow,
                stepMismatch: result.validity.stepMismatch,
                tooLong: result.validity.tooLong,
                tooShort: result.validity.tooShort,
                typeMismatch: result.validity.typeMismatch,
                valueMissing: result.validity.valueMissing
            }
        };
        return this._currentValidity;
    }
}
//# sourceMappingURL=ControlValidatorBase.js.map