private static void CopyValueConnectionData(FlowGraph superUnitGraph, GraphInput graphInput, GraphOutput graphOutput, List <ConnectionData> connectionData)
        {
            List <string> keys = new List <string>();

            for (int i = 0; i < connectionData.Count; i++)
            {
                var index = i;

                var _key = GetKeyName(connectionData[i], keys);

                if (!keys.Contains(_key))
                {
                    keys.Add(_key);
                }

                switch (connectionData[index].source)
                {
                case ConnectionDataSource.GraphInput:
                    var def       = connectionData[index].valueType.Default();
                    var isDefault = def != null;
                    superUnitGraph.valueInputDefinitions.Add(new ValueInputDefinition()
                    {
                        key  = _key,
                        type = connectionData[index].valueType
                    });
                    superUnitGraph.PortDefinitionsChanged();
                    graphInput.valueOutputs.Single((op) => { return(op.key == _key); }).ConnectToValid(superUnitGraph.units[connectionData[index].destinationUnitIndex].valueInputs.ToList()[connectionData[index].destinationInputIndex] as ValueInput);
                    connectionData[index].subgraph.valueInputs.Single((op) => { return(op.key == _key); }).ConnectToValid(connectionData[index].externalPort as ValueOutput);
                    break;

                case ConnectionDataSource.GraphOutput:
                    superUnitGraph.valueOutputDefinitions.Add(new ValueOutputDefinition()
                    {
                        key  = _key,
                        type = connectionData[index].valueType,
                    });
                    superUnitGraph.PortDefinitionsChanged();
                    graphOutput.valueInputs.Single((op) => { return(op.key == _key); }).ConnectToValid(superUnitGraph.units[connectionData[index].sourceUnitIndex].valueOutputs.ToList()[connectionData[index].sourceOutputIndex] as ValueOutput);
                    connectionData[index].subgraph.valueOutputs.Single((op) => { return(op.key == _key); }).ConnectToValid(connectionData[index].externalPort as ValueInput);
                    break;

                case ConnectionDataSource.Node:
                    superUnitGraph.valueConnections.Add(new ValueConnection(superUnitGraph.units[connectionData[index].sourceUnitIndex].valueOutputs.ToList()[connectionData[index].sourceOutputIndex] as ValueOutput,
                                                                            superUnitGraph.units[connectionData[index].destinationUnitIndex].valueInputs.ToList()[connectionData[index].destinationInputIndex] as ValueInput));
                    break;
                }
            }
        }
        private static void ConvertToEmbed()
        {
            var selection = GraphWindow.active?.reference?.graph?.Canvas().selection;

            if (selection != null && selection.Count > 0)
            {
                Undo.RegisterCompleteObjectUndo(GraphWindow.active?.reference.rootObject, "Add Sub Graph to Script Graph");

                var superUnit       = new NestedNode();
                var superUnitGraph  = new FlowGraph();
                var superUnitCanvas = superUnitGraph.Canvas <FlowCanvas>();
                var elements        = selection.ToList();

                superUnit.position = GetNestedNodePosition(elements);

                ((FlowGraph)GraphWindow.active.reference.graph).units.Add(superUnit);

                superUnit.nest.SwitchToEmbed(superUnitGraph);

                CopyElementsToGraph(superUnitCanvas);

                var graphInput  = new GraphInput();
                var graphOutput = new GraphOutput();

                var listWithoutInputOutput = ((FlowGraph)GraphWindow.active.reference.graph).units.ToList();

                superUnitGraph.units.Add(graphInput);
                superUnitGraph.units.Add(graphOutput);

                CopyControlConnectionData(superUnitGraph, graphInput, graphOutput, GetControlIndices(selection, superUnit));
                CopyValueConnectionData(superUnitGraph, graphInput, graphOutput, GetValueIndices(selection, superUnit));
                CopyInvalidConnectionData(superUnitGraph, graphInput, graphOutput, GetInvalidIndices(selection, superUnit));

                SetInputOutputPosition(superUnitGraph, graphInput, graphOutput);

                superUnitGraph.pan = superUnit.position;

                RemoveUnusedDefinitions(superUnitGraph);

                superUnitGraph.PortDefinitionsChanged();

                GraphWindow.active.reference.graph.Canvas <FlowCanvas>().DeleteSelection();
                GraphWindow.active.reference.graph.Canvas <FlowCanvas>().selection.Add(superUnit);
            }
        }