/**
 * This jscodeshift transform takes care of some of the rote things you'll
 * need to do while migrating from v2 to v3. See README.md for usage
 * instructions and further explanation.
 *
 * Original author: @dminkovsky (PR #6486)
 * Contributors: @jcreighton @benjamn
 * Reviewers: @hwillson @bnjmnt4n
 */
export default function transformer(file, api) {
  const j = api.jscodeshift;

  const source = j(file.source);

  renameOrCreateApolloClientImport();

  moveSpecifiersToApolloClient('@apollo/react-hooks');
  moveSpecifiersToApolloClient('apollo-cache-inmemory');
  moveSpecifiersToApolloClient('apollo-link');
  moveSpecifiersToApolloClient('apollo-link-http');
  moveSpecifiersToApolloClient('apollo-link-http-common');

  renameDefaultSpecifier(getImport('graphql-tag'), 'gql');
  moveSpecifiersToApolloClient('graphql-tag');

  renameImport('@apollo/react-components', '@apollo/client/react/components');
  renameImport('@apollo/react-hoc', '@apollo/client/react/hoc');
  renameImport('@apollo/react-ssr', '@apollo/client/react/ssr');
  renameImport('@apollo/react-testing', '@apollo/client/testing');

  renameDefaultSpecifier(getImport('apollo-link-schema'), 'SchemaLink');
  [
    'batch',
    'batch-http',
    'context',
    'error',
    'retry',
    'schema',
    'ws',
  ].forEach(link => renameImport(`apollo-link-${link}`, `@apollo/client/link/${link}`));

  return source.toSource();

  function renameOrCreateApolloClientImport() {
    const v3Import = getImport('@apollo/client');
    if (v3Import.size()) {
      return;
    }

    const v2Import = getImport('apollo-client');
    if (v2Import.size()) {
      renameDefaultSpecifier(v2Import, 'ApolloClient');
      renameImport('apollo-client', '@apollo/client');
    } else {
      source.find(j.ImportDeclaration).at(0).insertBefore(() => j.importDeclaration([], j.literal('@apollo/client')));
    }
  }

  function moveSpecifiersToApolloClient(
    moduleName,
  ) {
    const moduleImport = getImport(moduleName);

    if (moduleImport.size()) {
      const clientImports = getImport('@apollo/client');
      const specifiersToAdd = moduleImport.get('specifiers').value;
      clientImports.get('specifiers').push(...specifiersToAdd);
    }

    moduleImport.remove();
  }

  function renameImport(oldModuleName, newModuleName) {
    getImport(oldModuleName)
      .find(j.Literal)
      .replaceWith(path => ({
          ...path.value,
          value: newModuleName,
      }));
  }

  function getImport(moduleName) {
    return source
      .find(j.ImportDeclaration, {
        source: { value: moduleName }
      });
  }

  function renameDefaultSpecifier(moduleImport, name) {
    function replacer(path) {
      return path.value.local.name === name
        ? j.importSpecifier(j.identifier(name))
        : j.importSpecifier(j.identifier(name), path.value.local);
    }

    // Handle ordinary (no-{}s) default imports.
    moduleImport
      .find(j.ImportDefaultSpecifier)
      .replaceWith(replacer);

    // Handle { default as Foo } default imports.
    moduleImport.find(j.ImportSpecifier, {
      imported: {
        name: "default",
      },
    }).replaceWith(replacer);
  }
}

@@ -1370,24 +1370,183 @@
 %7Blink%7D%60));%0A%0A
+  const apolloClientImport = getImport('@apollo/client');%0A  if (!apolloClientImport.get('specifiers', 'length').value) %7B%0A    apolloClientImport.remove();%0A  %7D%0A%0A
   return sou

Remove @apollo/client import if no relevant imports are found