var through = require('through2');
var peliasConfig = require( 'pelias-config' ).generate();
var wofAdminLookup = require('pelias-wof-admin-lookup');

/**
 * Generate a stream object that will handle the adminLookup when enabled.
 * When disabled, generate a passthrough stream.
 *
 * @param {object} [config]
 * @param {object} [adminLookup]
 * @returns {Stream}
 */
function createStream(config, adminLookup) {
  config = config || peliasConfig;
  adminLookup = adminLookup || wofAdminLookup;

  if (config.imports.openstreetmap.adminLookup) {
    var pipResolver = adminLookup.createWofPipResolver(config.imports.adminLookup.url);
    return adminLookup.createLookupStream(pipResolver);
  }
  else {
    return through.obj(function (doc, enc, next) {
      next(null, doc);
    });
  }
}

module.exports = createStream;

@@ -144,16 +144,131 @@
 kup');%0A%0A
+%0Afunction sendPassthroughStream() %7B%0A  return through.obj(function (doc, enc, next) %7B%0A    next(null, doc);%0A  %7D);%0A%7D%0A%0A
 /**%0A * G
@@ -610,52 +610,359 @@
 %0A%0A  
-if (config.imports.openstreetmap.adminLookup
+// disable adminLookup with empty config%0A  if (!config.imports %7C%7C !config.imports.openstreetmap) %7B%0A    return sendPassthroughStream();%0A  %7D%0A%0A  // admin lookup disabled%0A  if (!config.imports.openstreetmap.adminLookup) %7B%0A    return sendPassthroughStream();%0A  %7D%0A%0A  // admin lookup enabled%0A  if (config.imports.adminLookup && config.imports.adminLookup.url
 ) %7B%0A
@@ -1113,102 +1113,362 @@
   %7D%0A
+%0A
   
-else %7B%0A    return through.obj(function (doc, enc, next) %7B%0A      next(null, doc);%0A    %7D);%0A  %7D
+// throw error if flag is set but no URL configured%0A  var message = 'Admin lookup is enabled but no url is specified in your config ' +%0A    'at imports.adminLookup.url. Make sure a URL pointing to a Who%5C's on First point ' +%0A    'in polygon server (https://github.com/whosonfirst/go-whosonfirst-pip) is set in your config';%0A  throw new Error(message);
 %0A%7D%0A%0A

Add helpful error message when adminLookup is misconfigured