"""Federation defpage setup — registers layouts, page helpers, and loads .sx pages."""
from __future__ import annotations

from typing import Any


def setup_federation_pages() -> None:
    """Register federation-specific layouts, page helpers, and load page definitions."""
    _register_federation_layouts()
    _register_federation_helpers()
    _load_federation_page_files()


def _load_federation_page_files() -> None:
    import os
    from shared.sx.pages import load_page_dir
    load_page_dir(os.path.dirname(__file__), "federation")


# ---------------------------------------------------------------------------
# Layouts
# ---------------------------------------------------------------------------

def _register_federation_layouts() -> None:
    from shared.sx.layouts import register_custom_layout
    register_custom_layout("social", _social_full, _social_oob)


def _social_full(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx, header_child_sx
    from sx.sx_components import _social_header_sx

    actor = ctx.get("actor")
    root_hdr = root_header_sx(ctx)
    social_hdr = _social_header_sx(actor)
    child = header_child_sx(social_hdr)
    return "(<> " + root_hdr + " " + child + ")"


def _social_oob(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx, sx_call, SxExpr
    from sx.sx_components import _social_header_sx

    actor = ctx.get("actor")
    social_hdr = _social_header_sx(actor)
    child_oob = sx_call("oob-header-sx",
                         parent_id="root-header-child",
                         row=SxExpr(social_hdr))
    root_hdr_oob = root_header_sx(ctx, oob=True)
    return "(<> " + child_oob + " " + root_hdr_oob + ")"


# ---------------------------------------------------------------------------
# Page helpers
# ---------------------------------------------------------------------------

def _register_federation_helpers() -> None:
    from shared.sx.pages import register_page_helpers

    register_page_helpers("federation", {
        "home-timeline-content": _h_home_timeline_content,
        "public-timeline-content": _h_public_timeline_content,
        "compose-content": _h_compose_content,
        "search-content": _h_search_content,
        "following-content": _h_following_content,
        "followers-content": _h_followers_content,
        "actor-timeline-content": _h_actor_timeline_content,
        "notifications-content": _h_notifications_content,
    })


def _h_home_timeline_content():
    from quart import g
    return getattr(g, "home_timeline_content", "")


def _h_public_timeline_content():
    from quart import g
    return getattr(g, "public_timeline_content", "")


def _h_compose_content():
    from quart import g
    return getattr(g, "compose_content", "")


def _h_search_content():
    from quart import g
    return getattr(g, "search_content", "")


def _h_following_content():
    from quart import g
    return getattr(g, "following_content", "")


def _h_followers_content():
    from quart import g
    return getattr(g, "followers_content", "")


def _h_actor_timeline_content():
    from quart import g
    return getattr(g, "actor_timeline_content", "")


def _h_notifications_content():
    from quart import g
    return getattr(g, "notifications_content", "")
