# shellcheck shell=dash

___x_cmd_slash_compile(){
    case "$1" in
        -h|--help) ___x_cmd help -m slash compile; return ;;
    esac

    local cfgfp="$___X_CMD_SLASH_CONFIG"
    ___x_cmd ensurefp "$cfgfp" || return $?

    # Step 1: scan sources into config
    ___x_cmd_slash_compile_scan || return $?

    # Step 2: compile shell code from config
    ___x_cmd_slash_compile_generate || return $?
}

___x_cmd_slash_compile_scan(){
    local cfgfp="$___X_CMD_SLASH_CONFIG"
    local data
    data="$(
        ___x_cmd_slash___filelist | \
            CFGFP="$cfgfp" OUTPUT_MODE=yml ___x_cmd cawk -f "$___X_CMD_ROOT_MOD/slash/lib/awk/scan.awk"
    )" || return $?

    [ -n "$data" ] || {
        slash:warn "No slash commands or skills found in .claude/{commands,skills} or ~/.claude/{commands,skills}"
        return 1
    }

    printf "%s\n" "$data" > "$cfgfp"
    local count=0
    count="$(printf "%s\n" "$data" | grep -c '^[a-zA-Z0-9_-]*:$')" || count=0
    slash:info "Scanned and registered $count items"
}

___x_cmd_slash_compile_generate(){
    local cfgfp="$___X_CMD_SLASH_CONFIG"
    [ -f "$cfgfp" ] || N=slash M="Not found config file" log:ret:1

    local cache_dir="$___X_CMD_ROOT_CACHE/slash/compile"
    ___x_cmd ensurefp "$cache_dir/slash.sh" || return $?

    local name="" fp="" item_status=""

    # posix function syntax - shared by bash, ash, zsh
    {
        printf "# shellcheck shell=sh\n# Generated by x slash compile\n\n"
        printf "/() { ___x_cmd slash runname \"\$@\"; }\n"
        local advise_code=""
        while IFS= read -r line; do
            case "$line" in
                [!\ ]*:*)
                    [ -n "$name" ] && [ "$item_status" = "enable" ] && {
                        printf "/%s() { ___x_cmd slash run %s \"\$@\"; }\n" "$name" "$fp"
                        [ -z "$advise_code" ] && \
                            advise_code="\t[ ! -f \"$___X_CMD_ROOT_ADV/slash/data/run.jso\" ] || ___x_cmd advise init /$name \"$___X_CMD_ROOT_ADV/slash/data/run.jso\"" || \
                            advise_code="$advise_code\n\t[ ! -f \"$___X_CMD_ROOT_ADV/slash/data/run.jso\" ] || ___x_cmd advise init /$name \"$___X_CMD_ROOT_ADV/slash/data/run.jso\""
                    }
                    name="${line%:}"
                    fp=""; item_status=""
                    ;;
                "  fp:"*)
                    fp="${line#  fp: }"
                    ;;
                "  status:"*)
                    item_status="${line#  status: }"
                    ;;
            esac
        done < "$cfgfp"
        [ -n "$name" ] && [ "$item_status" = "enable" ] && {
            printf "/%s() { ___x_cmd slash run %s \"\$@\"; }\n" "$name" "$fp"
            [ -z "$advise_code" ] && \
                advise_code="\t[ ! -f \"$___X_CMD_ROOT_ADV/slash/data/run.jso\" ] || ___x_cmd advise init /$name \"$___X_CMD_ROOT_ADV/slash/data/run.jso\"" || \
                advise_code="$advise_code\n\t[ ! -f \"$___X_CMD_ROOT_ADV/slash/data/run.jso\" ] || ___x_cmd advise init /$name \"$___X_CMD_ROOT_ADV/slash/data/run.jso\""
        }

        # advise registration
        if [ -n "$advise_code" ]; then
            printf "\nif ___x_cmd_is_suitable_advise_repl 2>/dev/null 1>&2; then\n"
            printf "\txrc:mod:lib advise env\n"
            printf "%b\n" "$advise_code"
            printf "fi\n"
        fi
    } > "$cache_dir/slash.sh"

    # advise cache for zsh completion
    {
        ___x_cmd_slash___filelist | \
            OUTPUT_MODE=advise CFGFP="$cfgfp" \
            ___x_cmd cawk -f "$___X_CMD_ROOT_MOD/slash/lib/awk/scan.awk"
    } > "$cache_dir/advise"

    # dash alias
    {
        printf "# shellcheck shell=sh\n# Generated by x slash compile\n\n"
        printf "alias /=\"___x_cmd_slash_runname\"\n"
        while IFS= read -r line; do
            case "$line" in
                [!\ ]*:*)
                    [ -n "$name" ] && [ "$item_status" = "enable" ] && {
                        printf "alias /%s=\"___x_cmd slash run %s\"\n" "$name" "$fp"
                    }
                    name="${line%:}"
                    fp=""; item_status=""
                    ;;
                "  fp:"*)
                    fp="${line#  fp: }"
                    ;;
                "  status:"*)
                    item_status="${line#  status: }"
                    ;;
            esac
        done < "$cfgfp"
        [ -n "$name" ] && [ "$item_status" = "enable" ] && {
            printf "alias /%s=\"___x_cmd slash run %s\"\n" "$name" "$fp"
        }
    } > "$cache_dir/slash.dash"

    slash:info \
        --posix "$cache_dir/slash.sh"   \
        --dash "$cache_dir/slash.dash"  \
        "Compiled slash commands."

    # Auto-source for current shell
    ___x_cmd_is_repl || return 0
    case "$___X_CMD_SHELL" in
        bash|ash|zsh)
            slash:debug "Loading posix script for $___X_CMD_SHELL"
            ___x_cmd_source_invoke "$cache_dir/slash.sh" ;;
        dash)
            slash:debug "Loading dash alias script"
            ___x_cmd_source_invoke "$cache_dir/slash.dash" ;;
        *)  slash:info "Please switch to a new shell environment" ;;
    esac
}
