from moonleap import u0
from moonleap.utils.inflect import plural
from moonleap.utils.magic_replace import magic_replace
from titan.api_pkg.pkg.ml_name import ml_type_spec_from_item_name
from titan.react_pkg.pkg.ts_var import ts_type, ts_type_import_path
from titan.react_state_pkg.itemview.props import get_item_view_route_params
from titan.react_view_pkg.router import RouterConfig


def create_router_configs(self):
    return [RouterConfig(component=self, url="")]


effect_args_template = """
type ArgsT = {
    yellowTulip
};
"""


def get_context(select_item_effect):
    _ = lambda: None
    _.route_params = get_item_view_route_params(select_item_effect.item_list.item_name)
    _.item_name = select_item_effect.item_list.item_name
    _.items_name = plural(_.item_name)
    _.item_ts_type = ts_type(select_item_effect.item_list.item)
    _.item_ts_type_import_path = ts_type_import_path(select_item_effect.item_list.item)
    _.type_spec = ml_type_spec_from_item_name(_.item_name)

    class Sections:
        def effect_args(self):
            args = ", ".join(
                [f"{route_param}: string" for route_param in _.route_params]
            )

            return magic_replace(
                effect_args_template,
                [
                    ("yellowTulip", args),
                ],
            )

        def declare_params(self):
            return "{ " + ", ".join(_.route_params) + " }"

        def extract_params(self):
            return ", ".join(
                [
                    f"{route_param}: params.{route_param}"
                    for route_param in _.route_params
                ]
            )

        def get_item_id(self):
            search_function = "(x) => " + " && ".join(
                [
                    f"x.{param} === {_.item_name + u0(param)}"
                    for param in _.type_spec.query_item_by or []
                ]
            )
            return (
                ""
                if _.route_params == [f"{_.item_name}Id"]
                else f"const {_.item_name}Id = "
                + f"R.find({search_function})(props.{_.items_name})?.id"
            )

    return dict(sections=Sections(), _=_)

from moonleap import u0
from moonleap.utils.inflect import plural
from moonleap.utils.magic_replace import magic_replace
from titan.api_pkg.pkg.ml_name import ml_type_spec_from_item_name
from titan.react_pkg.pkg.ts_var import ts_type, ts_type_import_path
from titan.react_state_pkg.itemview.props import get_item_view_route_params
from titan.react_view_pkg.router import RouterConfig


def create_router_configs(self):
    return [RouterConfig(component=self, url="")]


effect_args_template = """
type ArgsT = {
    yellowTulip
};
"""


def get_context(select_item_effect):
    _ = lambda: None
    _.route_params = get_item_view_route_params(select_item_effect.item_list.item_name)
    _.item_name = select_item_effect.item_list.item_name
    _.items_name = plural(_.item_name)
    _.item_ts_type = ts_type(select_item_effect.item_list.item)
    _.item_ts_type_import_path = ts_type_import_path(select_item_effect.item_list.item)
    _.type_spec = ml_type_spec_from_item_name(_.item_name)

    class Sections:
        def effect_args(self):
            args = ", ".join(
                [f"{route_param}: string" for route_param in _.route_params]
            )

            return magic_replace(
                effect_args_template,
                [
                    ("yellowTulip", args),
                ],
            )

        def declare_params(self):
            return "{ " + ", ".join(_.route_params) + " }"

        def extract_params(self):
            return ", ".join(
                [
                    f"{route_param}: params.{route_param}"
                    for route_param in _.route_params
                ]
            )

        def get_item_id(self):
            search_function = "(x) => " + " && ".join(
                [
                    f"x.{param} === {_.item_name + u0(param)}"
                    for param in _.type_spec.query_item_by or []
                ]
            )
            return (
                ""
                if _.route_params == [f"{_.item_name}Id"]
                else f"const {_.item_name}Id = "
                + f"R.find({search_function})(props.{_.items_name})?.id"
            )

    return dict(sections=Sections(), _=_)

type ArgsT = {     yellowTulip };