/**
 * Given a graph and a set of nodes, get all connections that connect these nodes amongst themselves
 * @param {NodeInstance[]} nodes
 * @returns {Connection[]}
 */
export function graph_collect_connections_amongst_nodes({
                                                            nodes
                                                        }) {

    const r = new Set();

    const node_count = nodes.length;

    for (let i = 0; i < node_count; i++) {
        const node_0 = nodes[i];

        const connections = node_0.connections.asArray();
        const connection_count = connections.length;

        for (let j = 0; j < connection_count; j++) {
            const connection = connections[j];

            if (connection.source.instance === connection.target.instance) {
                // Special case. Auto-connection, from node to itself
                r.add(connection);
                continue;
            }

            for (let k = i + 1; k < node_count; k++) {
                const node_1 = nodes[k];

                if (node_0 === node_1) {
                    // duplicate node found
                    continue;
                }

                if (connection.isAttachedToNode(node_1.id)) {
                    r.add(connection);

                    // connection can only exist between two ports, so we're done
                    break;
                }
            }
        }
    }

    return Array.from(r);
}
