import Ember from 'ember';
import layout from '../templates/components/select-for';
import InputFor from './input-for';

export default InputFor.extend({
  layout,

  init() {
    this._super(...arguments);

    Ember.assert(
      `Must provide an options attribute when using \`${this.toString().match(/component:.[^`:]*/)}\``,
      this.get("options")
    );

    if (this.get("hasBasicOptions")) {
      this.set("selectedValue", this.get(`data.${this.get("field")}`));
    } else {
      this.set(
        "selectedValue",
        this.get(`data.${this.get("field")}.${this.get("valuePath")}`)
      );
    }
  },

  hasBasicOptions: Ember.computed("options", function() {
    return typeof Ember.get(Ember.A(this.get("options")), "firstObject") !== "object";
  }),

  valuePath: Ember.computed("hasBasicOptions", function() {
    if (!this.get("hasBasicOptions")) {
      return "id";
    }
  }),

  selectedOption: Ember.computed("selectedValue", function() {
    if (this.get("hasBasicOptions")) {
      return this.get("selectedValue");
    } else {
      return Ember.A(this.get("options")).
        findBy(this.get("valuePath"), this.get("selectedValue"));
    }
  }),

  updateSelected(value) {
    this.set("selectedValue", value);

    if (this.get("hasBasicOptions")) {
      Ember.set(this.get("data"), this.get("field"), value);
    } else {
      const selectedOption =
        Ember.A(this.get("options")).
        findBy(this.get("valuePath"), value);

      Ember.set(this.get("data"), this.get("field"), selectedOption);
    }
  }
});

@@ -164,12 +164,23 @@
 %0A%0A  
-init
+didReceiveAttrs
 () %7B
@@ -376,254 +376,89 @@
     
-if (this.get(%22hasBasicOptions%22)) %7B%0A      this.set(%22selectedValue%22, this.get(%60data.$%7Bthis.get(%22field%22)%7D%60));%0A    %7D else %7B%0A      this.set(%0A        %22selectedValue%22,%0A        this.get(%60data.$%7Bthis.get(%22field%22)%7D.$%7Bthis.get(%22valuePath%22)%7D%60)%0A      );%0A    %7D
+this.set(%22selectedOption%22, Ember.computed.readOnly(%60data.$%7Bthis.get(%22field%22)%7D%60));
 %0A  %7D
@@ -750,347 +750,31 @@
 %0A%0A  
-selectedOption: Ember.computed(%22selectedValue%22, function() %7B%0A    if (this.get(%22hasBasicOptions%22)) %7B%0A      return this.get(%22selectedValue%22);%0A    %7D else %7B%0A      return Ember.A(this.get(%22options%22)).%0A        findBy(this.get(%22valuePath%22), this.get(%22selectedValue%22));%0A    %7D%0A  %7D),%0A%0A  updateSelected(value) %7B%0A    this.set(%22selectedValue%22, value);%0A
+updateSelected(value) %7B
 %0A   

Simplify select-for component computed properties