from typing import (
    Dict,
    List,
    Optional,
)

from nreports.repository import (
    I18NRepository,
    L10NRepository,
)
from nreports_api.shared import JSON
from ._Line import Line


class Report(object):

    def __init__(
        self,
        *args: List[Dict[str, JSON]],
    ) -> None:

        self.lines: List[Line] = []
        for line in args[0]:
            response = Line.build(line=line)
            if isinstance(response, list):
                self.lines.extend(response)
            else:
                self.lines.append(response)

    def get_data_to_build(
        self,
    ) -> Dict[str, Optional[Dict[str, None]]]:

        ret = {}
        for line in self.lines:
            for name, items in line.get_data_to_build().items():
                ret[name] = items

        return ret

    def format_data(
        self,
        built_data: Dict[str, JSON],
        i18n_repository: I18NRepository,
        l10n_repository: L10NRepository,
    ) -> List[Dict[str, str]]:

        ret = []
        for line in self.lines:
            ret.extend(
                line.format(
                    data=built_data,
                    i18n_repository=i18n_repository,
                    l10n_repository=l10n_repository,
                )
            )

        return ret
