/* eslint-disable @typescript-eslint/no-var-requires, import/no-dynamic-require, simple-import-sort/imports, simple-import-sort/exports */
const fs = require('fs');
const path = require('path');

// 1. Define paths
const packageJsonPath = path.resolve(__dirname, '../package.json');
const registryPath = path.resolve(
	__dirname,
	'../src/auto-import-registry.d.ts',
);

// 2. Read package.json
const packageJson = require(packageJsonPath);

// 3. Combine dependencies and devDependencies
const allDeps = {
	...packageJson.dependencies,
	...packageJson.devDependencies,
};

// 4. Filter for @signozhq packages
const signozPackages = Object.keys(allDeps).filter((dep) =>
	dep.startsWith('@signozhq/'),
);

// 5. Generate file content
const fileContent = `// -------------------------------------------------------------------------
// AUTO-GENERATED FILE
// -------------------------------------------------------------------------
// This file is generated by scripts/update-registry.cjs automatically
// whenever you run 'yarn install' or 'npm install'.
//
// It forces VS Code to index these specific packages to fix auto-import
// performance issues in TypeScript 4.x.
//
// PR for reference: https://github.com/SigNoz/signoz/pull/9694
// -------------------------------------------------------------------------

${signozPackages.map((pkg) => `import '${pkg}';`).join('\n')}
`;

// 6. Write the file
try {
	fs.writeFileSync(registryPath, fileContent);
	console.log(
		`✅ Auto-import registry updated with ${signozPackages.length} @signozhq packages.`,
	);
} catch (err) {
	console.error('❌ Failed to update auto-import registry:', err);
}
