import '../../chipper/js/initialize-globals.js';
import '../../assert/js/assert.js';
import '../../globals.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize from '../../phet-core/js/optionize.js';
import { ManualConstraint, Node } from '../../scenery/js/imports.js';
import { getGroupItemNodes } from './GroupItemOptions.js';
import sun from './sun.js';
export default class ToggleNode extends Node {
    constructor(valueProperty, elements, providedOptions) {
        const options = optionize()({
            // SelfOptions
            alignChildren: ToggleNode.CENTER,
            unselectedChildrenSceneGraphStrategy: 'included'
        }, providedOptions);
        const nodes = getGroupItemNodes(elements, options.tandem);
        options.children = nodes;
        options.alignChildren(options.children);
        super(options);
        const alignmentConstraint = new ManualConstraint(this, options.children, (...x) => {
            options.alignChildren(x);
        });
        const valueListener = (value) => {
            const matches = [];
            for (let i = 0; i < elements.length; i++) {
                const element = elements[i];
                const visible = element.value === value;
                nodes[i].visible = visible;
                if (visible) {
                    matches.push(nodes[i]);
                }
            }
            assert && assert(matches.length === 1, `Wrong number of matches: ${matches.length}`);
            if (options.unselectedChildrenSceneGraphStrategy === 'excluded') {
                this.children = matches;
            }
        };
        // Run the link after super so we can change the children if needed. This means that when areUnselectedChildrenInSceneGraph===false,
        // all children will temporarily be visible: true until this link is called. However, since this ToggleNode is not yet
        // in the scene graph, this should not cause any visual problems or significant performance issues.
        valueProperty.link(valueListener);
        this.nodes = nodes;
        this.disposeToggleNode = function () {
            valueProperty.unlink(valueListener);
            alignmentConstraint.dispose();
            nodes.forEach(node => node.dispose());
        };
        assert && phet?.chipper?.queryParameters?.binder && InstanceRegistry.registerDataURL('sun', 'ToggleNode', this);
    }
    dispose() {
        this.disposeToggleNode();
        super.dispose();
    }
    /**
     * A value for the alignChildren option.
     * Centers the latter nodes on the x,y center of the first node.
     */
    static CENTER(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].center = children[0].center;
        }
    }
    /**
     * A value for the alignChildren option.
     * Centers the latter nodes on the x center of the first node.
     */
    static CENTER_X(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].centerX = children[0].centerX;
        }
    }
    /**
     * A value for the alignChildren option.
     * Centers the latter nodes on the y center of the first node.
     */
    static CENTER_Y(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].centerY = children[0].centerY;
        }
    }
    /**
     * A value for the alignChildren option.
     * Left aligns nodes on the left of the first node.
     */
    static LEFT(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].left = children[0].left;
        }
    }
    /**
     * A value for the alignChildren option.
     * Aligns nodes on the bottom of the first node.
     */
    static BOTTOM(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].bottom = children[0].bottom;
        }
    }
    /**
     * A value for the alignChildren option.
     * Aligns nodes on the bottom of the first node.
     */
    static CENTER_BOTTOM(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].centerBottom = children[0].centerBottom;
        }
    }
    /**
     * A value for the alignChildren option.
     * Right aligns nodes on the right of the first node.
     */
    static RIGHT(children) {
        for (let i = 1; i < children.length; i++) {
            children[i].right = children[0].right;
        }
    }
    /**
     * A value for the alignChildren option.
     * No alignment is performed.
     */
    static NONE(children) {
        // do nothing
    }
}
sun.register('ToggleNode', ToggleNode);
//# sourceMappingURL=ToggleNode.js.map