"use strict";

/** @typedef {import("../../../../").Compilation} Compilation */
/** @typedef {import("../../../../").Module} Module */

/** @type {import("../../../../").Configuration} */
module.exports = {
	optimization: {
		usedExports: true,
		concatenateModules: true,
		sideEffects: false
	},
	plugins: [
		function apply() {
			this.hooks.compilation.tap(
				"Test",
				/**
				 * @param {Compilation} compilation the compilation
				 * @returns {void}
				 */
				(compilation) => {
					compilation.hooks.dependencyReferencedExports.tap(
						"Test",
						(referencedExports, dep) => {
							const module =
								/** @type {Module} */
								(compilation.moduleGraph.getParentModule(dep));
							if (!module.identifier().endsWith("module.js")) {
								return referencedExports;
							}
							const refModule = compilation.moduleGraph.getModule(dep);
							if (
								refModule &&
								refModule.identifier().endsWith("reference.js") &&
								referencedExports.some((ref) => {
									const names = Array.isArray(ref) ? ref : ref.name;
									return names.length === 1 && names[0] === "unused";
								})
							) {
								return referencedExports.filter((ref) => {
									const names = Array.isArray(ref) ? ref : ref.name;
									return names.length !== 1 || names[0] !== "unused";
								});
							}
							return referencedExports;
						}
					);
				}
			);
		}
	]
};
