"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.isDevelopment = isDevelopment;
exports.mapLabelKey = mapLabelKey;
exports.defaultValue = defaultValue;
exports.mapSchema = mapSchema;

var _selectn = require("selectn");

var _selectn2 = _interopRequireDefault(_selectn);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function isDevelopment() {
  return process.env.NODE_ENV !== "production";
}

function optionToString(fields, separator) {
  return function (option) {
    return fields.map(function (field) {
      return (0, _selectn2.default)(field, option);
    }).filter(function (fieldVal) {
      return fieldVal;
    }).reduce(function (agg, fieldVal, i) {
      if (i === 0) {
        return fieldVal;
      } else {
        return "" + agg + separator + fieldVal;
      }
    }, "");
  };
}

function mapLabelKey(labelKey) {
  if (Array.isArray(labelKey)) {
    return optionToString(labelKey, " ");
  } else if ((typeof labelKey === "undefined" ? "undefined" : _typeof(labelKey)) === "object" && labelKey.fields && labelKey.separator) {
    var fields = labelKey.fields,
        separator = labelKey.separator;

    return optionToString(fields, separator);
  } else {
    return labelKey;
  }
}

function defaultValue(_ref) {
  var properties = _ref.properties;

  var defVal = Object.keys(properties).reduce(function (agg, field) {
    if (properties[field].default !== undefined) {
      agg[field] = properties[field].default;
    }
    return agg;
  }, {});
  return defVal;
}

function mapSchema(events, schema, mapping) {
  if (!mapping) {
    return events;
  }

  var defVal = defaultValue(schema.properties ? schema : schema.items);
  var mappedEvents = events.map(function (event) {
    var mappedEvent = Object.keys(mapping).reduce(function (agg, field) {
      var schemaField = mapping[field];
      agg[schemaField] = (0, _selectn2.default)(field, event);
      return agg;
    }, Object.assign({}, defVal));
    return mappedEvent;
  });

  return mappedEvents;
}