import '../../../assert/js/assert.js';
import '../../../tandem/js/PhetioIDUtils.js';
import '../../../globals.js';
// Copyright 2014-2024, University of Colorado Boulder
/**
 * Model for a toggle button that changes value on each "up" event when the button is released.
 *
 * @author Sam Reid (PhET Interactive Simulations)
 * @author John Blanco (PhET Interactive Simulations)
 */
import Emitter from '../../../axon/js/Emitter.js';
import optionize from '../../../phet-core/js/optionize.js';
import EventType from '../../../tandem/js/EventType.js';
import Tandem from '../../../tandem/js/Tandem.js';
import sun from '../sun.js';
import ButtonModel from './ButtonModel.js';
export default class ToggleButtonModel extends ButtonModel {
    /**
     * @param valueOff - value when the button is in the off state
     * @param valueOn - value when the button is in the on state
     * @param property - axon Property that can be either valueOff or valueOn.
     * @param [providedOptions]
     */
    constructor(valueOff, valueOn, property, providedOptions) {
        assert && assert(property.valueComparisonStrategy === 'reference', 'ToggleButtonModel depends on "===" equality for value comparison');
        const options = optionize()({
            tandem: Tandem.REQUIRED
        }, providedOptions);
        super(options);
        this.valueOff = valueOff;
        this.valueOn = valueOn;
        this.valueProperty = property;
        // Behaves like a push button (with fireOnDown:false), but toggles its state when the button is released.
        const downListener = (down) => {
            if ((this.overProperty.get() || this.focusedProperty.get()) && this.enabledProperty.get() && !this.interrupted) {
                if (!down) {
                    this.toggle();
                }
            }
        };
        this.downProperty.link(downListener);
        this.toggledEmitter = new Emitter({
            tandem: options.tandem.createTandem('toggledEmitter'),
            phetioDocumentation: 'Emits when the button is toggled',
            phetioEventType: EventType.USER
        });
        const toggleListener = () => {
            assert && assert(this.valueProperty.value === this.valueOff || this.valueProperty.value === this.valueOn, `unrecognized value: ${this.valueProperty.value}`);
            this.valueProperty.value = this.valueProperty.value === this.valueOff ? this.valueOn : this.valueOff;
        };
        this.toggledEmitter.addListener(toggleListener);
        this.disposeToggleButtonModel = () => {
            this.downProperty.unlink(downListener);
            this.toggledEmitter.dispose();
        };
    }
    dispose() {
        this.disposeToggleButtonModel();
        super.dispose();
    }
    toggle() {
        this.toggledEmitter.emit();
        this.produceSoundEmitter.emit();
    }
}
sun.register('ToggleButtonModel', ToggleButtonModel);
//# sourceMappingURL=ToggleButtonModel.js.map