# shellcheck shell=dash

___x_cmd_claw___advise_connect_candidate(){
    printf "%s\n" "weixin" "telegram" "qywx" "feishu:groups"
    ___x_cmd feishu group ls 2>/dev/null | {
        local _="" chatid=""
        IFS= read -r _
        while IFS=, read -r chatid _; do
            [ -n "$chatid" ] && printf "feishu:%s\n" "$chatid"
        done
    }
}

___x_cmd_claw_connect(){
    while [ $# -gt 0 ]; do
        case "$1" in
            --help|-h)  ___x_cmd help -m claw connect "$@"; return 0 ;;
            --ls|-l)    ___x_cmd_claw_run___list_activate; return ;;
            *)          break ;;
        esac
    done

    [ $# -gt 0 ] || N=claw M="Please provide IM platform name (e.g., weixin|telegram|feishu:oc_xxx)" log:ret:64

    while [ $# -gt 0 ]; do
        local item="$1" im="" chatid=""
        case "$item" in
            *:*)    im="${item%%:*}"; chatid="${item#*:}" ;;
            *)      im="$item"; chatid="" ;;
        esac
        ___x_cmd_claw_run___ensure_im_service "$im" "$chatid" || return $?
        ___x_cmd_claw_run___activate_im "$im" "$chatid"
        shift
    done
}

___x_cmd_claw_disconnect(){
    while [ $# -gt 0 ]; do
        case "$1" in
            --help|-h)  ___x_cmd help -m claw disconnect "$@"; return 0 ;;
            *)          break ;;
        esac
    done

    [ $# -gt 0 ] || N=claw M="Please provide IM platform name (e.g., weixin|telegram|feishu:oc_xxx)" log:ret:64

    while [ $# -gt 0 ]; do
        local item="$1" im="" chatid=""
        case "$item" in
            *:*)    im="${item%%:*}"; chatid="${item#*:}" ;;
            *)      im="$item"; chatid="" ;;
        esac
        ___x_cmd_claw_run___deactivate_im "$im" "$chatid" || return $?
        ___x_cmd_claw_run___stop_im_service "$im" "$chatid"
        shift
    done
}
