import { extname } from 'path';
import { passthrough } from '@yeoman/transform';
import { isFileStateModified } from 'mem-fs-editor/state';
import { removeUnusedImports } from 'java-lint';
export const createRemoveUnusedImportsTransform = function (options = {}) {
    const { ignoreErrors } = options;
    return passthrough((file) => {
        if (extname(file.path) === '.java' && isFileStateModified(file)) {
            if (file.contents) {
                try {
                    file.contents = Buffer.from(removeUnusedImports(file.contents.toString('utf8')));
                }
                catch (error) {
                    const errorMessage = `Error parsing file ${file.relative}: ${error} at ${file.contents?.toString()}`;
                    if (ignoreErrors) {
                        this?.log?.warn?.(errorMessage);
                        return;
                    }
                    throw new Error(errorMessage);
                }
            }
        }
    });
};
