function transform(fileInfo, api) {
  const j = api.jscodeshift;
  const root = j(fileInfo.source);

  if (!fileInfo.path.includes('tailwind.config.js')) {
    return fileInfo.source;
  }

  console.log(`Processing file: ${fileInfo.path}`);

  const hasAkinonNextImport = root.find(j.VariableDeclarator, {
    id: { name: 'getAkinonNextContent' }
  });

  if (hasAkinonNextImport.size() === 0) {
    const requireStatement = j.variableDeclaration('const', [
      j.variableDeclarator(
        j.identifier('getAkinonNextContent'),
        j.callExpression(j.identifier('require'), [
          j.literal('@akinon/next/tailwind/content')
        ])
      )
    ]);

    root.get().node.program.body.unshift(requireStatement);

    console.log(`Added getAkinonNextContent import to ${fileInfo.path}`);
  }

  const defaultConfig = root
    .find(j.VariableDeclarator, {
      id: { name: 'defaultConfig' },
      init: { type: 'ObjectExpression' }
    })
    .paths()[0];

  if (!defaultConfig) {
    console.log(`No defaultConfig found in ${fileInfo.path}`);
    return fileInfo.source;
  }

  const contentArray = defaultConfig.value.init.properties.find(
    (prop) =>
      prop.key.name === 'content' && prop.value.type === 'ArrayExpression'
  );

  if (!contentArray) {
    console.log(`No content array found in defaultConfig`);
    return fileInfo.source;
  }

  const pluginsVarName = root
    .find(j.VariableDeclarator, {
      id: { name: 'plugins' },
      init: {
        type: 'CallExpression',
        callee: { name: 'require' }
      }
    })
    .paths()[0]?.value.id.name;

  if (!pluginsVarName) {
    console.log(`No plugins variable found in ${fileInfo.path}`);
    return fileInfo.source;
  }

  const contentElements = contentArray.value.elements;

  const filteredElements = contentElements.filter((element) => {
    if (element.type === 'SpreadElement') {
      if (
        element.argument.type === 'CallExpression' &&
        element.argument.callee.object?.callee.object.name === 'plugins'
      ) {
        return false;
      }

      if (
        element.argument.type === 'CallExpression' &&
        element.argument.callee.name === 'getAkinonNextContent'
      ) {
        return false;
      }
    }
    return true;
  });

  filteredElements.push(
    j.spreadElement(
      j.callExpression(j.identifier('getAkinonNextContent'), [
        j.identifier(pluginsVarName)
      ])
    )
  );

  contentArray.value.elements = filteredElements;

  console.log(`Successfully updated ${fileInfo.path}`);
  return root.toSource();
}

module.exports = transform;
