module.exports = function plugin(babel) {
  const t = babel.types;

  return {
    visitor: {
      CallExpression(path) {
        if (t.isMemberExpression(path.node.callee) && t.isIdentifier(path.node.callee.property, { name: 'formatMessage' }) && path.node.arguments.length === 1 && t.isObjectExpression(path.node.arguments[0])) {
          const idProp = path.node.arguments[0].properties.find(prop => t.isIdentifier(prop.key, { name: 'id' }));
          if (idProp) {
            const memberExp = t.memberExpression(t.memberExpression(path.node.callee.object, t.identifier('messages')), idProp.value, true);
            path.replaceWith(memberExp);
          }
        }
      },
    },
  };
};