<commit_before>/* jshint noyield:true */
import Ember from 'ember';
import DS from 'ember-data';
import ComponentFieldTypeMixin from 'ember-field-components/mixins/component-field-type';

import ModelUtils from 'ember-field-components/classes/model-utils';
import OfflineModelCacheMixin from 'ember-mist-components/mixins/offline-model-cache';

import { task } from 'ember-concurrency';

export default Ember.Component.extend(ComponentFieldTypeMixin, OfflineModelCacheMixin, {
  tagName: '',
  store: Ember.inject.service(),
  init(){
    this._super(...arguments);
    this.get('setInitialValue').perform();
  },
  setInitialValue: task(function * (){
    const { field, model, store, storage, fieldId, isPolymorphic } = this.getProperties('field', 'model', 'store', 'storage', 'fieldId', 'isPolymorphic');
    let relationshipTypeName = this.get('relationshipModelType');

    // This can potentially be an array of modeltypenames in case of a polymorphic relationship
    if(isPolymorphic){
      for(const singleRelationshipTypeName of relationshipTypeName){
        yield this.get('checkOfflineCache').perform(store, storage, singleRelationshipTypeName);
      }
    } else {
      yield this.get('checkOfflineCache').perform(store, storage, relationshipTypeName);
    }

    if(!Ember.isBlank(fieldId)){
      if(isPolymorphic){
        // AAARGGHH private ED api, watch out!
        relationshipTypeName = model.belongsTo(field).belongsToRelationship.inverseRecord.modelName;
      }

      if(store.hasRecordForId(relationshipTypeName, fieldId)){
        this.set('lookupValue', store.peekRecord(relationshipTypeName, fieldId));
      } else {
        yield model.get(field).then((value) => {
          this.set('lookupValue', value);
        });
      }
    }

    if(this.get('isSelect')){
      yield this.get('setSelectOptions').perform();
    }
  }),
  relationshipModelType: Ember.computed('model', 'field', function(){
    if(this.get('isPolymorphic')){
      return ModelUtils.getParentModelTypeNames(this.get('model'), this.get('field'), this.get('store'));
    } else {
      return ModelUtils.getParentModelTypeName(this.get('model'), this.get('field'));
    }
  }),
  isRequired: Ember.computed('relationshipAttributeOptions', function(){
    return this.get('relationshipAttributeOptions').validation.required;
  }),
  isPolymorphic: Ember.computed('relationshipAttributeOptions', function(){
    const options = this.get('relationshipAttributeOptions');
    return options.hasOwnProperty('polymorphic') && options.polymorphic;
  }),
  fieldId: Ember.computed('model', 'field', function(){
    return this.get('model').belongsTo(this.get('field')).id();
  }),
  isSelect: Ember.computed(function(){
    return ModelUtils.hasWidget(this.get('relationshipAttributeOptions'), 'select');
  }),
  setSelectOptions: task(function * (){
    const store = this.get('store');
    const relationshipType = ModelUtils.getParentModelType(this.get('model'), this.get('field'), store);
    const relationshipTypeName = this.get('relationshipModelType');

    Ember.assert('Select widget is only supported for Offline cacheable models', ModelUtils.modelTypeIsCacheable(relationshipType));
    Ember.assert('Select widget is not supported for polymorphic relationships', !this.get('isPolymorphic'));

    const models = store.peekAll(relationshipTypeName);

    let selectOptions = [];
    models.forEach((model) => {
      let selectOption = {};
      selectOption.value = model.get('id');
      selectOption.label = model.get('name');
      selectOptions.push(selectOption);
    });<commit_msg>Bugfix<commit_after>/* jshint noyield:true */
import Ember from 'ember';
import DS from 'ember-data';
import ComponentFieldTypeMixin from 'ember-field-components/mixins/component-field-type';

import ModelUtils from 'ember-field-components/classes/model-utils';
import OfflineModelCacheMixin from 'ember-mist-components/mixins/offline-model-cache';

import { task } from 'ember-concurrency';

export default Ember.Component.extend(ComponentFieldTypeMixin, OfflineModelCacheMixin, {
  tagName: '',
  store: Ember.inject.service(),
  init(){
    this._super(...arguments);
    this.get('setInitialValue').perform();
  },
  setInitialValue: task(function * (){
    const { field, model, store, storage, fieldId, isPolymorphic } = this.getProperties('field', 'model', 'store', 'storage', 'fieldId', 'isPolymorphic');
    let relationshipTypeName = this.get('relationshipModelType');

    // This can potentially be an array of modeltypenames in case of a polymorphic relationship
    if(isPolymorphic){
      for(const singleRelationshipTypeName of relationshipTypeName){
        yield this.get('checkOfflineCache').perform(store, storage, singleRelationshipTypeName);
      }
    } else {
      yield this.get('checkOfflineCache').perform(store, storage, relationshipTypeName);
    }

    if(!Ember.isBlank(fieldId)){
      if(isPolymorphic){
        // AAARGGHH private ED api, watch out!
        relationshipTypeName = model.belongsTo(field).belongsToRelationship.inverseRecord.modelName;
      }

      if(store.hasRecordForId(relationshipTypeName, fieldId)){
        this.set('lookupValue', store.peekRecord(relationshipTypeName, fieldId));
      } else {
        yield model.get(field).then((value) => {
          this.set('lookupValue', value);
        });
      }
    }

    if(this.get('isSelect')){
      yield this.get('setSelectOptions').perform();
    }
  }),
  relationshipModelType: Ember.computed('model', 'field', function(){
    if(this.get('isPolymorphic')){
      return ModelUtils.getParentModelTypeNames(this.get('model'), this.get('field'), this.get('store'));
    } else {
      return ModelUtils.getParentModelTypeName(this.get('model'), this.get('field'));
    }
  }),
  isRequired: Ember.computed('relationshipAttributeOptions', function(){
    const options = this.get('relationshipAttributeOptions');
    return options.hasOwnProperty('validation') && options.validation.hasOwnProperty('required') && options.validation.required;
  }),
  isPolymorphic: Ember.computed('relationshipAttributeOptions', function(){
    const options = this.get('relationshipAttributeOptions');
    return options.hasOwnProperty('polymorphic') && options.polymorphic;
  }),
  fieldId: Ember.computed('model', 'field', function(){
    return this.get('model').belongsTo(this.get('field')).id();
  }),
  isSelect: Ember.computed(function(){
    return ModelUtils.hasWidget(this.get('relationshipAttributeOptions'), 'select');
  }),
  setSelectOptions: task(function * (){
    const store = this.get('store');
    const relationshipType = ModelUtils.getParentModelType(this.get('model'), this.get('field'), store);
    const relationshipTypeName = this.get('relationshipModelType');

    Ember.assert('Select widget is only supported for Offline cacheable models', ModelUtils.modelTypeIsCacheable(relationshipType));
    Ember.assert('Select widget is not supported for polymorphic relationships', !this.get('isPolymorphic'));

    const models = store.peekAll(relationshipTypeName);

    let selectOptions = [];
    models.forEach((model) => {
      let selectOption = {};
      selectOption.value = model.get('id');
      selectOption.label = model.get('name');
      selectOptions.push(selectOption);
    });
{'fraction_non_alphanumeric': 0.14234924107753316, 'fraction_numerical': 0.0, 'mean_word_length': 4.591743119266055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '409807', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2038, 'n_words': 383}