from typing import TYPE_CHECKING

from sobiraka.utils import Location
from .page import Page, PageMeta
from ..syntax import Syntax

if TYPE_CHECKING:
    from ..source import SourceDirectory


class DirPage(Page):
    """
    An autogenerated page with a table of contents.
    Used by SourceDirectory when there is no actual index page in the directory.
    """

    def __init__(self, source: 'SourceDirectory', location: Location):
        super().__init__(source, location, Syntax.MD, PageMeta(), '')

        if self.location.is_root:
            self.text = '@toc'
        else:
            naming_scheme = self.volume.config.paths.naming_scheme
            stem = naming_scheme.parse(self.source.path_in_volume).stem
            self.text = '# ' + stem + '\n\n@toc'
