from __future__ import annotations

import json
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "data"


def load_settings() -> dict:
    path = DATA / "settings.json"
    if not path.exists():
        return {}
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except Exception:
        return {}


def main() -> int:
    settings = load_settings()
    source_mode = not bool(getattr(sys, "frozen", False))
    source_direct = bool(settings.get("source_mode_close_direct", True))
    close_to_tray = bool(settings.get("close_to_tray", True))
    source_force_os_exit = bool(settings.get("source_mode_force_os_exit", True)) or os.environ.get("NEXAOFFICE_SOURCE_FORCE_EXIT") == "1"
    effective_close_to_tray = close_to_tray
    if source_mode and source_direct:
        effective_close_to_tray = False

    payload = {
        "source_mode": source_mode,
        "source_mode_close_direct": source_direct,
        "close_to_tray": close_to_tray,
        "source_mode_force_os_exit": source_force_os_exit,
        "effective_close_to_tray": effective_close_to_tray,
        "expected_x_action": "hide_to_tray" if effective_close_to_tray else "direct_exit",
        "ctrl_q_enabled": True,
    }
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
