#!/usr/bin/env node

/**
 * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
 */

import { checkVersionMatch } from '@ckeditor/ckeditor5-dev-dependency-checker';
import {
	CKEDITOR5_ROOT_PATH,
	CKEDITOR5_COMMERCIAL_PATH,
	CKEDITOR5_PACKAGES_PATH,
	CKEDITOR5_COMMERCIAL_PACKAGES_PATH
} from '../constants.mjs';
import isCKEditor5PackageFactory from '../release/utils/isckeditor5packagefactory.mjs';
import getCKEditor5PackageNames from '../release/utils/getckeditor5packagenames.mjs';

const shouldFix = process.argv[ 2 ] === '--fix';

/**
 * All dependencies should be pinned to the exact version. However, there are some exceptions,
 * where we want to use the caret or tilde operator. This object contains such exceptions.
 */
const versionExceptions = {
	/**
	 * CodeMirror packages are modular and depend on each other. We must use the same versions
	 * as they have in their dependencies to avoid issues with versions mismatch.
	 *
	 * See: https://github.com/ckeditor/ckeditor5-commercial/issues/6939.
	 */
	'@codemirror/autocomplete': '^',
	'@codemirror/lang-html': '^',
	'@codemirror/language': '^',
	'@codemirror/state': '^',
	'@codemirror/view': '^',
	'@codemirror/theme-one-dark': '^'
};

const pkgJsonPatterns = [
	'package.json',
	CKEDITOR5_PACKAGES_PATH + '/*/package.json',
	CKEDITOR5_COMMERCIAL_PACKAGES_PATH + '/*/package.json',
	CKEDITOR5_COMMERCIAL_PATH + '/package.json'
];

const options = {
	cwd: CKEDITOR5_ROOT_PATH,
	fix: shouldFix,
	devDependenciesFilter: await isCKEditor5PackageFactory(),
	pkgJsonPatterns,
	versionExceptions,
	workspacePackages: await getCKEditor5PackageNames()
};

checkVersionMatch( options )
	.catch( err => {
		console.error( err );

		process.exit( 1 );
	} );
