import '../../chipper/js/browser/initialize-globals.js';
import '../../assert/js/assert.js';
import '../../globals.js';
import Utils from '../../dot/js/Utils.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { combineOptions } from '../../phet-core/js/optionize.js';
import { FlowBox, ManualConstraint, Node, SceneryConstants } from '../../scenery/js/imports.js';
import Tandem from '../../tandem/js/Tandem.js';
import PlayPauseStepButtonGroup from './buttons/PlayPauseStepButtonGroup.js';
import sceneryPhet from './sceneryPhet.js';
import SceneryPhetStrings from './SceneryPhetStrings.js';
import TimeSpeed from './TimeSpeed.js';
import TimeSpeedRadioButtonGroup from './TimeSpeedRadioButtonGroup.js';
// default speeds for SpeedRadioButtonGroup
const DEFAULT_TIME_SPEEDS = [TimeSpeed.NORMAL, TimeSpeed.SLOW];
export default class TimeControlNode extends Node {
    constructor(isPlayingProperty, providedOptions) {
        const options = optionize()({
            // TimeControlNodeOptions
            timeSpeedProperty: null,
            timeSpeeds: DEFAULT_TIME_SPEEDS,
            speedRadioButtonGroupPlacement: 'right',
            flowBoxSpacing: 40,
            flowBoxAlign: 'center',
            // NodeOptions
            disabledOpacity: SceneryConstants.DISABLED_OPACITY,
            // phet-io
            tandem: Tandem.REQUIRED,
            tandemNameSuffix: 'TimeControlNode',
            visiblePropertyOptions: { phetioFeatured: true },
            phetioEnabledPropertyInstrumented: true, // opt into default PhET-iO instrumented enabledProperty
            excludeInvisibleChildrenFromBounds: true,
            // pdom
            tagName: 'div',
            labelTagName: 'h3',
            labelContent: SceneryPhetStrings.a11y.timeControlNode.labelStringProperty
        }, providedOptions);
        super();
        this.pushButtonGroup = new PlayPauseStepButtonGroup(isPlayingProperty, combineOptions({
            tandem: options.tandem.createTandem('playPauseStepButtonGroup')
        }, options.playPauseStepButtonOptions));
        this.speedRadioButtonGroup = null;
        if (options.timeSpeedProperty !== null) {
            this.speedRadioButtonGroup = new TimeSpeedRadioButtonGroup(options.timeSpeedProperty, options.timeSpeeds, combineOptions({
                tandem: options.tandem.createTandem('speedRadioButtonGroup')
            }, options.speedRadioButtonGroupOptions));
            this.addDisposable(this.speedRadioButtonGroup);
        }
        const children = [
            this.pushButtonGroup
        ];
        if (this.speedRadioButtonGroup) {
            if (options.speedRadioButtonGroupPlacement === 'left' || options.speedRadioButtonGroupPlacement === 'top') {
                children.unshift(this.speedRadioButtonGroup);
            }
            else {
                children.push(this.speedRadioButtonGroup);
            }
        }
        // Use a nested Node that will allow us to keep the play/pause button centered at (0,0) to simplify layout
        const flowBox = new FlowBox({
            children: children,
            spacing: options.flowBoxSpacing,
            orientation: options.speedRadioButtonGroupPlacement === 'left' || options.speedRadioButtonGroupPlacement === 'right' ? 'horizontal' : 'vertical',
            align: options.flowBoxAlign
        });
        options.children = [flowBox];
        ManualConstraint.create(this, [flowBox], flowBoxProxy => {
            const localBounds = this.globalToLocalBounds(this.pushButtonGroup.playPauseButton.globalBounds);
            const x = localBounds.centerX;
            const y = localBounds.centerY;
            // Round to prevent hysteresis on roundoff error
            flowBoxProxy.translation = flowBoxProxy.translation.plusXY(-Utils.roundToInterval(x, 1E-6), -Utils.roundToInterval(y, 1E-6));
        });
        this.addDisposable(this.pushButtonGroup);
        // mutate with options after spacing and layout is complete so other layout options apply correctly to the
        // whole TimeControlNode
        this.mutate(options);
        // support for binder documentation, stripped out in builds and only runs when ?binder is specified
        assert && window.phet?.chipper?.queryParameters?.binder && InstanceRegistry.registerDataURL('scenery-phet', 'TimeControlNode', this);
    }
    /**
     * Add a push button to the TimeControlNode.
     */
    addPushButton(pushButton, index) {
        this.pushButtonGroup.insertChild(index, pushButton);
        this.pushButtonGroup.updateLayout();
    }
}
sceneryPhet.register('TimeControlNode', TimeControlNode);
//# sourceMappingURL=TimeControlNode.js.map