"""
Per-env data_dir multiplexer.

PlatformIO's [platformio].data_dir is a single project-level setting — it
ignores `data_dir` inside [env:...] blocks. With more than one panel in the
tree (waveshare73-v1, waveshare13e6-v1), we need to route uploadfs to the
right LittleFS payload based on the active env.

Wired into envs via:
    extra_scripts = pre:scripts/data_dir.py

The script runs before SCons evaluates uploadfs and overrides
PROJECT_DATA_DIR for the envs in `ENV_TO_DATA`. Envs not listed fall
through to the project default (set in [platformio]).
"""

import os

Import("env")  # noqa: F821 — provided by PlatformIO's SCons context

ENV_TO_DATA = {
    "waveshare73-v1":    "waveshare73-v1",
    "test-display":      "waveshare73-v1",
    "sim-yellow":        "waveshare73-v1",
    "sim-red":           "waveshare73-v1",
    "waveshare13e6-v1":  "waveshare13e6-v1",
}

pioenv = env["PIOENV"]  # noqa: F821
if pioenv in ENV_TO_DATA:
    data_dir = os.path.join(env["PROJECT_DIR"], "data", ENV_TO_DATA[pioenv])  # noqa: F821
    env.Replace(PROJECT_DATA_DIR=data_dir)  # noqa: F821
    print(f"[data_dir] {pioenv} -> {data_dir}")
