// Copyright 2014-2025, University of Colorado Boulder
/**
 * A round toggle button that displays some custom icon when playing and a triangular "Play" icon when not playing.
 *
 * @author Sam Reid (PhET Interactive Simulations)
 * @author Jesse Greenberg (PhET Interactive Simulations)
 */
import { string_scenery_phet_a11y_playControlButton_play_StringProperty } from '../strings/a11y/playControlButton/play.js';
import { string_scenery_phet_keyboardHelpDialog_timingControls_pauseOrPlayAction_StringProperty } from '../strings/keyboardHelpDialog/timingControls/pauseOrPlayAction.js';
import '../../../globals.js';
import '../../../chipper/js/browser/initialize-globals.js';
import Property from '../../../axon/js/Property.js';
import optionize from '../../../phet-core/js/optionize.js';
import HotkeyData from '../../../scenery/js/input/HotkeyData.js';
import KeyboardListener from '../../../scenery/js/listeners/KeyboardListener.js';
import Circle from '../../../scenery/js/nodes/Circle.js';
import Path from '../../../scenery/js/nodes/Path.js';
import BooleanRoundToggleButton from '../../../sun/js/buttons/BooleanRoundToggleButton.js';
import sharedSoundPlayers from '../../../tambo/js/sharedSoundPlayers.js';
import PlayIconShape from '../PlayIconShape.js';
import sceneryPhet from '../sceneryPhet.js';
import SceneryPhetConstants from '../SceneryPhetConstants.js';
class PlayControlButton extends BooleanRoundToggleButton {
    /**
     * @param isPlayingProperty
     * @param endPlayingIcon - icon for the button when pressing it will stop play
     * @param providedOptions
     */
    constructor(isPlayingProperty, endPlayingIcon, providedOptions) {
        const options = optionize()({
            // SelfOptions
            radius: SceneryPhetConstants.PLAY_CONTROL_BUTTON_RADIUS,
            scaleFactorWhenNotPlaying: 1,
            includeGlobalHotkey: false,
            startPlayingLabel: string_scenery_phet_a11y_playControlButton_play_StringProperty,
            endPlayingLabel: null,
            valueOffSoundPlayer: sharedSoundPlayers.get('pause'),
            valueOnSoundPlayer: sharedSoundPlayers.get('play'),
            // It's dimensions are calculated dynamically based on radius below to make sure the play and pause buttons are
            // in sync.
            xMargin: 0,
            yMargin: 0
        }, providedOptions);
        ;
        // play and pause icons are sized relative to the radius
        const playWidth = options.radius * 0.8;
        const playHeight = options.radius;
        const playPath = new Path(new PlayIconShape(playWidth, playHeight), {
            fill: 'black',
            centerX: options.radius * 0.05, // move to right slightly since we don't want it exactly centered
            centerY: 0
        });
        // put the play and stop symbols inside circles so they have the same bounds,
        // otherwise BooleanToggleNode will re-adjust their positions relative to each other
        const playCircle = new Circle(options.radius, {
            children: [playPath]
        });
        endPlayingIcon.centerX = 0;
        endPlayingIcon.centerY = 0;
        const endPlayingCircle = new Circle(options.radius, {
            children: [endPlayingIcon]
        });
        super(isPlayingProperty, endPlayingCircle, playCircle, options);
        const isPlayingListener = (isPlaying, oldValue) => {
            // pdom - accessible name for the button
            this.innerContent = isPlaying ? options.endPlayingLabel
                : options.startPlayingLabel;
            // so we don't scale down the button immediately if isPlayingProperty is initially false
            const runningScale = oldValue === null ? 1 : 1 / options.scaleFactorWhenNotPlaying;
            this.scale(isPlaying ? runningScale : options.scaleFactorWhenNotPlaying);
        };
        isPlayingProperty.link(isPlayingListener);
        // a listener that toggles the isPlayingProperty with hotkey Alt+K, regardless of where focus is in the document
        let globalKeyboardListener = null;
        if (options.includeGlobalHotkey && phet.chipper.queryParameters.supportsInteractiveDescription) {
            globalKeyboardListener = KeyboardListener.createGlobal(this, {
                keyStringProperties: PlayControlButton.TOGGLE_PLAY_HOTKEY_DATA.keyStringProperties,
                fireOnDown: false,
                fire: () => {
                    isPlayingProperty.set(!isPlayingProperty.get());
                    const soundPlayer = isPlayingProperty.get() ? options.valueOnSoundPlayer : options.valueOffSoundPlayer;
                    if (soundPlayer) {
                        soundPlayer.play();
                    }
                }
            });
        }
        this.disposePlayStopButton = () => {
            if (isPlayingProperty.hasListener(isPlayingListener)) {
                isPlayingProperty.unlink(isPlayingListener);
            }
            if (globalKeyboardListener) {
                globalKeyboardListener.dispose();
            }
        };
    }
    dispose() {
        this.disposePlayStopButton();
        super.dispose();
    }
}
PlayControlButton.TOGGLE_PLAY_HOTKEY_DATA = new HotkeyData({
    keyStringProperties: [new Property('alt+k')],
    keyboardHelpDialogLabelStringProperty: string_scenery_phet_keyboardHelpDialog_timingControls_pauseOrPlayAction_StringProperty,
    repoName: sceneryPhet.name,
    global: true
});
export default PlayControlButton;
//# sourceMappingURL=PlayControlButton.js.map