"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 tv4 = require('tv4');
var clone = require('clone');
var LineItem = require('./line-item');
var PersonReference = require('../core/person-reference');
var PurchaseOrder = /*#__PURE__*/function () {
  function PurchaseOrder(opt) {
    _classCallCheck(this, PurchaseOrder);
    this._id = null;
    this.purchaseOrderNumber = null;
    this.createdDate = null;
    this.revisionDate = null;
    this.priority = null;
    this.items = [];
    this.supplier = {
      id: null,
      name: null,
      number: null
    };
    this.orderedBy = null;
    this.shippingInfo = {
      address: null,
      city: null,
      state: null,
      postalCode: null,
      country: null
    };
    this.billingInfo = {
      name: null,
      address: null,
      city: null,
      state: null,
      postalCode: null,
      country: null
    };
    this.organizationCodes = [];
    this.deptCodes = [];
    this.speedtype = null;
    this.chartstring = null;
    if (opt) {
      this.fillObject(clone(opt));
    }
  }
  _createClass(PurchaseOrder, [{
    key: "fillObject",
    value: function fillObject(opt) {
      this._id = new ObjectId(opt._id);
      this.purchaseOrderNumber = opt.purchaseOrderNumber || null;
      this.createdDate = opt.createdDate ? new Date(opt.createdDate) : null;
      this.revisionDate = opt.revisionDate ? new Date(opt.revisionDate) : null;
      this.priority = opt.priority || null;
      this.supplier = {
        id: opt.supplier && opt.supplier.id ? opt.supplier.id : null,
        name: opt.supplier && opt.supplier.name ? opt.supplier.name : null,
        number: opt.supplier && opt.supplier.number ? opt.supplier.number : null
      };
      this.orderedBy = opt.orderedBy ? new PersonReference(opt.orderedBy) : null;
      this.shippingInfo = {
        address: opt.shippingInfo && opt.shippingInfo.address ? opt.shippingInfo.address : [],
        city: opt.shippingInfo && opt.shippingInfo.city ? opt.shippingInfo.city : null,
        state: opt.shippingInfo && opt.shippingInfo.state ? opt.shippingInfo.state : null,
        postalCode: opt.shippingInfo && opt.shippingInfo.postalCode ? opt.shippingInfo.postalCode : null,
        country: opt.shippingInfo && opt.shippingInfo.country ? opt.shippingInfo.country : null
      };
      this.billingInfo = {
        name: opt.billingInfo && opt.billingInfo.name ? opt.billingInfo.name : null,
        address: opt.billingInfo && opt.billingInfo.address ? opt.billingInfo.address : [],
        city: opt.billingInfo && opt.billingInfo.city ? opt.billingInfo.city : null,
        state: opt.billingInfo && opt.billingInfo.state ? opt.billingInfo.state : null,
        postalCode: opt.billingInfo && opt.billingInfo.postalCode ? opt.billingInfo.postalCode : null,
        country: opt.billingInfo && opt.billingInfo.country ? opt.billingInfo.country : null
      };
      this.organizationCodes = opt.organizationCodes ? opt.organizationCodes : [];
      this.deptCodes = opt.deptCodes ? opt.deptCodes : [];
      this.speedtype = opt.speedtype || null;
      this.chartstring = opt.chartstring || null;
      if (opt.items) {
        this.items = opt.items.map(function (item) {
          return new LineItem(item);
        });
      }
    }
  }, {
    key: "validate",
    value: function validate() {
      return tv4.validateMultiple(this, PurchaseOrder.schema());
    }
  }], [{
    key: "schema",
    value: function schema() {
      return {
        $schema: 'http://json-schema.org/draft-04/schema#',
        id: 'purchase-order.schema.json',
        type: 'object',
        additionalProperties: false,
        properties: {
          _id: {
            type: 'object'
          },
          purchaseOrderNumber: {
            type: 'string'
          },
          createdDate: {
            type: 'object'
          },
          priority: {
            type: 'string'
          },
          items: {
            anyOf: [{
              $ref: 'line-item.schema.json'
            }, {
              type: ['null']
            }]
          },
          supplier: {
            type: 'object'
          },
          orderedBy: {
            $ref: 'person-reference.schema.json'
          },
          shippingInfo: {
            type: ['object', 'null']
          },
          billingInfo: {
            type: ['object', 'null']
          }
        },
        required: ['_id', 'createdDate', 'items']
      };
    }
  }]);
  return PurchaseOrder;
}();
tv4.addSchema(LineItem.schema());
tv4.addSchema(PersonReference.schema());
module.exports = PurchaseOrder;