# Copyright (c) 2017-2026 Juancarlo Añez (apalala@gmail.com)
# SPDX-License-Identifier: BSD-4-Clause
from __future__ import annotations

from .._version import version


HEADER: str = f"""\
        #!/usr/bin/env python3
        #
        #  CAVEAT UTILITOR
        #
        #  This file was automatically generated by 竜TatSu v{version}
        #
        #     https://pypi.python.org/pypi/tatsu/
        #
        #  Any changes you make to it will be overwritten the next time
        #  the file is generated.
        #
        # noqa # type: ignore # ruff: noqa
        # copyright: ignore
        # fmt: off

        from __future__ import annotations
    """.rstrip()


IMPORTS: str = """
        from typing import Any

        from tatsu import decorators as tatsu
        from tatsu.contexts import Ctx
        from tatsu.config import ParserConfig
        from tatsu.parsing import Parser
        from tatsu.input.buffer import Buffer
        from tatsu.input.textlines import TextLines
    """.rstrip()


def PARSER_BODY(rules_name: str) -> str:
    return f"""\
        config = ParserConfig.new(config, **settings)
        rulessource = {rules_name}()
        assert isinstance(config, ParserConfig)

        super().__init__(rulessource, config=config)
    """.rstrip()


def FOOTER(name: str) -> str:
    return f"""\
        def main(filename, **kwargs):
            if not filename or filename == '-':
                import sys

                text = sys.stdin.read()
            else:
                import pathlib

                text = pathlib.Path(filename).read_text()

            parser = {name}Parser()
            return parser.parse(text, filename=filename, **kwargs)


        if __name__ == '__main__':
            import json
            from tatsu.parsing import generic_main
            from tatsu.util import asjson

            ast = generic_main(main, {name}Parser, name='{name}')
            data = asjson(ast)
            print(json.dumps(data, indent=2))
    """.rstrip()
