#!/usr/bin/env bash
# android_app_clear — Wipe app data + cache via pm clear. App stays installed.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./adb_wsl.sh
source "$SCRIPT_DIR/adb_wsl.sh"

# ---------------------------------------------------------------------------
# android_app_clear [--serial <S>] <package>
#
# --serial <S>  Optional target device/emulator serial.
# package       App package whose data+cache to clear (e.g. com.example.app).
#
# Calls: adb shell pm clear <package>
# The app remains installed but is reset to factory state (no data, no cache).
# Exit 0 on success, exit 1 on bad args, exit 3 if no device found.
# ---------------------------------------------------------------------------
android_app_clear() {
    adb_pick_serial "$@" || exit 3
    local serial="$ADB_PICK_SERIAL"
    set -- "${ADB_PICK_REST[@]}"

    local pkg="${1:-}"
    if [[ -z "$pkg" ]]; then
        echo "android_app_clear: se requiere <package> como argumento." >&2
        return 1
    fi

    adb_s "$serial" shell pm clear "$pkg"
    echo "cleared data for $pkg on $serial"
}

# Run directly if not sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    android_app_clear "$@"
fi
