<commit_before>// This lint error disables "this.attrs" everywhere.  What could go wrong?
/* eslint-disable ember/no-attrs-in-components */

import Ember from 'ember';
import DS from 'ember-data';
import { RootState } from 'ember-data/-private';
import { dasherize } from '@ember/string';
import EmberObject, { computed, get, set, defineProperty } from '@ember/object';
import { A, isArray } from '@ember/array';
import { warn } from '@ember/debug';
import { alias } from '@ember/object/computed';
import Map from '@ember/map';

import SchemaManager from './schema-manager';
import M3RecordArray from './record-array';
import { OWNER_KEY } from './util';

const { propertyDidChange } = Ember;
let { notifyPropertyChange } = Ember;

const HasNotifyPropertyChange = notifyPropertyChange !== undefined;
if (!HasNotifyPropertyChange) {
  notifyPropertyChange = propertyDidChange;
}

const {
  deleted: { uncommitted: deletedUncommitted },
  loaded: {
    saved: loadedSaved,
    updated: { uncommitted: updatedUncommitted },
  },
} = RootState;

class EmbeddedSnapshot {
  constructor(record) {
    this.record = record;
    this.modelName = this.record._internalModel.modelName;
    this.attrs = Object.create(null);
    this.eachAttribute(key => (this.attrs[key] = this.record.get(key)));
  }

  serialize(options) {
    return this.record._store.serializerFor('-ember-m3').serialize(this, options);
  }

  eachAttribute(callback, binding) {
    return this.record.eachAttribute(callback, binding);
  }

  attr(key) {
    return this.attrs[key];
  }
}

// TODO: shouldn't need this anymore; this level of indirection for nested modeldata isn't useful
class EmbeddedInternalModel {
  constructor({ id, modelName, parentInternalModel, parentKey, parentIdx }) {
    this.id = id;
    this.modelName = modelName;

    this._modelData = parentInternalModel._modelData._getChildModelData(
      parentKey,
      parentIdx,
      modelName,
      id,
      this
    );
    this.parentInternalModel = parentInternalModel;

    this.record = null;
  }

  createSnapshot() {
    return new EmbeddedSnapshot(this.record);
  }
}

function _setAttribute(model, attr, value) {
  const schemaInterface = model._internalModel._modelData.schemaInterface;
  model._schema.setAttribute(model._modelName, attr, value, schemaInterface);
}

function _computeAttributeReference(key, value, modelName, schemaInterface, schema) {
  schemaInterface._beginDependentKeyResolution(key);
  let reference = schema.computeAttributeReference(key, value, modelName, schemaInterface);
  schemaInterface._endDependentKeyResolution(key);
  return reference;
}

function _isResolvedValue(value) {
  return value && value.constructor && value.constructor.isModel;
}

function resolveReference(store, reference) {
  if (reference.type === null) {
    // for schemas with a global id-space but multiple types, schemas may
    // report a type of null
    let internalModel = store._globalM3Cache[reference.id];
    return internalModel ? internalModel.getRecord() : null;
  } else {
    // respect the user schema's type if provided
    return store.peekRecord(reference.type, reference.id);
  }
}

function resolveReferenceOrReferences(store, value, reference) {
  if (Array.isArray(value) || Array.isArray(reference)) {
    return resolveRecordArray(store, reference);
  }

  return resolveReference(store, reference);
}

/**
 * There are two different type of values we have to worry about:
 * 1. References
 * 2. Nested Models
 *
 * Here is a mapping of input -> output:
 * 1. Single reference -> resolved reference
 * 2. Array of references -> RecordArray of resolved references
 * 3. Single nested model -> EmbeddedMegaMorphicModel
 * 4. Array of nested models -> array of EmbeddedMegaMorphicModel
 */
function resolveValue(key, value, modelName, store, schema, model, parentIdx) {
  const schemaInterface = model._internalModel._modelData.schemaInterface;

  // First check to see if given value is either a reference or an array of references
  let reference = _computeAttributeReference(key, value, modelName, schemaInterface, schema);
  if (reference !== undefined && reference !== null) {
    return resolveReferenceOrReferences(store, value, reference);
  }

  if (Array.isArray(value)) {
    return resolvePlainArray(key, value, modelName, store, schema, model);
  }
  let nested = schema.computeNestedModel(key, value, modelName, schemaInterface);
  if (nested) {
    let internalModel = new EmbeddedInternalModel({
      // nested models with ids is pretty misleading; all they really ought to need is type
      id: nested.id,
      // maintain consistency with internalmodel.modelName, which is normalized
      // internally within ember-data
      modelName: nested.type ? dasherize(nested.type) : null,
      parentInternalModel: model._internalModel,
      parentKey: key,
      parentIdx,
    });

    let nestedModel = new EmbeddedMegamorphicModel({
      store,
      _internalModel: internalModel,
      _parentModel: model,
      _topModel: model._topModel,
    });
    internalModel.record = nestedModel;<commit_msg>allow computeNestedModel to access different properties<commit_after>// This lint error disables "this.attrs" everywhere.  What could go wrong?
/* eslint-disable ember/no-attrs-in-components */

import Ember from 'ember';
import DS from 'ember-data';
import { RootState } from 'ember-data/-private';
import { dasherize } from '@ember/string';
import EmberObject, { computed, get, set, defineProperty } from '@ember/object';
import { A, isArray } from '@ember/array';
import { warn } from '@ember/debug';
import { alias } from '@ember/object/computed';
import Map from '@ember/map';

import SchemaManager from './schema-manager';
import M3RecordArray from './record-array';
import { OWNER_KEY } from './util';

const { propertyDidChange } = Ember;
let { notifyPropertyChange } = Ember;

const HasNotifyPropertyChange = notifyPropertyChange !== undefined;
if (!HasNotifyPropertyChange) {
  notifyPropertyChange = propertyDidChange;
}

const {
  deleted: { uncommitted: deletedUncommitted },
  loaded: {
    saved: loadedSaved,
    updated: { uncommitted: updatedUncommitted },
  },
} = RootState;

class EmbeddedSnapshot {
  constructor(record) {
    this.record = record;
    this.modelName = this.record._internalModel.modelName;
    this.attrs = Object.create(null);
    this.eachAttribute(key => (this.attrs[key] = this.record.get(key)));
  }

  serialize(options) {
    return this.record._store.serializerFor('-ember-m3').serialize(this, options);
  }

  eachAttribute(callback, binding) {
    return this.record.eachAttribute(callback, binding);
  }

  attr(key) {
    return this.attrs[key];
  }
}

// TODO: shouldn't need this anymore; this level of indirection for nested modeldata isn't useful
class EmbeddedInternalModel {
  constructor({ id, modelName, parentInternalModel, parentKey, parentIdx }) {
    this.id = id;
    this.modelName = modelName;

    this._modelData = parentInternalModel._modelData._getChildModelData(
      parentKey,
      parentIdx,
      modelName,
      id,
      this
    );
    this.parentInternalModel = parentInternalModel;

    this.record = null;
  }

  createSnapshot() {
    return new EmbeddedSnapshot(this.record);
  }
}

function _setAttribute(model, attr, value) {
  const schemaInterface = model._internalModel._modelData.schemaInterface;
  model._schema.setAttribute(model._modelName, attr, value, schemaInterface);
}

function _computeAttributeReference(key, value, modelName, schemaInterface, schema) {
  schemaInterface._beginDependentKeyResolution(key);
  let reference = schema.computeAttributeReference(key, value, modelName, schemaInterface);
  schemaInterface._endDependentKeyResolution(key);
  return reference;
}

function _computeNestedModel(key, value, modelName, schemaInterface, schema) {
  schemaInterface._beginDependentKeyResolution(key);
  let nestedModel = schema.computeNestedModel(key, value, modelName, schemaInterface);
  schemaInterface._endDependentKeyResolution(key);
  return nestedModel;
}

function _isResolvedValue(value) {
  return value && value.constructor && value.constructor.isModel;
}

function resolveReference(store, reference) {
  if (reference.type === null) {
    // for schemas with a global id-space but multiple types, schemas may
    // report a type of null
    let internalModel = store._globalM3Cache[reference.id];
    return internalModel ? internalModel.getRecord() : null;
  } else {
    // respect the user schema's type if provided
    return store.peekRecord(reference.type, reference.id);
  }
}

function resolveReferenceOrReferences(store, value, reference) {
  if (Array.isArray(value) || Array.isArray(reference)) {
    return resolveRecordArray(store, reference);
  }

  return resolveReference(store, reference);
}

/**
 * There are two different type of values we have to worry about:
 * 1. References
 * 2. Nested Models
 *
 * Here is a mapping of input -> output:
 * 1. Single reference -> resolved reference
 * 2. Array of references -> RecordArray of resolved references
 * 3. Single nested model -> EmbeddedMegaMorphicModel
 * 4. Array of nested models -> array of EmbeddedMegaMorphicModel
 */
function resolveValue(key, value, modelName, store, schema, model, parentIdx) {
  const schemaInterface = model._internalModel._modelData.schemaInterface;

  // First check to see if given value is either a reference or an array of references
  let reference = _computeAttributeReference(key, value, modelName, schemaInterface, schema);
  if (reference !== undefined && reference !== null) {
    return resolveReferenceOrReferences(store, value, reference);
  }

  if (Array.isArray(value)) {
    return resolvePlainArray(key, value, modelName, store, schema, model);
  }
  let nested = _computeNestedModel(key, value, modelName, schemaInterface, schema);
  if (nested) {
    let internalModel = new EmbeddedInternalModel({
      // nested models with ids is pretty misleading; all they really ought to need is type
      id: nested.id,
      // maintain consistency with internalmodel.modelName, which is normalized
      // internally within ember-data
      modelName: nested.type ? dasherize(nested.type) : null,
      parentInternalModel: model._internalModel,
      parentKey: key,
      parentIdx,
    });

    let nestedModel = new EmbeddedMegamorphicModel({
      store,
      _internalModel: internalModel,
      _parentModel: model,
      _topModel: model._topModel,
    });
    internalModel.record = nestedModel;
{'fraction_non_alphanumeric': 0.10427934339121359, 'fraction_numerical': 0.0017079419299743809, 'mean_word_length': 4.225582548339117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '175679', 'n_tokens_mistral': 2981, 'n_tokens_neox': 2933, 'n_words': 831}