/* eslint-disable @typescript-eslint/prefer-function-type */
// #region Imports
import { RendererServiceLocator } from '../../../Dom/Renderer';
/**
 * Base class for all ControlValueAccessor classes defined in Forms package.
 * Contains common logic and utility functions.
 *
 * @public
 * @abstract
 */
export class ElementValueAccessorBase {
    // #region Fields
    _host;
    _value;
    _onTouched;
    _onChange;
    _onReset;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `ElementValueAccessorBase` class.
     *
     * @public
     */
    constructor(host) {
        this._host = host;
        this._value = undefined;
        this._onTouched = () => { }; // noop
        this._onChange = () => { }; // noop
        this._onReset = () => { }; // noop
    }
    // #endregion
    // #region Properties
    /**
     * Returns the element that the accessor is attached to.
     *
     * @protected
     * @readonly
     */
    get value() {
        return this._value;
    }
    set value(value) {
        if (this._value !== value) {
            this._value = value;
            this._onChange(value);
        }
    }
    /**
     * Returns the element that the accessor is attached to.
     *
     * @protected
     * @readonly
     */
    get host() {
        return this._host;
    }
    /**
     * Returns the onTouched callback.
     *
     * @protected
     * @readonly
     */
    get onTouched() {
        return this._onTouched;
    }
    /**
     * Returns the onChange callback.
     *
     * @protected
     * @readonly
     */
    get onChange() {
        return this._onChange;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    registerOnChange(fn) {
        this._onChange = typeof fn === 'function'
            ? fn
            : () => { };
    }
    /**
     * @public
     */
    registerOnTouched(fn) {
        this._onTouched = typeof fn === 'function'
            ? fn
            : () => { };
    }
    /**
     * @public
     */
    registerOnReset(fn) {
        this._onReset = typeof fn === 'function'
            ? fn
            : () => { };
    }
    /**
     * Sets the "disabled" property on the range input element.
     * @public
     */
    setDisabled(isDisabled) {
        this.setProperty('disabled', isDisabled);
    }
    /**
     * Writes a value from the form framework into the accessor without
     * triggering the onChange callback. This is the correct behavior because
     * writeValue represents a programmatic push from the form model into the
     * control — it is not a user-initiated change.
     *
     * @public
     */
    writeValue(value) {
        this._value = value;
    }
    /**
     * @public
     */
    resetValue() {
        this.writeValue(null);
        this._onReset();
    }
    /**
     * @public
     */
    releaseCallbacks() {
        this._onTouched = () => { };
        this._onChange = () => { };
        this._onReset = () => { };
    }
    /**
     * Helper method that sets a property on a target element using the current Renderer implementation.
     *
     * @protected
     */
    setProperty(key, value) {
        RendererServiceLocator.current.setProperty(this._host, key, String(value));
    }
}
//# sourceMappingURL=ElementValueAccessorBase.js.map