const clone = require('clone');
const tv4 = require('tv4');
const { ObjectId } = require('bson');

// Adjust properties of this class when the details of
// library item and inventory item
// diverge from each other in actual use cases

class InventoryPPEDetails {
  constructor(opt) {
    if (opt) {
      this.altered = null;
      this.size = null;
      this.style = null;
      this.laundryLocation = null;

      this.fillObject(opt);
    }
  }

  fillObject(opt) {
    const { __typename, ...rest } = opt;
    Object.assign(this, clone(rest));
    this.laundryLocation = rest.laundryLocation ? { _id: new ObjectId(rest.laundryLocation._id) } : null;
  }

  validate() {
    return tv4.validateMultiple(this, InventoryPPEDetails.schema());
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'inventory-item.ppe.details.schema.json',
      title: 'Library Item PPE details Schema',
      type: 'object',
      additionalProperties: false,
      properties: {
        altered: { type: ['string', 'null'] },
        size: { type: ['string', 'null'] },
        style: { type: ['string', 'null'] },
        laundryLocation: { type: ['object', 'null'] },
      },
    };
  }
}

module.exports = InventoryPPEDetails;
