"""Helpers for invoking host commands across Flatpak and native runtimes."""

from __future__ import annotations

import shutil
import subprocess
from collections.abc import Sequence
from pathlib import Path


def is_flatpak() -> bool:
    return Path('/.flatpak-info').exists()


def host_command(args: Sequence[str]) -> list[str]:
    if is_flatpak() and shutil.which('flatpak-spawn'):
        return ['flatpak-spawn', '--host', *args]
    return list(args)


def run_host_command(args: Sequence[str], **kwargs) -> subprocess.CompletedProcess:
    return subprocess.run(host_command(args), **kwargs)
