---
import PageList from './PageList.astro';
import '../css/subsections.css';

const { indexArray, pages } = Astro.props;

let index = indexArray;

// Only add the auto index for Parts, not any deeper structure
const skip = index === undefined || index === null || index.length > 1;

// Special hacky handling for the the /contents page
if (!skip && index[0] === -1) {
  index = [];
}

// Find pages that are subsections of the page we are on
const indexFilterString = skip || index.length === 0 ? '' : index.join() + ',';
const filteredPages = skip
  ? []
  : pages.filter((p) => p.data.index.join().startsWith(indexFilterString));
---

{
  filteredPages.length > 0 && (
    <div class="subsection-list">
      <PageList pages={filteredPages} depth={index.length} />
    </div>
  )
}
