Just a suggestion, but wouldn't it be easier to first create a mock collection tree as a JS object and then at the end go through it and create Zotero collections?