import '../../../chipper/js/browser/initialize-globals.js';
import '../../../globals.js';
// Copyright 2018-2024, University of Colorado Boulder
/**
 * Tracks object allocations for reporting using binder.
 *
 * @author Sam Reid (PhET Interactive Simulations)
 * @author Michael Kauzmann (PhET Interactive Simulations)
 */
import phetCore from '../phetCore.js';
function registerImplementation(instance, key, map) {
    instance.toDataURL(dataURL => {
        map[key].push(dataURL);
    });
}
class InstanceRegistry {
    /**
     * Adds a screenshot of the given scenery Node
     */
    static registerDataURL(repoName, typeName, instance) {
        if (phet.chipper.queryParameters.binder) {
            // Create the map if we haven't seen that component type before
            const key = `${repoName}/${typeName}`;
            InstanceRegistry.componentMap[key] = InstanceRegistry.componentMap[key] || [];
            try {
                if (instance.boundsProperty.value.isFinite()) {
                    registerImplementation(instance, key, InstanceRegistry.componentMap);
                }
                else {
                    const boundsListener = (bounds) => {
                        if (bounds.isFinite()) {
                            registerImplementation(instance, key, InstanceRegistry.componentMap);
                            instance.boundsProperty.unlink(boundsListener); // less for memory, and more to not double add
                        }
                    };
                    instance.boundsProperty.lazyLink(boundsListener);
                }
            }
            catch (e) {
                // Ignore nodes that don't draw anything
                // TODO https://github.com/phetsims/phet-core/issues/80 is this masking a problem?
            }
        }
    }
    /**
     * Register a toolbox pattern node. There is no strict class for this, so this factored out method can be used by any constructor
     */
    static registerToolbox(instance) {
        if (phet.chipper.queryParameters.binder) {
            InstanceRegistry.registerDataURL('sun', 'ToolboxPattern', instance);
        }
    }
    /**
     * Register a Hotkey for binder documentation.
     */
    static registerHotkey(hotkeyData) {
        if (phet.chipper.queryParameters.binder) {
            InstanceRegistry.hotkeys.push(hotkeyData.serialize());
        }
    }
}
// Per named component, store image URIs of what their usages look like
InstanceRegistry.componentMap = {};
// An array of all Hotkeys that have been registered.
InstanceRegistry.hotkeys = [];
phetCore.register('InstanceRegistry', InstanceRegistry);
export default InstanceRegistry;
//# sourceMappingURL=InstanceRegistry.js.map