This may be one way to do it in the software, but too simple. What's about [maybe "forgotten"] headers covered by a picture frame? You don't see the header, only the picture, but in the TOC you will see the header. So the software should first create a list with positions of the entries and their visibilities and create then the TOC using the position. And here is the next question: You have more than one column, either independent or dependent. How should the TOC be build? There should be a switch to determine whether (a) the entries of a column are before the entries of the next column, or (b) the entries are looked up according to the vertical position and when there are more than one from left to right (or in some countries from right to left).
A TOC should be independent of the layer structure as many people try to order the layers as they appear on a page - to find everything really fast. And often the "layers" are not real layers, they are more representatives of boxes.