<commit_before>import { get } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { A, isArray } from '@ember/array';
import { computed } from '@ember/object';
import Component from '@ember/component';
import InputDefaultsMixin from "ember-form-builder/mixins/input-defaults";

export default Component.extend(InputDefaultsMixin, {
  tagName: "select",
  attributeBindings: ["isMultiple:multiple"],
  isMultiple: false,
  collection: A(),
  optionValuePath: "content.id",
  optionLabelPath: "content.name",
  modelValue: null,
  optionComponentName: "inputs/select-option",

  resolvedCollection: computed("collection.content", {
    get() {
      if (this.get("collection") && this.get("collection").then) {
        this.get("collection").then((result) => {
          this.set("resolvedCollection", result);
        });
        return A();
      } else {
        return this.get("collection");
      }
    },
    set(key, value) {
      return value;
    }
  }),

  didInsertElement: function() {
    if (isEmpty(this.get("value"))) {
      this.change();
    }
  },

  change: function() {
    this._setSelection(this.$("option:selected").map(function() {
      return this.index;
    }).toArray());
  },

  _setSelection: function(indicies) {
    var selection = this.get("collection").objectsAt(indicies);
    var valuePath = this.get("optionValuePath");
    var newValues = A(selection.map(function(item) {
      if (typeof item === "string" || valuePath === "content") {
        return item;
      } else {
        return get(item, valuePath.replace(/^content\./, ""));
      }
    }));

    if (isArray(this.get("value"))) {
      this.get("value").replace(0, this.get("value.length"), newValues);
    } else {
      this.set("value", newValues.get("firstObject"));
    }
  }
});<commit_msg>Fixed setting selection based on resolvedCollection<commit_after>import { get } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { A, isArray } from '@ember/array';
import { computed } from '@ember/object';
import Component from '@ember/component';
import InputDefaultsMixin from "ember-form-builder/mixins/input-defaults";

export default Component.extend(InputDefaultsMixin, {
  tagName: "select",
  attributeBindings: ["isMultiple:multiple"],
  isMultiple: false,
  collection: A(),
  optionValuePath: "content.id",
  optionLabelPath: "content.name",
  modelValue: null,
  optionComponentName: "inputs/select-option",

  resolvedCollection: computed("collection.content", {
    get() {
      if (this.get("collection") && this.get("collection").then) {
        this.get("collection").then((result) => {
          this.set("resolvedCollection", result);
        });
        return A();
      } else {
        return this.get("collection");
      }
    },
    set(key, value) {
      return value;
    }
  }),

  didInsertElement: function() {
    if (isEmpty(this.get("value"))) {
      this.change();
    }
  },

  change: function() {
    this._setSelection(this.$("option:selected").map(function() {
      return this.index;
    }).toArray());
  },

  _setSelection: function(indicies) {
    var selection = this.get("resolvedCollection").objectsAt(indicies);
    var valuePath = this.get("optionValuePath");
    var newValues = A(selection.map(function(item) {
      if (typeof item === "string" || valuePath === "content") {
        return item;
      } else {
        return get(item, valuePath.replace(/^content\./, ""));
      }
    }));

    if (isArray(this.get("value"))) {
      this.get("value").replace(0, this.get("value.length"), newValues);
    } else {
      this.set("value", newValues.get("firstObject"));
    }
  }
});
{'fraction_non_alphanumeric': 0.17247007616974971, 'fraction_numerical': 0.000544069640914037, 'mean_word_length': 3.3360849056603774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1448665', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1095, 'n_words': 213}