# shellcheck shell=bash disable=SC2154,2034

___x_cmd_shodan_query_ls(){
    # shodan:info "Obtain a list of search queries that users have saved in Shodan"
    local X_help_cmd='x help -m shodan query ls';   help:arg:parse
    shodan:arg:format:all
    "___x_cmd_shodan_query_ls___${format}"
}

___x_cmd_shodan_query_ls___app(){
    local res;  res="$( ___x_cmd_shodan_query_ls___csv )" || return
    [ -n "$res" ] || return
    local query=
    query="$(___x_cmd_pipevar res | x csv app --return print)"
    query="${query#*,*,}"; query="${query%%,*}"
    local id=
    x ui select id "Next for query=$query"  \
        "search" \
        "open browser to search"  \
        "ABORT" || return

    case "$id" in
        1)  x shodan search     "$query" ;;
        2)  x shodan open       "$query" ;;
        3)  return
    esac
}

___x_cmd_shodan_query_ls___app1(){
    local data; if data="$(___x_cmd_shodan_query_ls___raw)"; then
        ___x_cmd_pipevar data ___x_cmd_shodan_query_ls___parse_json2csvapp
    else
        ___x_cmd_pipevar data x j2y >&2
        return 1
    fi
}

___x_cmd_shodan_query_ls___csv(){
    local data
    data="$(___x_cmd_shodan_query_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data ___x_cmd_shodan_query_ls___parse_json2csv
}

___x_cmd_shodan_query_ls___tsv(){
    local data
    data="$(___x_cmd_shodan_query_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data ___x_cmd_shodan_query_ls___parse_json2csv | x csv 2tsv
}

___x_cmd_shodan_query_ls___json(){
    local data
    data="$(___x_cmd_shodan_query_ls___raw)"    || shodan:data:ret
    ___x_cmd_pipevar data
}

___x_cmd_shodan_query_ls___raw(){
    ___x_cmd_shodan_ourl get /shodan/query
}

# Section: Parser
___x_cmd_shodan_query_ls___parse_json2csv(){
    x jo . .matches |\
        x ja 2c .title .votes .query .description .timestamp  |\
        x csv header --add  Title Votes query Description Timestamp
}

___x_cmd_shodan_query_ls___parse_json2csvapp(){
    ___x_cmd_shodan_query_ls___parse_json2csv | \
        x csv app --width "10%,-,-,-,-" --return print
}

___x_cmd_shodan_query_ls___parse_jsonitem2navjson(){
    :
    # json item transformed to the dictionary
    # json item annotation
}
# EndSection
