# shellcheck shell=dash
___x_cmd log init sed
xrc:mod:lib     sed     fzf/_index

___x_cmd_sed___main(){
    [ "$#" -gt 0 ] || set -- --auto

    local op="$1"; shift
    case "$op" in
        -h|--help)          ___x_cmd_sed___help         "$@" ;;
        ,|，|--co)          ___x_cmd_sed_co             "$@" ;;

        --auto)             ___x_cmd_sed___auto         "$@" ;;
        --fzfapp|--fzfapppreview|--fzfprompt)
                            ___x_cmd_sed___"${op#--}"   "$@" ;;
        --fzdata)
                            ___x_cmd_sed___fz_data      "$@" ;;
        *)                  ___x_cmd_cmds sed "$op"     "$@" ;;
    esac
}

___x_cmd_sed___help(){
    ___x_cmd help -m sed
}

___x_cmd_sed___auto(){
    if ___x_cmd_is_stdout2tty; then
        ___x_cmd_sed___fzfapp "$@"
    else
        ___x_cmd_cmds sed "$@"
    fi
}



___x_cmd_sed_co(){
    [ "$#" -gt 0 ] || set -- "$(cat)"
    local IFS=" "
    local user_message="$___X_CMD_CHAT_PROMPT_CODE
        IMPORTANT: ONLY command line using sed command as output, return only plaintext.
        You MUST always follow them. No exceptions.

        Example: change the content of the file test.txt from hi to hello
        sed s/hi/hello/ test.txt

        Example: count the word x-cmd using sed command
        sed -n 's/\bx-cmd\b/&/gp' test.txt | wc -l

        prompt: $*
    "
    local msg; msg='{
        role: user,
        content: '$(x chat --jqu "$user_message")'
    }'

    # Consider refine the code ...
    ___x_cmd chat -n 0 --request co,sed "" "$msg"
}

