const clone = require('clone');
const tv4 = require('tv4');

class TableItemSchema {
  constructor(opt) {
    this.key = null;
    this.label = null;
    this.type = null;

    if (opt) {
      this.fillObject(clone(opt));
    }
  }

  fillObject(details) {
    this.key = details.key || null;
    this.label = details.label || 'Text';
    this.type = details.type || 'TEXT';
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'table-item-schema.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        details: {
          key: { type: 'string' },
          label: { type: 'string' },
          type: { type: 'string', enum: ['TEXT', 'DATE', 'NUMBER'] },
        },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = TableItemSchema;
