package com.huangzj.protoplugin;

import com.intellij.openapi.vfs.VirtualFile;

/** 根据输出目录路径推断 Java package（优先 {@code src/main/java} 之后的路径）。 */
public final class PackageInference {

    private PackageInference() {}

    public static String inferPackage(VirtualFile directory) {
        if (directory == null) {
            return "com.example.cmd.cmds";
        }
        String path = directory.getPath().replace('\\', '/');
        String marker = "/src/main/java/";
        int idx = path.indexOf(marker);
        if (idx >= 0) {
            String sub = path.substring(idx + marker.length());
            if (sub.isEmpty()) {
                return "";
            }
            return sub.replace('/', '.');
        }
        int j = path.lastIndexOf("/java/");
        if (j >= 0) {
            String sub = path.substring(j + "/java/".length());
            return sub.replace('/', '.');
        }
        return "com.example.cmd.cmds";
    }
}
