import { dirname, join, relative } from 'path';
import { passthrough } from '@yeoman/transform';
import { Minimatch } from 'minimatch';
const DEFAULT_DOC = 'This package file was generated by Begcode';
const packageInfoTransform = ({ javaRoots, editor, javadocs }) => {
    return passthrough(function (file) {
        for (const root of javaRoots) {
            if (file.path.startsWith(root)) {
                const directory = dirname(file.path);
                const packageName = relative(root, directory).replaceAll(/[/\\]/g, '.');
                const packageInfoFile = join(directory, 'package-info.java');
                const packageJavadoc = javadocs?.[packageName] ?? DEFAULT_DOC;
                if (!editor.exists(packageInfoFile)) {
                    editor.write(packageInfoFile, `/**
 * ${packageJavadoc}
 */
package ${packageName};
`);
                }
            }
        }
    });
};
export const matchMainJavaFiles = (srcMainJava) => new Minimatch(`**/${srcMainJava}/**/*.java`);
export default packageInfoTransform;
