const fs = require("fs");
const path = require("path");

function processFile(filePath) {
	try {
		let content = fs.readFileSync(filePath, "utf8");
		const original = content;

		// 替换所有 number[] 为 string[]
		content = content.replace(/number\[\]/g, "string[]");

		// 可选：替换其他相关类型
		content = content.replace(/Array<number>/g, "Array<string>");
		content = content.replace(
			/ReadonlyArray<number>/g,
			"ReadonlyArray<string>",
		);

		if (content !== original) {
			fs.writeFileSync(filePath, content, "utf8");
			console.log(`✓ Updated: ${path.relative(process.cwd(), filePath)}`);
			return true;
		}
		return false;
	} catch (error) {
		console.error(`✗ Error processing ${filePath}:`, error.message);
		return false;
	}
}

function processDirectory(dirPath) {
	let updatedCount = 0;

	function walk(dir) {
		const items = fs.readdirSync(dir);

		for (const item of items) {
			const fullPath = path.join(dir, item);
			const stat = fs.statSync(fullPath);

			if (stat.isDirectory()) {
				walk(fullPath);
			} else if (item.endsWith(".ts") || item.endsWith(".tsx")) {
				if (processFile(fullPath)) {
					updatedCount++;
				}
			}
		}
	}

	walk(dirPath);
	return updatedCount;
}

// 主程序
const targetDir = "src/services";
if (fs.existsSync(targetDir)) {
	console.log(`Processing directory: ${targetDir}`);
	const updated = processDirectory(targetDir);
	console.log(`\nDone! Updated ${updated} files.`);
} else {
	console.error(`Directory not found: ${targetDir}`);
	process.exit(1);
}
