# shellcheck shell=dash

# In 1min ...
___x_cmd_hn_story_ls(){
    [ $# -gt 0 ]    ||  N=hn M="Please provide topic -> top new best ask show job" log:ret:64
    local type="${1}"; shift
    local style=""
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --app|--csv|--tsv|--yml|--json|--preview)
                            style="${1#--}" ;;
            -h|--help)      ___x_cmd help -m hn "$type"; return ;;
            *)              break ;;
        esac
        shift
    done

    local num="${2:-10}"
    [ -n "$style" ] || {
        style=app
        { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty; } || style=preview
    }

    ___x_cmd_hn_story_ls___"$style"
}

___x_cmd_hn_story_ls___app(){         ___x_cmd_hn___app "$type" ; }
___x_cmd_hn_story_ls___preview(){     ___x_cmd_hn_live___data "$type" "$num" | ___x_cmd_hn___story_preview_style; }

___x_cmd_hn_story_ls___json(){        ___x_cmd_hn_live___data "$type" "$num" | ___x_cmd_hn___story_json_style;  }
___x_cmd_hn_story_ls___yml(){         ___x_cmd_hn_story_ls___json | ___x_cmd json2yml;      }

___x_cmd_hn_story_ls___tsv(){         ___x_cmd_hn_story_ls___csv | ___x_cmd tsv fromcsv; }
___x_cmd_hn_story_ls___csv(){         ___x_cmd_hn_live___data "$type" "$num" | ___x_cmd_hn___story_csv_style; }

# TODO: ___x_cmd_hn_live___data -> ___x_cmd_hn_story_ls___raw
___x_cmd_hn_live___data(){
    local type="${1:-top}"
    case "$type" in
        new|top|best|ask|show|job)  ;;
        *)  N=hn M="Not found such [type=$type]" log:ret:64 ;;
    esac

    local start_num=1; local end_num=30
    case "$2" in
        [1-9]*,[1-9]*)  start_num="${2%%,*}";   end_num="${2#*,}"   ;;
        [1-9]*)                                 end_num="${2}"      ;;
        "") ;;
        *)              N=hn M="Please provide an integer num" log:ret:64 ;;
    esac

    start_num=$((start_num + 0))
    end_num=$((end_num + 0))
    hn:info "live data [type=$type] [start=$start_num] [end=$end_num]" # 2>>"$HOME/hn.log"

    # local id=""; local arg=""
    ___x_cmd_hn_topic "$type"                           | \
        ___x_cmd jo list2line                           | \
        ___x_cmd_cmds sed -n "${start_num},${end_num}p" | \
        ___x_cmd line args '___x_cmd_hn___story_data "$@"'

        # {
        #     while read -r id; do
        #         hn:debug "[type=$type] [id=$id]"
        #         arg="$arg $id"
        #     done
        #     eval ___x_cmd_hn___story_data "$arg"
        # }
}


