# shellcheck shell=dash

___x_cmd log init slash

xrc:mod:lib     slash   ls run compile init cat get preset/_index enable disable

___X_CMD_SLASH_CONFIG="$___X_CMD_ROOT_CFG/slash/config.yml"

___x_cmd_slash___main(){
    [ "$#" -gt 0 ] ||   set -- --help

    local op="$1";      shift
    case "$op" in
        -h|--help)      ___x_cmd help -m slash      "$@" ;      return 0 ;;
        init|ls|run|compile|runname|check|cat|get|enable|disable)
                        ___x_cmd_slash_"$op"        "$@" ;;
        *)              ___x_cmd_slash_runname "$op" "$@" ;;
    esac
}

___x_cmd_slash___set_status(){
    local name="$1" new_status="$2"
    local cfgfp="$___X_CMD_SLASH_CONFIG"
    local tmpfp="${cfgfp}.tmp.$$"
    local cur_name="" found=0 item_status=""

    while IFS= read -r line; do
        case "$line" in
            [!\ ]*:*)
                cur_name="${line%:}"
                item_status=""
                ;;
            "  status:"*)
                item_status="${line#  status: }"
                [ "$cur_name" = "$name" ] && {
                    if [ "$item_status" = "$new_status" ]; then
                        found=2
                    else
                        printf "  status: %s\n" "$new_status" >> "$tmpfp"
                        found=1
                        continue
                    fi
                }
                ;;
        esac
        printf "%s\n" "$line" >> "$tmpfp"
    done < "$cfgfp"

    if [ "$found" -eq 0 ]; then
        ___x_cmd rmrf "$tmpfp"
        slash:warn "Slash command not found: $name"
        return 1
    elif [ "$found" -eq 2 ]; then
        ___x_cmd rmrf "$tmpfp"
        slash:info "Already $new_status: $name"
        return 1
    fi

    ___x_cmd_cmds mv "$tmpfp" "$cfgfp"
    slash:info "$new_status: $name"
    return 0
}

___x_cmd_slash_check(){
    [ ! -f "$___X_CMD_SLASH_CONFIG" ] || {
        slash:info "Config file exists, skipping compilation"
        return 0
    }
    ___x_cmd_slash_compile
}

___x_cmd_slash_check___inner(){
    [ -f "$___X_CMD_SLASH_CONFIG" ] || ___x_cmd_slash_compile
}

___x_cmd_slash___filelist(){
    local type dir_name fp glob tool_dir
    for type in command skill; do
        case "$type" in
            command) dir_name="commands"; glob="*.md" ;;
            skill)   dir_name="skills"  ; glob="*/SKILL.md" ;;
        esac
        for tool_dir in .claude .agents .codex .config/opencode .openclaw; do
            for dir in "$tool_dir/$dir_name" "$HOME/$tool_dir/$dir_name"; do
                [ -d "$dir" ] || continue
                ___x_cmd fsiter --glob "$dir" "$glob" | while read -r fp; do
                    printf "%s\t%s\n" "$type" "$dir/$fp"
                done
            done
        done
    done

    ___x_cmd_slash_preset___filelist
}

___x_cmd_slash_findfp_(){
    x_=""
    local name="$1"
    local cfgfp="$___X_CMD_SLASH_CONFIG"

    # 1. try cache first
    if [ -f "$cfgfp" ]; then
        local cur_name="" fp="" item_status=""
        while IFS= read -r line; do
            case "$line" in
                [!\ ]*:*)
                    [ "$cur_name" = "$name" ] && [ "$item_status" = "enable" ] && { x_="$fp"; return 0; }
                    cur_name="${line%:}"
                    fp=""; item_status=""
                    ;;
                "  fp:"*)
                    fp="${line#  fp: }"
                    ;;
                "  status:"*)
                    item_status="${line#  status: }"
                    ;;
            esac
        done < "$cfgfp"
        [ "$cur_name" = "$name" ] && [ "$item_status" = "enable" ] && { x_="$fp"; return 0; }
    fi

    # 2. fallback to filesystem scan
    x_="$(
        ___x_cmd_slash___filelist | \
            SEARCH_NAME="$name" OUTPUT_MODE=path \
            ___x_cmd cawk -f "$___X_CMD_ROOT_MOD/slash/lib/awk/scan.awk"
    )" || return 1
    [ -n "$x_" ] || return 1
}

# Shell-specific dispatch function definitions
case "$___X_CMD_SHELL" in
    dash)
        # shellcheck disable=SC2139
        ___x_cmd_slash___def(){
            local name="$1"
            alias "/$name=___x_cmd_slash_runname \"$name\""
        }
        ;;

    ash|bash|zsh)
        ___x_cmd_slash___def(){
            local name="$1"
            eval "/$name(){
                ___x_cmd_slash_runname \"$name\" \"\$@\"
            }"
        }

        ;;

esac
