import type core from 'jscodeshift';
import type { Collection } from 'jscodeshift/src/Collection';

/**
 * Generate a codemod to move component from Editor to
 * EditorMigrationComponent
 * Ref: ED-16826
 */
export const createUpdateEditorToMigrationComponent = (pkg: string, component: string) => {
	return (j: core.JSCodeshift, source: Collection<unknown>): void => {
		source
			.find(j.ImportDeclaration, { source: { value: pkg } })
			.filter(
				(path) =>
					j(path).find(j.ImportSpecifier, {
						imported: { type: 'Identifier', name: component },
					}).length > 0,
			)
			.find(j.ImportSpecifier)
			.filter((path) => path.node.imported.name === component)
			.replaceWith((currentImport) =>
				j.importSpecifier(j.identifier('EditorMigrationComponent'), currentImport.node.local),
			);
	};
};

export const renameEditorToMigrationComponent: (
	j: core.JSCodeshift,
	source: Collection<unknown>,
) => void = createUpdateEditorToMigrationComponent('@atlaskit/editor-core', 'Editor');
