# Copyright 2026, Jiri Eischmann <jiri@eischmann.cz> and the meshy contributors
# SPDX-License-Identifier: GPL-3.0-or-later

"""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)
