"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var ObjectId = require('bson').ObjectID;
var clone = require('clone');
var tv4 = require('tv4');
var FacilityBuilding = require('./facility-building');
var Facility = /*#__PURE__*/function () {
  function Facility(opt) {
    _classCallCheck(this, Facility);
    this._id = null;
    this.name = null;
    this.campusCode = null;
    this.tenantCode = null;
    this.buildings = [];
    this.cersId = null;
    this.epaId = null;
    this.hazardousLiquidLimit = {
      value: 55,
      unit: 'gal'
    };
    this.hazardousSolidLimit = {
      value: 500,
      unit: 'lb'
    };
    this.compressedGasLimit = {
      value: 200,
      unit: 'ft3'
    };
    this.liquefiedCarbonDioxideLimit = {
      value: 200,
      unit: 'lbs'
    };
    if (opt) {
      this.fillObject(clone(opt));
    }
  }
  _createClass(Facility, [{
    key: "fillObject",
    value: function fillObject(opt) {
      var _this = this;
      Object.keys(this).forEach(function (prop) {
        if (_this[prop]) {
          Object.assign(_this[prop], opt[prop]);
        } else {
          _this[prop] = opt[prop] || null;
        }
      });
      this._id = new ObjectId(opt._id);
      // eslint-disable-next-line no-underscore-dangle
      this._createdDate = this._id.getTimestamp();
      this.buildings = Array.isArray(opt.buildings) ? opt.buildings.map(function (building) {
        return new FacilityBuilding(building);
      }) : [];
    }
  }, {
    key: "validate",
    value: function validate() {
      return tv4.validateMultiple(this, Facility.schema());
    }
  }], [{
    key: "schema",
    value: function schema() {
      var facility = new Facility();
      var requiredFacility = Object.keys(facility);
      var valueUnitSchema = {
        type: 'object',
        properties: {
          value: {
            type: 'number'
          },
          unit: {
            type: 'string'
          }
        },
        required: ['value', 'unit']
      };
      return {
        $schema: 'http://json-schema.org/draft-04/schema#',
        id: 'facility.schema.json',
        type: 'object',
        additionalProperties: false,
        properties: {
          _id: {
            type: 'object'
          },
          _createdDate: {
            type: 'object'
          },
          name: {
            type: 'string'
          },
          campusCode: {
            type: 'string'
          },
          tenantCode: {
            type: 'string'
          },
          buildings: {
            type: 'array',
            item: {
              $ref: 'facility-building.schema.json'
            }
          },
          cersId: {
            type: ['string', 'null']
          },
          epaId: {
            type: ['string', 'null']
          },
          hazardousLiquidLimit: valueUnitSchema,
          hazardousSolidLimit: valueUnitSchema,
          compressedGasLimit: valueUnitSchema,
          liquefiedCarbonDioxideLimit: valueUnitSchema
        },
        required: requiredFacility
      };
    }
  }]);
  return Facility;
}();
tv4.addSchema(FacilityBuilding.schema());
module.exports = Facility;