// #region Imports
import { ElementValueAccessorBase } from '../../../../../Forms/Accessors/Abstracts/ElementValueAccessorBase';
// #endregion
/**
 * The `ChipBoxElementValueAccessor` class.
 *
 * @public
 */
export class ChipBoxElementValueAccessor extends ElementValueAccessorBase {
    // #region Utils
    static toChips(value) {
        return Array.isArray(value)
            ? [...value]
            : [];
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    hostConnected() {
        this.host.on('chipAdded', () => this.value = this.host.chips);
        this.host.on('chipRemoved', () => this.value = this.host.chips);
        this.host.on('blur', () => this.onTouched());
        // Sync the initial host value without triggering onChange, because
        // the host element may already have a value before the accessor is connected.
        void this.host.updateComplete.then(() => {
            this.writeValue(ChipBoxElementValueAccessor.toChips(this.host.chips));
        });
    }
    /**
     * @public
     * @override
     */
    writeValue(value) {
        const chips = ChipBoxElementValueAccessor.toChips(value);
        super.writeValue(chips);
        this.host.chips = chips;
    }
    /**
     * @public
     * @override
     */
    resetValue() {
        super.resetValue();
        this.host.reset();
    }
}
/**
 * @public
 */
export const chipBoxElementValueAccessor = (host) => new ChipBoxElementValueAccessor(host);
//# sourceMappingURL=ChipBoxElementValueAccessor.js.map