# shellcheck shell=dash

# Section: list collections
# https://ossinsight.io/docs/api/list-collections
___x_cmd_ossinsight_collection_ls(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_ossinsight_collection_ls___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_collection_ls___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_collection_ls___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_collection_ls___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_collection_ls___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_collection_ls___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_collection_ls___csv --no-header "$@" ;;
        -h|--help)    ___x_cmd help -m ossinsight collection ls "$@" ;;
        *)            ___x_cmd_ossinsight_collection_ls___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_collection_ls___raw(){
    ___x_cmd_ossinsight_api get "/collections"
}

___x_cmd_ossinsight_collection_ls___json(){
    ___x_cmd_ossinsight_collection_ls___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_collection_ls___yml(){
    ___x_cmd_ossinsight_collection_ls___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_collection_ls___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_collection_ls___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .id .name \
        | ___x_cmd csv header $csv_flags id name
}

___x_cmd_ossinsight_collection_ls___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "id\tname\n"
    fi
    ___x_cmd_ossinsight_collection_ls___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .id .name \
        | tr ',' '\t'
}

___x_cmd_ossinsight_collection_ls___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_collection_ls___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_collection_ls___tsv "$@"
    fi
}
# EndSection

# Section: list hot collections
# https://ossinsight.io/docs/api/list-hot-collections
___x_cmd_ossinsight_collection_hot(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_ossinsight_collection_hot___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_collection_hot___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_collection_hot___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_collection_hot___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_collection_hot___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_collection_hot___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_collection_hot___csv --no-header "$@" ;;
        -h|--help)    ___x_cmd help -m ossinsight collection hot "$@" ;;
        *)            ___x_cmd_ossinsight_collection_hot___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_collection_hot___raw(){
    ___x_cmd_ossinsight_api get "/collections/hot"
}

___x_cmd_ossinsight_collection_hot___json(){
    ___x_cmd_ossinsight_collection_hot___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_collection_hot___yml(){
    ___x_cmd_ossinsight_collection_hot___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_collection_hot___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_collection_hot___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .id .name .repos .repo_id .repo_name .repo_current_period_rank .repo_past_period_rank .repo_rank_changes \
        | ___x_cmd csv header $csv_flags id name repos repo_id repo_name current_rank past_rank rank_changes
}

___x_cmd_ossinsight_collection_hot___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "id\tname\trepos\trepo_id\trepo_name\tcurrent_rank\tpast_rank\trank_changes\n"
    fi
    ___x_cmd_ossinsight_collection_hot___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .id .name .repos .repo_id .repo_name .repo_current_period_rank .repo_past_period_rank .repo_rank_changes \
        | tr ',' '\t'
}

___x_cmd_ossinsight_collection_hot___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_collection_hot___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_collection_hot___tsv "$@"
    fi
}
# EndSection