# shellcheck shell=dash

___x_cmd_mac_vol(){
    [ "$#" -gt 0 ] || set -- status

    local op="$1";  shift
    case "$op" in
        ismute|get|set)
                        ___x_cmd_mac_vol_"$op"              "$@" ;;

        m|mute)         ___x_cmd_mac_vol_mute               "$@" ;;
        u|unmute)       ___x_cmd_mac_vol_unmute             "$@" ;;
        s|status)       ___x_cmd_mac_vol_status             "$@" ;;

        -h|--help)      ___x_cmd help -m mac vol            "$@" ;;

        *)              ___x_cmd_mac_vol___default "$op"    "$@" ;;
    esac
}

___x_cmd_mac_vol_ismute(){
    local mute_status=
    mute_status="$(___x_cmd_mac___cmd osascript -e "get output muted of (get volume settings)")"
    [ "$mute_status" = "true" ]
}

___x_cmd_mac_vol_mute(){
    ___x_cmd_mac___cmd osascript -e "set volume output muted true"
}

___x_cmd_mac_vol_unmute(){
    ___x_cmd_mac___cmd osascript -e "set volume output muted false"
}

___x_cmd_mac_vol_status(){
    if ___x_cmd_mac_vol_ismute; then
        printf "%s: %s\n" is-mute yes
    else
        printf "%s: %s\n" is-mute no
    fi

    printf "%s: %s\n" volume "$(___x_cmd_mac_vol_get)"
}

___x_cmd_mac_vol_get(){
    ___x_cmd_mac___cmd osascript -e "output volume of (get volume settings)"
}

___x_cmd_mac_vol_set(){
    local volume="$1"
    [ "$volume" -lt 100 ]   || return $?
    [ "$volume" -gt 0 ]     || return $?
    ___x_cmd_mac___cmd osascript -e "set volume output volume $volume"
}

___x_cmd_mac_vol___default(){
    if ___x_cmd is int "$1"; then
        ___x_cmd_mac_vol_set "$1"
    else
        N=mac M="Unknown subcmd -> $op" log:ret:64
    fi
}
