/**
 * @private
 * Modern.
 */
Ext.define('Ext.d3.ComponentBase', {
    extend: 'Ext.Component',
 
    // Modern's ResizeObserver-based monitoring (onElementResize below) is not
    // guaranteed to fire its initial notification for an already-sized element
    // created via Ext.create and added programmatically (e.g. Ext.Viewport.add
    // inside a vbox/flex layout) -- observed to never fire at all in that case,
    // leaving the component permanently unrendered with no error. 'painted' is
    // a stronger guarantee, but only as a future event: if the element was
    // already painted by the time this component finished constructing (as
    // happens in that same scenario), the transition already occurred and a
    // plain listener here would miss it exactly the same way. So check current
    // state first and only fall back to listening for the future transition.
    //
    // Even that transition was observed to sometimes never fire at all (its
    // own underlying browser detection -- IntersectionObserver in some Ext
    // versions -- isn't universally reliable either, notably in headless/
    // automated browser contexts). So this also arms one last-resort deferred
    // check: if nothing has rendered by then, force it directly rather than
    // stay silently blank forever. Harmless if the normal paths already did
    // their job -- handleResize no-ops without a valid size, and re-rendering
    // the same size again is a no-op for D3's own selection.data() diffing.
    initialize: function() {
        var me = this;
 
        me.callParent();
 
        if (me.isPainted()) {
            me.handleResize(null, true);
        }
        else {
            me.on('painted', 'onD3Painted', me, { single: true });
        }
 
        Ext.defer(function() {
            if (!me.destroyed && !me.size && me.isPainted()) {
                me.handleResize(null, true);
            }
        }, 300);
    },
 
    onD3Painted: function() {
        this.handleResize(null, true);
    },
 
    onElementResize: function(element, width, height) {
        this.handleResize({
            width: width,
            height: height
        });
    }
});