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

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 typing import List, Optional
from itertools import chain
from ruamel.yaml import YAML

from ..tiramisu import normalize_family
from ..convert.path import Paths

# from ..error import DictConsistencyError
# from ..i18n import _


class Walker:
    def __init__(
        self,
        convert,
    ) -> None:
        """Parse directories content"""
        self.convert = convert
        self.yaml = YAML()
        rougailconfig = self.convert.rougailconfig
        if rougailconfig["main_namespace"]:
            self.load_with_extra(
                rougailconfig["extra_namespaces"],
                rougailconfig["main_namespace"],
                rougailconfig["main_structural_strings"],
                rougailconfig["isolated_namespace"],
            )
        else:
            self.convert.namespace = None
            for string in rougailconfig["main_structural_strings"]:
                self.parse_variable(
                    string,
                )

    def load_with_extra(
        self,
        extra_structures: dict,
        main_namespace: str,
        main_strings: Optional[List[str]] = None,
        isolated_namespace: bool = True,
    ) -> None:
        directory_dict = chain(
            (
                (
                    main_namespace,
                    main_strings,
                ),
            ),
            extra_structures.items(),
        )
        for namespace, extra_objects in directory_dict:
            namespace_path = normalize_family(namespace)
            for idx, string in enumerate(extra_objects):
                if not idx:
                    # create only for the first file
                    self.convert.create_namespace(namespace, isolated_namespace)
                self.parse_variable(
                    string,
                )

    def parse_variable(
        self,
        string: str,
    ) -> None:
        objects = self.yaml.load(string)
        if objects is None:
            return
        path = self.convert.namespace
        if path:
            name = f"yaml file for {path}"
        else:
            name = "yaml file"
        version = self.convert.validate_file_version(
            objects,
            name,
        )
        self.convert.parse_root_file(
            [name],
            path,
            version,
            objects,
        )


__all__ = ("Walker",)
