import prettier from 'prettier';
import prettierPluginJava from 'prettier-plugin-java';
import prettierPluginProperties from 'prettier-plugin-properties';
import prettierPluginPackagejson from 'prettier-plugin-packagejson';
import { addLineNumbers } from '../internal/transform-utils.js';
export default async ({ relativeFilePath, filePath, fileContents, prettierOptions, prettierPackageJson, prettierJava, prettierProperties, }) => {
    try {
        const resolvedDestinationFileOptions = await prettier.resolveConfig(relativeFilePath);
        const fileOptions = {
            ...resolvedDestinationFileOptions,
            plugins: [],
            filepath: relativeFilePath,
            ...prettierOptions,
        };
        if (prettierPackageJson && filePath.endsWith('package.json')) {
            fileOptions.plugins.push(prettierPluginPackagejson);
        }
        if (prettierJava && filePath.endsWith('.java')) {
            fileOptions.plugins.push(prettierPluginJava);
        }
        if (prettierProperties) {
            fileOptions.plugins.push(prettierPluginProperties);
        }
        return { result: await prettier.format(fileContents, fileOptions) };
    }
    catch (error) {
        let errorMessage;
        if (fileContents) {
            errorMessage = `Error parsing file ${relativeFilePath}: ${error}

At: ${addLineNumbers(fileContents)}`;
        }
        else {
            errorMessage = `Unknown prettier error: ${error}`;
        }
        return { errorMessage };
    }
};
