// Copyright 2013-2025, University of Colorado Boulder
/**
 * Reset All button, typically used to reset everything ('reset all') on a Screen.
 * Extends ResetButton, adding things that are specific to 'reset all'.
 *
 * @author John Blanco (PhET Interactive Simulations)
 * @author Chris Malley (PixelZoom, Inc.)
 */
import { string_scenery_phet_a11y_resetAll_label_StringProperty } from '../strings/a11y/resetAll/label.js';
import { string_scenery_phet_a11y_voicing_resetAll_contextResponse_StringProperty } from '../strings/a11y/voicing/resetAll/contextResponse.js';
import { string_scenery_phet_a11y_resetAll_alert_StringProperty } from '../strings/a11y/resetAll/alert.js';
import { string_scenery_phet_keyboardHelpDialog_resetAll_StringProperty } from '../strings/keyboardHelpDialog/resetAll.js';
import { string_scenery_phet_a11y_keyboardHelpDialog_general_resetAllDescriptionPattern_StringProperty } from '../strings/a11y/keyboardHelpDialog/general/resetAllDescriptionPattern.js';
import '../../../globals.js';
import _ from 'lodash';
import Property from '../../../axon/js/Property.js';
import optionize from '../../../phet-core/js/optionize.js';
import StringUtils from '../../../phetcommon/js/util/StringUtils.js';
import voicingUtteranceQueue from '../../../scenery/js/accessibility/voicing/voicingUtteranceQueue.js';
import HotkeyData from '../../../scenery/js/input/HotkeyData.js';
import KeyboardListener from '../../../scenery/js/listeners/KeyboardListener.js';
import sharedSoundPlayers from '../../../tambo/js/sharedSoundPlayers.js';
import Tandem from '../../../tandem/js/Tandem.js';
import ActivationUtterance from '../../../utterance-queue/js/ActivationUtterance.js';
import isResettingAllProperty from '../isResettingAllProperty.js';
import TextKeyNode from '../keyboard/TextKeyNode.js';
import PhetColorScheme from '../PhetColorScheme.js';
import sceneryPhet from '../sceneryPhet.js';
import SceneryPhetConstants from '../SceneryPhetConstants.js';
import ResetButton from './ResetButton.js';
const MARGIN_COEFFICIENT = 5 / SceneryPhetConstants.DEFAULT_BUTTON_RADIUS;
class ResetAllButton extends ResetButton {
    constructor(providedOptions) {
        const options = optionize()({
            // ResetAllButtonOptions
            radius: SceneryPhetConstants.DEFAULT_BUTTON_RADIUS,
            phetioRestoreScreenStateOnReset: true,
            interruptScreenViewInput: true,
            // Fine tuned in https://github.com/phetsims/tasks/issues/985 and should not be overridden lightly.
            touchAreaDilation: 5.2,
            baseColor: PhetColorScheme.RESET_ALL_BUTTON_BASE_COLOR,
            arrowColor: 'white',
            // phet-io
            tandem: Tandem.REQUIRED,
            tandemNameSuffix: 'ResetAllButton',
            phetioDocumentation: 'The orange, round button that can be used to restore the initial state',
            // sound generation
            soundPlayer: sharedSoundPlayers.get('resetAll'),
            // pdom
            accessibleName: string_scenery_phet_a11y_resetAll_label_StringProperty,
            // voicing
            voicingNameResponse: string_scenery_phet_a11y_resetAll_label_StringProperty,
            voicingContextResponse: string_scenery_phet_a11y_voicing_resetAll_contextResponse_StringProperty
        }, providedOptions);
        ;
        options.xMargin = options.yMargin = options.radius * MARGIN_COEFFICIENT;
        super(options);
        // a11y - When reset all button is fired, disable alerts so that there isn't an excessive stream of alerts while
        // many Properties are reset. When callbacks are ended for reset all, enable alerts again and announce an alert that
        // everything was reset.
        const resetUtterance = new ActivationUtterance({ alert: string_scenery_phet_a11y_resetAll_alert_StringProperty });
        let voicingEnabledOnFire = voicingUtteranceQueue.enabled;
        const ariaEnabledOnFirePerUtteranceQueueMap = new Map(); // Keep track of the enabled of each connected description UtteranceQueue
        this.pushButtonModel.isFiringProperty.lazyLink((isFiring) => {
            isResettingAllProperty.value = isFiring;
            // Handle voicingUtteranceQueue.
            if (isFiring) {
                // Interrupt before doing anything else.
                options.interruptScreenViewInput && this.interruptScreenViewInput();
                voicingEnabledOnFire = voicingUtteranceQueue.enabled;
                voicingUtteranceQueue.enabled = false;
                voicingUtteranceQueue.clear();
            }
            else {
                // Every ResetAllButton has the option to reset to the last PhET-iO state if desired.
                if (Tandem.PHET_IO_ENABLED && options.phetioRestoreScreenStateOnReset &&
                    // Even though this is Tandem.REQUIRED, still be graceful if not yet instrumented.
                    this.isPhetioInstrumented()) {
                    phet.phetio.phetioEngine.phetioStateEngine.restoreStateForScreen(options.tandem);
                }
                // Restore the enabled state to each utteranceQueue after resetting.
                voicingUtteranceQueue.enabled = voicingEnabledOnFire;
                this.voicingSpeakFullResponse();
            }
            // Handle each connected description UtteranceQueue.
            this.forEachUtteranceQueue(utteranceQueue => {
                if (isFiring) {
                    // Mute and clear the utteranceQueue.
                    ariaEnabledOnFirePerUtteranceQueueMap.set(utteranceQueue, utteranceQueue.enabled);
                    utteranceQueue.enabled = false;
                    utteranceQueue.clear();
                }
                else {
                    utteranceQueue.enabled = ariaEnabledOnFirePerUtteranceQueueMap.get(utteranceQueue) || utteranceQueue.enabled;
                    utteranceQueue.addToBack(resetUtterance);
                }
            });
        });
        const keyboardListener = KeyboardListener.createGlobal(this, {
            keyStringProperties: ResetAllButton.RESET_ALL_HOTKEY_DATA.keyStringProperties,
            fire: () => this.pdomClick(),
            // fires on up because the listener will often call interruptSubtreeInput (interrupting this keyboard listener)
            fireOnDown: false
        });
        this.disposeResetAllButton = () => {
            keyboardListener.dispose();
            ariaEnabledOnFirePerUtteranceQueueMap.clear();
        };
    }
    interruptScreenViewInput() {
        const screenViews = this.getParentScreenViews();
        for (let i = 0; i < screenViews.length; i++) {
            screenViews[i].interruptSubtreeInput();
        }
    }
    getParentScreenViews() {
        const ScreenViewClass = self.phet?.joist?.ScreenView;
        if (!ScreenViewClass) {
            return [];
        }
        const trails = this.getTrails();
        const screenViews = [];
        for (let i = 0; i < trails.length; i++) {
            const trail = trails[i];
            const nodes = trail.nodes;
            // Start at the closest ancestor
            for (let j = nodes.length - 1; j >= 0; j--) {
                const node = nodes[j];
                if (node instanceof ScreenViewClass) {
                    screenViews.push(node);
                    break; // Stop at first ScreenView
                }
            }
        }
        return _.uniq(screenViews);
    }
    dispose() {
        this.disposeResetAllButton();
        super.dispose();
    }
}
ResetAllButton.RESET_ALL_HOTKEY_DATA = new HotkeyData({
    // alt+r
    keyStringProperties: [new Property('alt+r')],
    // visual label for this Hotkey in the Keyboard Help dialog
    keyboardHelpDialogLabelStringProperty: string_scenery_phet_keyboardHelpDialog_resetAll_StringProperty,
    // PDOM description for this Hotkey in the Keyboard Help dialog
    keyboardHelpDialogPDOMLabelStringProperty: StringUtils.fillIn(string_scenery_phet_a11y_keyboardHelpDialog_general_resetAllDescriptionPattern_StringProperty, {
        altOrOption: TextKeyNode.getAltKeyString()
    }),
    repoName: sceneryPhet.name,
    global: true
});
export default ResetAllButton;
//# sourceMappingURL=ResetAllButton.js.map