"""Shared axes for formal creation-query families."""
from __future__ import annotations

import json
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
TREES = ROOT / "scope_trees" / "trees_index.json"

ACTIONS = ["构思", "策划", "组织", "撰写", "改编", "润色"]
STAGES = ["灵感", "选题", "脚本"]
KTYPE_SUFFIX = {"what": "有哪些", "why": "为什么", "how": "怎么做"}


def _nonleaf_d4(
    source_type: str,
    limit: int,
    under: str | None = None,
    *,
    tree_path: Path = TREES,
) -> list[str]:
    """Return level-4 non-leaf node names, preserving the legacy axis order."""
    idx = json.loads(tree_path.read_text("utf-8"))
    paths = {
        n.get("path") or ""
        for n in idx
        if n.get("source_type") == source_type
        and (not under or under in (n.get("path") or "").split("/"))
    }
    d4 = [p for p in paths if len([x for x in p.split("/") if x]) == 4]
    nonleaf = sorted(p for p in d4 if any(o != p and o.startswith(p + "/") for o in paths))
    return [p.split("/")[-1] for p in nonleaf][:limit]
