"""Resolve OpenClaw's state directory for standalone skill scripts.

Skill scripts may run outside the OpenClaw process, so this helper keeps
credential storage aligned with the runtime's state directory convention.
It honors ``OPENCLAW_STATE_DIR`` first, then ``OPENCLAW_HOME`` for older
launchers, and finally falls back to ``~/.openclaw``.

All scripts under ``google-workspace/scripts/`` should import from here
instead of duplicating state-directory resolution.
"""

from __future__ import annotations

import os
from pathlib import Path

def get_openclaw_state_dir() -> Path:
    """Return the OpenClaw state directory (default: ~/.openclaw)."""
    for env_name in ("OPENCLAW_STATE_DIR", "OPENCLAW_HOME"):
        val = os.environ.get(env_name, "").strip()
        if val:
            return Path(val).expanduser()
    return Path.home() / ".openclaw"


def display_openclaw_state_dir() -> str:
    """Return a user-friendly ``~/``-shortened display string."""
    state_dir = get_openclaw_state_dir()
    try:
        return "~/" + str(state_dir.relative_to(Path.home()))
    except ValueError:
        return str(state_dir)
