const clone = require('clone');
const tv4 = require('tv4');
const QuestionSchema = require('../../flexible-questionnaire/question.schema');

class PPEDetails {
  constructor(opt) {
    this.questions = [];
    this.defaults = {};
    if (opt) {
      this.fillObject(clone(opt));
    }
  }

  fillObject(opt) {
    const { defaults = {}, questions = [] } = opt;
    this.defaults = { ...defaults };
    this.questions = [...questions];
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'library-item.ppe.details.schema.json',
      title: 'Library Item PPE details Schema',
      type: 'object',
      additionalProperties: false,
      properties: {
        questions: { type: 'array', items: { $ref: 'fqQuestion.schema.json' } },
        defaults: { type: 'object' },
      },
    };
  }
}

tv4.addSchema(QuestionSchema);
module.exports = PPEDetails;
