from TexSoup import TexNode
from typing import Optional

from texmemo.parser import Parser
from texmemo.filter import Filter
from texmemo.writer import TeXWriter
from texmemo.types import SectionConfig


class Extractor():
    def __init__(self) -> None:
        self._parser = Parser()
        self._filter = Filter()
        self._writer = TeXWriter()

    def extract(self,
                input_path: str,
                output_path: Optional[str],
                title: Optional[str],
                author: Optional[str],
                sections: Optional[list[SectionConfig]],
                file_only: bool,
                strict_mode: bool,
                force: bool,
                write_file: bool,
                skip_filter: bool
                ) -> TexNode:
        """Extract sections from a TeX document.

        Args:
            input_path (str): Path to the TeX input document
            output_path (str): Path to the TeX output document
            title (str): Overrides the title of the input document
            author (str): Overrides the title of the output document
            sections (list[str]): List of sections to be picked from the docuemnt
            file_only (bool): Do not wrap output file with a parent folder
            strict_mode (bool): Require TeX syntax to be correct
            force (bool): Override existing file during write
            write_file (bool): Write the file
            skip_filter (bool): Do not apply any filter after parsing
            extract_proof (bool): Extracts the proof-like environment following sections

        Returns:
            TexNode: _description_
        """
        soup = self._parser.parse(input_path, strict_mode)
        if not skip_filter and sections:
            self._filter.filter(soup, title, author, sections)
        if write_file and output_path is not None:
            self._writer.write(soup, output_path, file_only, force)
        return soup
