# shellcheck shell=dash

xrc cfgy

___x_cmd_hotkey_co_cfg(){
    [ "$#" -gt 0 ] || {
        ___x_cmd_hotkey_co_init
        return
    }

    local op="$1"; shift
    case "$op" in
        -h|--help)          ___x_cmd help -m hotkey co --cfg ;;
        *)                  ___x_cmd_hotkey_co_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_hotkey_co_cur(){
    local X_help_cmd=; X_help_cmd='___x_cmd help -m hotkey co --cur' help:arg:parse
    ___x_cmd_hotkey_co_cfg --current "$@"
}

___X_CMD_HOTKEY_CO_CFG_VARLIST="emoji,aicmd"

___x_cmd_hotkey_co_init(){
    local cur_emoji=; local cur_aicmd=;
    if [ -f "$(___x_cmd_hotkey_co_cur --get config)" ]; then
        ___x_cmd_hotkey_co_cur cur_emoji:=emoji cur_aicmd:=aicmd
    fi

    printf "\n"
    printf "  🤖 Welcome to x-cmd hotkey co configuration\n"
    printf "\n"
    printf "  This widget lets you send \"command not found\" errors to an AI\n"
    printf "  for intelligent assistance. Press Ctrl+X to activate.\n"
    case "$BASH_VERSION" in
        3.*) printf "\n  Note: Bash 3 requires pressing Ctrl+X twice (technical limitation).\n" ;;
    esac
    printf "\n"
    printf "  Let's set up your preferences:\n"
    printf "\n"

    ___x_cmd_hotkey_co_cfg___invoke --init --ctrl_exit_strategy     \
        aicmd  "AI Command (how to invoke the AI assistant)"        \
               "${cur_aicmd:-"kimi -c"}"                            \
               '='  'kimi -c'  'claude -p'  'x co --exec' --        \
        emoji  "Emoji Indicator (visual feedback when active)"      \
               "${cur_emoji:-"🤖"}"                                 \
               '='  "🤖"  "💫"  "✨"  "🔥"  "📖"  "⏳"  "🔍"   || return $?

    ___x_cmd_hotkey_co_cur cur_emoji:=emoji cur_aicmd:=aicmd 2>/dev/null
    ___X_CMD_HOTKEY_CO_EMOJI="$cur_emoji"

    printf "\n"
    printf "  ✓ Configuration saved successfully!\n"
    printf "\n"
    printf "    AI Command: %s\n" "$cur_aicmd"
    printf "    Emoji:      %s\n" "$cur_emoji"
    printf "\n"
    printf "  Press Ctrl+X in your shell to try it out.\n"
    printf "  Run 'x hotkey use co' to enable permanently.\n"
    printf "\n"
}

___x_cmd_hotkey_co_cfg___invoke(){
    ___x_cmd_cfgy_obj                                               \
        --prefix            ___X_CMD_HOTKEY_CO_CFG_DATA                  \
        --default-config    "${___X_CMD_ROOT_CFG}/hotkey/co/X.cfg.yml"   \
        --current-config    "${___X_CMD_HOTKEY_CO_LOCAL_CONFIG}"         \
        --current-profile   "${___X_CMD_HOTKEY_CO_LOCAL_PROFILE}"        \
        --varlist           "$___X_CMD_HOTKEY_CO_CFG_VARLIST"            \
        "$@"
}
