# shellcheck shell=dash

___x_cmd_gh_search(){
    param:scope  ___x_cmd_github
    param:subcmd ___x_cmd_gh_search     \
        "repo"       "search repo"      \
        "topic"      "search topic"
    param:subcmd:try
    param:run

    ___x_cmd_gh_search _param_help_doc
    return 1
}
# https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28#search-repositories
___x_cmd_gh_search_repo(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The query contains one or more search keywords and qualifiers"         <>=""
    --per_page      "Results per page"                                                      <>="30"
    --page          "Page number of the results to fetch."                                  <>="1"
    --topic         "Filter by topic"                                                       <>=""
    --sort          "sort"                                                                  <>=""      = stars forks help-wanted-issues updated
    --order         "order"                                                                 <>="desc"  = desc asc
    --json|-j       "output origin json data"
'
    param:run
    local q="$1"
    if [ -n "$topic" ]; then
        if [ -n "$q" ]; then
            q="${q}+topic:${topic}"
        else
            q="topic:${topic}"
        fi
    fi
    [ -n "$q" ] || M='Please provide search keywords' arg:ret:64

    if ___x_cmd_gx_output_is_format; then
         ___x_cmd_gh_get_multi "/search/repositories" sort order q | ___x_cmd_gx_output_format
    else
         ___x_cmd_gh_get_multi "/search/repositories" sort order q | \
            ___x_cmd jo .items                                                    | \
            ___x_cmd jo 2c            .full_name  .visibility .description        | \
            ___x_cmd csv header --add  RepoPath    Visibility  Description        | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    ___x_cmd csv app
            fi
    fi
}

___x_cmd_gh_search_topic(){
    param:scope     ___x_cmd_github
    param:dsl       '
options:
    #1              "The query contains one or more search keywords and qualifiers"         <>:String
    --per_page      "Results per page"                                                      <>="30"
    --page          "Page number of the results to fetch."                                  <>="1"

    --json|-j       "output origin json data"
'
    param:run
    local q="$1"

    if ___x_cmd_gx_output_is_format; then
         ___x_cmd_gh_get_multi "/search/topics" q | ___x_cmd_gx_output_format
    else
         ___x_cmd_gh_get_multi "/search/topics" q | \
            ___x_cmd jo .items                                            | \
            ___x_cmd jo 2c            .name  .created_at  .updated_at     | \
            ___x_cmd csv header --add  Name   Created_at   Updated_at     | \
            if [ -n "$csv" ]; then  ___x_cmd_cmds cat
            else                    ___x_cmd csv app
            fi
    fi
}