import _ from 'lodash';
import '../../../globals.js';
// Copyright 2016-2022, University of Colorado Boulder
/**
 * base type for an object that accumulates key presses, works in conjunction with the common-code keypad
 *
 * @author John Blanco
 * @author Aadish Gupta
 */
import Property from '../../../axon/js/Property.js';
import sceneryPhet from '../sceneryPhet.js';
import KeyID from './KeyID.js';
class AbstractKeyAccumulator {
    constructor(validators) {
        this.validators = validators;
        // Property that tracks the accumulated key presses as an array
        this.accumulatedKeysProperty = new Property([]);
        // When true, the next key press (expect backspace) will clear the accumulated value
        this._clearOnNextKeyPress = false;
    }
    get clearOnNextKeyPress() { return this.getClearOnNextKeyPress(); }
    set clearOnNextKeyPress(value) { this.setClearOnNextKeyPress(value); }
    /**
     * Clears the accumulated keys.
     */
    clear() {
        this.accumulatedKeysProperty.reset();
    }
    /**
     * Sets/clears the flag that determines whether pressing a key (except for backspace) will clear the accumulated keys.
     */
    setClearOnNextKeyPress(clearOnNextKeyPress) {
        this._clearOnNextKeyPress = clearOnNextKeyPress;
    }
    /**
     * Gets the value of the flag determines whether pressing a key (except for backspace) will clear the accumulated keys.
     */
    getClearOnNextKeyPress() {
        return this._clearOnNextKeyPress;
    }
    /**
     * validates a proposed set of keys and (if valid) update the property that represents the accumulated keys
     * @param proposedKeys - the proposed set of keys, to be validated
     *
     * @returns boolean
     */
    validateKeys(proposedKeys) {
        // Ensures that proposedKeys exist before validation
        let valid = !!proposedKeys;
        // If any validator returns false then the proposedKey is not valid
        this.validators.forEach(validator => {
            valid = valid && validator(proposedKeys);
        });
        return valid;
    }
    /**
     * update the property that represents the accumulated keys
     * @param proposedKeys - the proposed set of keys
     */
    updateKeys(proposedKeys) {
        this.accumulatedKeysProperty.set(proposedKeys);
    }
    /**
     * creates an empty array if clearOnNextKeyPress is true, the behavior differs if Backspace key is pressed
     */
    handleClearOnNextKeyPress(keyIdentifier) {
        let proposedArray;
        if (!this.getClearOnNextKeyPress() || keyIdentifier === KeyID.BACKSPACE) {
            proposedArray = _.clone(this.accumulatedKeysProperty.get());
        }
        else {
            proposedArray = [];
        }
        this.setClearOnNextKeyPress(false);
        return proposedArray;
    }
    dispose() {
        this.accumulatedKeysProperty.dispose();
    }
}
sceneryPhet.register('AbstractKeyAccumulator', AbstractKeyAccumulator);
export default AbstractKeyAccumulator;
//# sourceMappingURL=AbstractKeyAccumulator.js.map