<commit_before>import Ember from 'ember';
import ZSchema from "npm:z-schema";

export default Ember.Object.extend({
  _schemas: {},
  _validators: {},

  load: function(url, name){
    return Ember.$.getJSON(url, (schema) => {
      this.set('_schemas.' + name, schema);
    });
  },

  createObject: function(name){
    var schema = this.get('_schemas.' + name);
    var obj = create(schema)
    return obj;
  }

});

function create(schema){
  if (schema.type === "object"){
    return JSObject.create({_schema: schema})
  } else if (schema.type === "array"){
    return JSArrayProxy.create({_schema: schema, content: []})
  }
}

// var JSArray = Ember.Object.extend(Ember.MutableArray, {
//   replace: function(){
//     debugger
//     console.log('called replace')
//   },

//   objectAt: function(){
//     console.log('called objectAt')
//   }
// })

var JSArrayProxy = Ember.ArrayProxy.extend({})

JSArrayProxy.reopen({
  pushObjects:function(objects){
    var schema = this.get('_schema.items');
    this._super(objects.map((obj)=>{
      return create(schema)
    }))
  },

  pushObject:function(object){
    var schema = this.get('_schema.items');
    return create(schema)
  },

  isValid: Ember.computed.empty('errors'),

  errors: function(){
    var errors = []
    if (this.get('content.length') > this.get('_schema.maxItems')) {
      errors.push({field: 'base', message: 'too many items in array'})
    }
    return errors
  }.property('content.[]')
})

var JSObject = Ember.Object.extend({<commit_msg>Override replaceContent in array class<commit_after>import Ember from 'ember';
import ZSchema from "npm:z-schema";

export default Ember.Object.extend({
  _schemas: {},
  _validators: {},

  load: function(url, name){
    return Ember.$.getJSON(url, (schema) => {
      this.set('_schemas.' + name, schema);
    });
  },

  createObject: function(name){
    var schema = this.get('_schemas.' + name);
    var obj = create(schema)
    return obj;
  }

});

function create(schema){
  if (schema.type === "object"){
    return JSObject.create({_schema: schema})
  } else if (schema.type === "array"){
    return JSArrayProxy.create({_schema: schema, content: []})
  }
}

var JSArrayProxy = Ember.ArrayProxy.extend({})

JSArrayProxy.reopen({
  replaceContent: function(idx, amt, objects) {
    if (objects) {
      var schema = this.get('_schema.items');
      objects = objects.map((obj) => { return create(schema) })
    }
    this._super(idx, amt, objects)
  },

  isValid: Ember.computed.empty('errors'),

  errors: function(){
    var errors = []
    if (this.get('content.length') > this.get('_schema.maxItems')) {
      errors.push({field: 'base', message: 'too many items in array'})
    }
    return errors
  }.property('content.[]')
})

var JSObject = Ember.Object.extend({
{'fraction_non_alphanumeric': 0.1767857142857143, 'fraction_numerical': 0.0, 'mean_word_length': 3.460191082802548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1642216', 'n_tokens_mistral': 935, 'n_tokens_neox': 908, 'n_words': 196}