'use strict'

var FieldError = require('../errors').FieldError

module.exports = dimension




/**
 * Set dimension spec
 *
 * Depending on arguments length creates default or extraction dimension spec.
 *
 * If one or two arguments are specified DefaultDimensionSpec is created.
 *
 * If all arguments are specified ExtractionDimensionSpec is created.
 *
 * @see http://druid.io/docs/0.6.121/DimensionSpecs.html
 * @param {string} dimension Dimension name
 * @param {string} [outputName] Renamed name
 * @param {object} [fn] Extraction function object
 * @returns {object}
 */
function dimension(dimension, outputName, fn) {
  if (!dimension) {
    throw new FieldError('At least dimension must be specified')
  }

  if (arguments.length === 1) {
    return {
      type:      'default',
      dimension: dimension
    }
  }
  else if (arguments.length === 2) {
    return {
      type:       'default',
      dimension:  dimension,
      outputName: outputName
    }
  }
  else if (arguments.length === 3) {
    return {
      type:            'extraction',
      dimension:       dimension,
      outputName:      outputName,
      dimExtractionFn: fn
    }
  }
}

@@ -209,18 +209,23 @@
  If 
+sec
 on
-e
+d
  or t
-wo
+hird
  arg
@@ -233,31 +233,29 @@
 ment
-s are specified Default
+ is object Extraction
 Dime
@@ -287,48 +287,29 @@
  * I
-f all arguments are specified Extraction
+n other cases Default
 Dime
@@ -836,16 +836,50 @@
 th === 2
+ && typeof outputName !== 'object'
 ) %7B%0A    
@@ -976,32 +976,210 @@
 tName%0A    %7D%0A  %7D%0A
+  else if (arguments.length === 2) %7B%0A    fn = outputName%0A%0A    return %7B%0A      type:            'extraction',%0A      dimension:       dimension,%0A      dimExtractionFn: fn%0A    %7D%0A  %7D%0A
   else if (argum
@@ -1354,10 +1354,93 @@
   %7D%0A  %7D%0A
+  else %7B%0A    throw new FieldError('Bad arguments number: ' + arguments.length)%0A  %7D%0A
 %7D%0A

handle arguments more smart