import { UndoRedoGraph } from '../graph';

export function generateNetworkAtElement<N, E>(networkRenderElement:any, options, maxStackSize = 100){

    /** Defeine the Network */
    const network = new UndoRedoGraph<N,E>(networkRenderElement, options, maxStackSize);

    /**
     * Define a Function, which reacts to resizing the Elements. This Function
     * will resize the Network and adapt its postion.
     * @param width width of the Network
     * @param height height of the Network
     */
    const resize = (width: number, height: number) => {

        if (width > 0 && height > 0) {

            /**
             * Extract the currently used Postion and Scale, to
             * reset this after resizing the Network
             */
            const position = network.visNetwork.getViewPosition();
            const currentScale = network.visNetwork.getScale();

            // Update the Network Size        
            network.visNetwork.setSize(width, height);
            // Perform a redraw off the Network
            network.visNetwork.redraw();

            network.visNetwork.moveTo({
                position,
                scale: currentScale,
                animation: false
            });
        }
    }

    /**
     * Return the Network and a Resize Function 
     */
    return {
        network,
        resize
    }
}