Script to make a table of contents for a book

I’m trying to make a script that creates a table of contents for a book. The script right now, makes a book, adds the neccesary documents to the book, but I can’t get it to make a Table of Contents for the book. I can do it manually, but I’m really struggling to script it. Any help out there would be greatly appreciated. Thanks

Sonar Bookends InDex from Virginia Systems Inc. of Midlothian, Va., is a cross-platform indexing utility that will let InDesign users create a table of contents and index books. An index can span multiple documents and automatically reverse first and last names as well as sort word lists