#!/usr/bin/env python3
#
# Copyright (c) 2026 Stormux
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

"""Normalized compositor-state event vocabulary and snapshots."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Mapping, Optional

DESKTOP_FOCUS_CONTEXT_CHANGED = "DESKTOP_FOCUS_CONTEXT_CHANGED"
PRIORITIZE_FOCUS = "PRIORITIZE_FOCUS"
WORKSPACE_STATE_CHANGED = "WORKSPACE_STATE_CHANGED"
DESKTOP_TRANSITION_STARTED = "DESKTOP_TRANSITION_STARTED"
DESKTOP_TRANSITION_FINISHED = "DESKTOP_TRANSITION_FINISHED"
PAUSE_ATSPI_CHURN = "PAUSE_ATSPI_CHURN"
RESUME_ATSPI_CHURN = "RESUME_ATSPI_CHURN"
FLUSH_STALE_ATSPI_EVENTS = "FLUSH_STALE_ATSPI_EVENTS"


@dataclass(frozen=True, slots=True)
class DesktopContextSnapshot:
    """Immutable snapshot of the desktop context tracked by the adapter."""

    session_type: str
    backend_name: str = ""
    active_workspace_ids: frozenset[str] = field(default_factory=frozenset)
    workspace_transition_pending: bool = False
    active_window_token: str = ""
    locus_of_focus_token: str = ""
    active_window_pid: int = -1
    locus_of_focus_pid: int = -1
    active_window_name: str = ""
    locus_of_focus_name: str = ""


@dataclass(frozen=True, slots=True)
class CompositorStateEvent:
    """Normalized event emitted by the compositor state adapter."""

    type: str
    reason: str = ""
    snapshot: Optional[DesktopContextSnapshot] = None
    payload: Mapping[str, Any] = field(default_factory=dict)
