"""
Config file for Rougail-doc

Silique (https://www.silique.fr)
Copyright (C) 2024-2025

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from pathlib import Path
from rougail.utils import load_modules, _


OUTPUTS = None


def get_outputs() -> None:
    """Load all outputs"""
    module_name = "rougail.output_doc.output"
    outputs = {}
    for path in (Path(__file__).parent / "output").iterdir():
        name = path.name
        if not name.endswith(".py") or name.endswith("__.py"):
            continue
        module = load_modules(module_name + "." + name[:-3], str(path))
        if "Formater" not in dir(module):
            continue
        level = module.Formater.level
        if level in outputs:
            raise ImportError(
                _('duplicated level rougail-doc for output "{0}": {1} and {2}').format(
                    level, module.Formater.name, outputs[level].name
                )
            )
        outputs[module.Formater.level] = module.Formater
    return {outputs[level].name: outputs[level] for level in sorted(outputs)}


class OutPuts:  # pylint: disable=R0903
    """Transformations applied on a object instance"""

    def __init__(
        self,
    ) -> None:
        global OUTPUTS
        if OUTPUTS is None:
            OUTPUTS = get_outputs()

    def get(self) -> dict:
        """Get all outputs"""
        return OUTPUTS


def get_rougail_config(
    *,
    backward_compatibility=True,  # pylint: disable=unused-argument
) -> dict:
    """Get documentation for output_doc modules"""
    outputs = list(OutPuts().get())
    output_format_default = outputs[0]
    rougail_options = f"""
doc:
  description: {_('Configuration rougail-doc')}
  disabled:
    jinja: |
      {{% if step.output is propertyerror or step.output != 'doc' %}}
      disabled
      {{% endif %}}

  title_level:
    description: {_('Starting title level')}
    alternative_name: dt
    default: 1

  example:
    description: {_('Generate example')}
    alternative_name: de
    default: false

  without_family:
    description: {_('Do not add families in documentation')}
    default: false

  disabled_modes:
    description: {_('Disable documentation for variables with those modes')}
    multi: true
    mandatory: false
    disabled:
      jinja: |
        {{% if not modes_level %}}
        there is no mode
        {{% endif %}}
      description: {_('disabled when there is no mode available')}
    validators:
      - jinja: |
          {{% if _.disabled_modes not in modes_level %}}
          this mode is not available
          {{% endif %}}
        description: {_('verify if disable modes already exists')}

  change_default_value:
    description: {_('Modify values to document leaderships and dynamics families')}
    default: true

  output_format:
    description: {_('Generate document in format')}
    alternative_name: do
    default: output_format_default
    choices:
""".replace(
        "output_format_default", output_format_default
    )
    for output in outputs:
        rougail_options += f"      - {output}\n"
    return {
        "name": "doc",
        "process": "output",
        "options": rougail_options,
        "allow_user_data": False,
        "level": 50,
    }


__all__ = ("OutPuts", "get_rougail_config")
