# shellcheck shell=dash

___x_cmd_cd_relm___get_(){
    x_=""
    local name="${1#:}"
    case "$name" in
        ws)     ___x_cmd wsroot_ ;;
        git)    ___x_cmd locatefolder_ "$PWD" ".git"            || return $?
                x_="$x_/.git"
                ;;
        nm)     ___x_cmd locatefolder_ "$PWD" ".node_modules"   || return $?
                x_="$x_/.node_modules"
                ;;
        lm)     ___x_cmd locatefolder_ "$PWD" ".lua_modules"    || return $?
                x_="$x_/.lua_modules"
                ;;
        *)
                ___x_cmd_cd_util___dict_get_ relm_custom "$name" || \
                    ___x_cmd_cd_util___dict_get_inner_ relm_init "$name"
                ;;
    esac
}

___x_cmd_cd_relm___get(){
    local name="${1#:}"
    [ -n "$name" ] || N=cd M="Please provide relm name" log:ret:64
    local x_=; ___x_cmd_cd_relm___get_ "$name" || {
        cd:warn "RELM NOT found ==> $name"
        return 1
    }
    printf "%s\n" "$x_"
}

___x_cmd_cd_relm___set(){
    local name="${1#:}"
    case "$name" in
        "")     N=cd M="Please provide relm name" log:ret:64 ;;
        ws|git|nm|lm)
                cd:warn "The relm [name=$name] is predefined and already occupied"; return 1 ;;
        *)
                if ___x_cmd_cd_util___dict_inner has relm_init "$name"; then
                    cd:warn "The relm [name=$name] is predefined and already occupied"
                    return 1
                fi
                ;;
    esac
    [ -n "$2" ] || N=cd M="Please provide the relm [name=$name] directory path" log:ret:64

    local x_=; ___x_cmd_cd_relm___abs_dir_path "$2" || {
        cd:error "Failed to set relm, no such [directory=$2]"
        return 1
    }
    cd:info "Setup relm → [$name = $x_]"
    ___x_cmd_cd_util___dict_set relm_custom "$name" "$x_"
}

___x_cmd_cd_relm___rm(){
    local name="${1#:}"
    case "$name" in
        "")     N=cd M="Please provide relm name" log:ret:64 ;;
        ws|git|nm|lm)
                cd:warn "The relm [name=$name] is predefined and cannot be removed"; return 1 ;;
        *)
                if ___x_cmd_cd_util___dict_inner has relm_init "$name"; then
                    cd:warn "The relm [name=$name] removed only in current environment"
                    ___x_cmd_cd_util___dict_rm relm_init "$name"
                    return
                fi
                ;;
    esac
    ___x_cmd_cd_util___dict_rm relm_custom "$name" || {
        cd:warn "Remove the relm [name=$name] failed"
        return 1
    }
    cd:info "Removed the relm [name=$name] successfully"
}

