"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 tv4 = require('tv4');
var Email = require('./email');
var Recipient = require('./recipient');
var Todo = require('./todo');
var REMINDER_STATUS = require('../../constant/reminder/reminder-status.constant');
var REMINDER_TYPE = require('../../constant/reminder/reminder-type.constant');
var Helper = require('../../helper/helper');
var Reminder = /*#__PURE__*/function () {
  function Reminder(opt) {
    _classCallCheck(this, Reminder);
    this._id = null;
    this.label = null;
    this.campusCode = null;
    this.tenantCode = null;
    this.applicationName = null;
    this.status = null;
    this.type = null;
    this.createdDate = null;
    this.completedDate = null;
    this.details = {};
    if (opt) {
      this.fillObject(opt);
    }
  }
  _createClass(Reminder, [{
    key: "fillObject",
    value: function fillObject(opt) {
      this._id = new ObjectId(opt._id);
      this.label = opt.label;
      this.campusCode = opt.campusCode;
      this.tenantCode = opt.tenantCode;
      this.applicationName = opt.applicationName;
      this.status = opt.status || REMINDER_STATUS.PENDING;
      this.type = opt.type;
      this.createdDate = opt.createdDate;
      this.completedDate = opt.completedDate || null;
      if (this.type === REMINDER_TYPE.EMAIL) {
        this.validateObj(new Email(opt));
      } else if (this.type === REMINDER_TYPE.TODO) {
        this.validateObj(new Todo(opt));
      }
    }
  }, {
    key: "validateObj",
    value: function validateObj(obj) {
      var validation = obj.validate();
      if (!validation.valid) {
        throw Helper.validateError(validation).msg;
      }
      this.details = obj;
    }
  }, {
    key: "validate",
    value: function validate() {
      var validation = tv4.validateMultiple(this, Reminder.schema());
      var labelIsValid = Helper.validateLabel(this.label);
      if (!labelIsValid) {
        validation.errors.push(new Error('Invalid type (expecting string, boolean or number) for values on label'));
        validation.valid = false;
      }
      return validation;
    }
  }], [{
    key: "schema",
    value: function schema() {
      return {
        $schema: 'http://json-schema.org/draft-04/schema#',
        id: 'reminder.schema.json',
        type: 'object',
        additionalProperties: false,
        properties: {
          _id: {
            type: ['object', 'null']
          },
          label: {
            type: 'object'
          },
          tenantCode: {
            type: 'string'
          },
          campusCode: {
            type: 'string'
          },
          applicationName: {
            type: 'string'
          },
          status: {
            "enum": Object.keys(REMINDER_STATUS)
          },
          type: {
            "enum": Object.keys(REMINDER_TYPE)
          },
          details: {
            type: ['object']
          },
          createdDate: {
            type: 'object'
          },
          completedDate: {
            type: ['object', 'null']
          }
        },
        required: ['_id', 'label', 'tenantCode', 'campusCode', 'status', 'type', 'applicationName', 'createdDate', 'completedDate']
      };
    }
  }]);
  return Reminder;
}();
tv4.addSchema(Email.schema());
tv4.addSchema(Todo.schema());
tv4.addSchema(Recipient.schema());
module.exports = Reminder;