'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var utils = require('@forensic-js/utils');

var Model =
/*#__PURE__*/
function () {
  function Model(handler) {
    _classCallCheck(this, Model);

    _defineProperty(this, "handler", void 0);

    _defineProperty(this, "fieldsToSkip", []);

    _defineProperty(this, "fieldsToRename", {});

    this.handler = handler;
  }
  /**
   * list of fields to skip when exporting data
   * @param fields comma separated list of fields to skip
   */


  _createClass(Model, [{
    key: "skipFields",
    value: function skipFields() {
      var _this = this;

      for (var _len = arguments.length, fields = new Array(_len), _key = 0; _key < _len; _key++) {
        fields[_key] = arguments[_key];
      }

      fields.forEach(function (field) {
        return _this.fieldsToSkip.push(field);
      });
      return this;
    }
    /**
     * rename a given given
     * @param oldName field old name
     * @param newName field new name
     */

  }, {
    key: "renameField",
    value: function renameField(oldName, newName) {
      this.fieldsToRename[oldName] = newName;
      return this;
    }
    /**
     * renames the given fields
     * @param fields object of field old name to new name value pairs
     */

  }, {
    key: "renameFields",
    value: function renameFields(fields) {
      var _this2 = this;

      Object.keys(fields).forEach(function (oldName) {
        return _this2.fieldsToRename[oldName] = fields[oldName];
      });
      return this;
    }
    /**
     * exports the data to the given target
     * @param target model to export data to
     * @param expandProperties boolean indicating if properties should be expanding
     */

  }, {
    key: "export",
    value: function _export() {
      var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var expandProperties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
      var handler = this.handler,
          fieldsToSkip = this.fieldsToSkip,
          fieldsToRename = this.fieldsToRename;
      Object.keys(handler.data).forEach(function (field) {
        if (!fieldsToSkip.includes(field)) {
          var newName = fieldsToRename[field] || field;
          var value = handler.data[field];

          if (expandProperties) {
            utils.expandProperty(target, newName, value, undefined, handler.getDBCaseStyle());
          } else {
            target[utils.applyCase(newName, handler.getDBCaseStyle())] = value;
          }
        }
      });
      return target;
    }
  }]);

  return Model;
}();

module.exports = Model;
//# sourceMappingURL=Model.js.map
