# shellcheck shell=dash

___x_cmd_mac_hidden(){
    [ "$#" -gt 0 ] || set -- --app

    local op="$1";  shift
    case "$op" in
        --app)
            ___x_cmd_mac_hidden___app "$@" ;;
        s|status)
            ___x_cmd_mac_hidden_status "$@" ;;
        show)
            ___x_cmd_mac_hidden_show ;;
        hide)
            ___x_cmd_mac_hidden_hide ;;
    esac
}

___x_cmd_mac_hidden___app(){
    ___x_cmd_mac_hidden_status

    local id;   local cmd
    ___x_cmd ui select id,cmd "What to do next?"   \
        "x mac hidden show      # Show all hidden files"    \
        "x mac hidden hide      # Hide all hidden files"    \
        "return 1" || return 1
    eval "$cmd" 
}

# TODO: The domain/default pair of (com.apple.finder, AppleShowAllFiles) does not exist
___x_cmd_mac_hidden_status(){
    if [ "$(___x_cmd_mac___cmd defaults read com.apple.finder AppleShowAllFiles)" = 1 ]; then
        printf "Hidden Files: %s\n" show
    else
        printf "Hidden Files: %s\n" hide
    fi
}

___x_cmd_mac_hidden_show(){
    ___x_cmd_mac___cmd defaults write com.apple.finder AppleShowAllFiles YES
    ___x_cmd_mac___cmd killall Finder
}

___x_cmd_mac_hidden_hide(){
    ___x_cmd_mac___cmd defaults write com.apple.finder AppleShowAllFiles NO
    ___x_cmd_mac___cmd killall Finder
}
