const fs = require('node:fs');
const path = require('node:path');

/**
 * @description 不存在或不在该列表内的包不做同步
 */
const INCLUDE_PACKAGES = new Set([
  '@udecode/cn',
  '@udecode/plate-alignment',
  '@udecode/plate-autoformat',
  '@udecode/plate-basic-marks',
  '@udecode/plate-block-quote',
  '@udecode/plate-break',
  '@udecode/plate-caption',
  '@udecode/plate-code-block',
  '@udecode/plate-combobox',
  '@udecode/plate-comments',
  '@udecode/plate-common',
  '@udecode/plate-cursor',
  '@udecode/plate-dnd',
  '@udecode/plate-emoji',
  '@udecode/plate-excalidraw',
  '@udecode/plate-find-replace',
  '@udecode/plate-floating',
  '@udecode/plate-font',
  '@udecode/plate-heading',
  '@udecode/plate-highlight',
  '@udecode/plate-horizontal-rule',
  '@udecode/plate-indent',
  '@udecode/plate-indent-list',
  '@udecode/plate-juice',
  '@udecode/plate-kbd',
  '@udecode/plate-line-height',
  '@udecode/plate-link',
  '@udecode/plate-list',
  '@udecode/plate-media',
  '@udecode/plate-mention',
  '@udecode/plate-node-id',
  '@udecode/plate-normalizers',
  '@udecode/plate-paragraph',
  '@udecode/plate-reset-node',
  '@udecode/plate-resizable',
  '@udecode/plate-select',
  '@udecode/plate-selection',
  '@udecode/plate-serializer-csv',
  '@udecode/plate-serializer-docx',
  '@udecode/plate-serializer-html',
  '@udecode/plate-serializer-md',
  '@udecode/plate-tabbable',
  '@udecode/plate-table',
  '@udecode/plate-toggle',
  '@udecode/plate-trailing-block',
]);

// 定义要搜索的根目录
const rootDir = path.join(__dirname, '../../../', './packages');
const versionMap = new Map();
// 递归函数，只读取深度为1的目录下的package.json文件
function readPackageJsonFiles(directory, depth) {
  if (depth === 0) {
    const files = fs.readdirSync(directory);

    for (const file of files) {
      const filePath = path.join(directory, file);

      if (file === 'package.json') {
        // 如果是package.json文件，则读取name和version字段
        try {
          const content = fs.readFileSync(filePath, 'utf8');
          const packageJson = JSON.parse(content);
          // console.log(`"${packageJson.name}":"${packageJson.version}",`);
          versionMap.set(packageJson.name, packageJson.version);
        } catch (error) {
          console.error(`Error reading ${filePath}: ${error.message}`);
        }
      }
    }
  } else {
    const subDirs = fs.readdirSync(directory);

    for (const subDir of subDirs) {
      const subDirPath = path.join(directory, subDir);
      const stats = fs.statSync(subDirPath);

      if (stats.isDirectory()) {
        // 如果是目录，则递归调用该函数，深度减1
        readPackageJsonFiles(subDirPath, depth - 1);
      }
    }
  }
}

// 启动递归函数
readPackageJsonFiles(rootDir, 1);

const TARGET_PATH = path.join(__dirname, '../', './package.json');
const files = fs.readFileSync(TARGET_PATH);
const json = JSON.parse(files);

function updatedVersion() {
  versionMap.forEach((version, name) => {
    if (json.dependencies[name] && INCLUDE_PACKAGES.has(name)) {
      json.dependencies[name] = version;
    }
  });

  fs.writeFileSync(TARGET_PATH, JSON.stringify(json, null, 2));
}

updatedVersion();
