'use strict';

var ValidationError = require('../error/validation_error');
var getType = require('../util/get_type');
var validate = require('./validate');
var validateTypes = {
    enum: require('./validate_enum'),
    color: require('./validate_color'),
    array: require('./validate_array'),
    number: require('./validate_number'),
    function: require('./validate_function')
};

module.exports = function validateLight(options) {
    var light = options.value;
    var styleSpec = options.styleSpec;
    var lightSpec = styleSpec.light;
    var style = options.style;

    var errors = [];

    var rootType = getType(light);
    if (rootType !== 'object') {
        errors = errors.concat([new ValidationError('light', light, 'object expected, %s found', rootType)]);
        return errors;
    }

    var type;
    for (var key in light) {
        var transitionMatch = key.match(/^(.*)-transition$/);

        if (transitionMatch && lightSpec[transitionMatch[1]] && lightSpec[transitionMatch[1]].transition) {
            var baseKey = transitionMatch[1];

            type = lightSpec[baseKey].type;
            errors = errors.concat(validate({
                key: key,
                value: light[key],
                valueSpec: styleSpec.transition,
                style: style,
                styleSpec: styleSpec
            }));
        } else if (lightSpec[key]) {
            type = lightSpec[key].type;
            errors = errors.concat(validateTypes[type]({
                key: key,
                value: light[key],
                valueSpec: lightSpec[key],
                style: style,
                styleSpec: styleSpec
            }));
        } else {
            errors = errors.concat([new ValidationError(key, light[key], 'unknown property "%s"', key)]);
        }
    }

    return errors;
};

@@ -152,238 +152,8 @@
 e');
-%0Avar validateTypes = %7B%0A    enum: require('./validate_enum'),%0A    color: require('./validate_color'),%0A    array: require('./validate_array'),%0A    number: require('./validate_number'),%0A    function: require('./validate_function')%0A%7D;
 %0A%0Amo
@@ -404,16 +404,77 @@
 %0A    if 
+(light === undefined) %7B%0A        return errors;%0A    %7D else if 
 (rootTyp
@@ -635,22 +635,8 @@
  %7D%0A%0A
-    var type;%0A
     
@@ -835,99 +835,8 @@
 ) %7B%0A
-            var baseKey = transitionMatch%5B1%5D;%0A%0A            type = lightSpec%5BbaseKey%5D.type;%0A
     
@@ -1112,48 +1112,8 @@
 ) %7B%0A
-            type = lightSpec%5Bkey%5D.type;%0A
     
@@ -1155,19 +1155,8 @@
 date
-Types%5Btype%5D
 (%7B%0A 

Fix validation errors thrown when using a function for light intensity (#598)