# shellcheck shell=dash

# Section: me - user info, teams, websites
# https://v2.umami.is/docs/api/me-api
___x_cmd_umami_me_ls(){
    [ "$#" -gt 0 ] || set -- --json
    local op="$1"; shift
    case "$op" in
        --json|--yml|--raw|--app)
            ___x_cmd_umami_me_ls___raw "$@" ;;
        --me)       ___x_cmd_umami_me_ls___me   "$@" ;;
        --teams)    ___x_cmd_umami_me_ls___teams "$@" ;;
        --websites) ___x_cmd_umami_me_ls___websites "$@" ;;
        -h|--help)  ___x_cmd help -m umami me ls "$@" ;;
        *)          ___x_cmd_umami_me_ls___raw --json "$op" "$@" ;;
    esac
}

# GET /api/me - user info
___x_cmd_umami_me_ls___me(){
    ___x_cmd_umami_api get "/me"
}

# GET /api/me/teams - user teams
___x_cmd_umami_me_ls___teams(){
    ___x_cmd_umami_api get "/me/teams"
}

# GET /api/me/websites - user websites
___x_cmd_umami_me_ls___websites(){
    ___x_cmd_umami_api get "/me/websites"
}

___x_cmd_umami_me_ls___raw(){
    local output_format="json"
    local subcmd="--me"

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --json|--yml|--raw|--app) output_format="${1#--}"; shift ;;
            --me|--teams|--websites) subcmd="${1#--}"; shift ;;
            *) break ;;
        esac
    done

    local data
    case "$subcmd" in
        me)       data="$(___x_cmd_umami_me_ls___me)" ;;
        teams)    data="$(___x_cmd_umami_me_ls___teams)" ;;
        websites)  data="$(___x_cmd_umami_me_ls___websites)" ;;
    esac

    case "$output_format" in
        json|raw)  printf "%s\n" "$data" ;;
        yml)       printf "%s\n" "$data" | ___x_cmd j2y ;;
        app)
            if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
                printf "%s\n" "$data" | ___x_cmd j2y | ___x_cmd csv app
            else
                printf "%s\n" "$data"
            fi
            ;;
    esac
}
# EndSection
