How to create duplicate instance of a selected portion from figma canvas in plugin development?

  //in first one I getting compNode from component. it's working fine. I'm able to create duplicate instance.
  const compNode = figma.root.findOne(node => node.type == "COMPONENT_SET" && node.name == "post");
  console.log(compNode,'compNode');
  const postComponentSet = compNode as ComponentSetNode;
  const defaultVarient= postComponentSet.defaultVariant as ComponentNode; 
  defaultVarient.createInstance();

//but 2nd option I getting selectedNode by selecting something on figma canvas
  const selectedNode = figma.currentPage.selection[0];
  console.log(selectedNode,'selected');
  const postComponentSet2=selectedNode as ComponentSetNode;
  const newNode = postComponentSet2.defaultVariant as ComponentNode;
  newNode.createInstance();

//log of these two


Here the first case we got a ComponentSetNode and 2nd time we got InstanceNode
So my question is how can I convert the 2nd InstanceNode to ComponentSetNode?

Or is there any other way to createInstance of a selected portion/duplicate?

You cannot create an InstanceNode from a ComponentSetNode, so use the clone() method. In addition, you don’t need to create an instance.

It’s not entirely clear if you want the InstanceNode to be nested within a variant in the component set, or do you want to detach the instance and create the variant?

If the first, then create a new ComponentNode, insert the selected instance into it, and then combine as variants.
If the second, then create a new ComponentNode, detach instance, insert the selected frame into the component, and then combine as variants.

I see. Thanks