"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 _require = require('bson'),
  ObjectId = _require.ObjectId;
var _ = require('lodash');
var tv4 = require('tv4');
var NODE_TYPE = require('../../constant/relationship/node-type.constant');
var Node = /*#__PURE__*/function () {
  function Node(opt) {
    _classCallCheck(this, Node);
    this._id = null;
    this.label = null;
    this.type = null;
    if (opt) {
      this.fillObject(opt);
    }
  }
  _createClass(Node, [{
    key: "fillObject",
    value: function fillObject(opt) {
      this._id = opt._id || new ObjectId().toString();
      this.label = opt.label;
      this.type = opt.type;
    }
  }, {
    key: "validate",
    value: function validate() {
      var validation = tv4.validateMultiple(this, Node.schema());
      var isLabelValid = this.validateLabel(this.type, this.label);
      if (!isLabelValid) {
        validation.errors.push(new Error("Invalid label for type ".concat(this.type)));
        validation.valid = false;
      }
      return validation;
    }

    // eslint-disable-next-line class-methods-use-this
  }, {
    key: "validateLabel",
    value: function validateLabel(type, label) {
      var sortedLabel = Object.keys(label).sort();
      switch (type) {
        case NODE_TYPE.PERSON:
          return _.isEqual(sortedLabel, ['email', 'firstName', 'lastName']);
        case NODE_TYPE.ROOM:
          return _.isEqual(sortedLabel, ['buildingName', 'roomNumber']);
        case NODE_TYPE.ASSESSMENT:
          return _.isEqual(sortedLabel, ['name', 'seriesId', 'status', 'statusDisplay', 'templateId', 'templateType']);
        case NODE_TYPE.GROUP:
          return _.isEqual(sortedLabel, ['name']);
        case NODE_TYPE.DEPARTMENT:
          return _.isEqual(sortedLabel, ['code', 'costCenter', 'description', 'name']);
        case NODE_TYPE.COLLEGE:
        case NODE_TYPE.DIVISION:
        case NODE_TYPE.PROGRAM:
        case NODE_TYPE.TENANT:
        case NODE_TYPE.UNIT:
          return _.isEqual(sortedLabel, ['code', 'name']);
        case NODE_TYPE.INVENTORY:
        default:
          return _.isEqual(sortedLabel, ['name']);
      }
    }
  }], [{
    key: "schema",
    value: function schema() {
      return {
        $schema: 'http://json-schema.org/draft-04/schema#',
        id: 'node.schema.json',
        type: 'object',
        additionalProperties: false,
        properties: {
          _id: {
            type: 'string'
          },
          label: {
            type: 'object'
          },
          type: {
            "enum": Object.values(NODE_TYPE)
          }
        },
        required: Object.keys(new Node())
      };
    }
  }]);
  return Node;
}();
module.exports = Node;