import {
  moduleForModel,
  test
} from 'ember-qunit';
import modelList from '../../helpers/model-list';
import { initialize } from '../../../initializers/replace-promise';
import Ember from 'ember';

const { run } = Ember;

initialize();
moduleForModel('objective', 'Unit | Model | Objective', {
  needs: modelList
});

test('it exists', function(assert) {
  let model = this.subject();
  // let store = this.store();
  assert.ok(!!model);
});

test('top parent with no parents should be self', async function(assert) {
  assert.expect(2);

  let model = this.subject();
  run(async () => {
    let topParents = await model.get('topParents');
    assert.ok(topParents.length === 1);
    assert.equal(topParents.get('firstObject'), model);
  });
});

test('current top parents with single parent tree', async function(assert) {
  assert.expect(2);
  let store = this.store();
  let model = this.subject();
  run(async () => {
    let parent1 = store.createRecord('objective', {
      children: [model]
    });
    let parent2 = store.createRecord('objective', {
      children: [parent1]
    });

    let topParents = await model.get('topParents');
    assert.ok(topParents.length === 1);
    assert.equal(topParents.get('firstObject'), parent2);
  });
});

test('current top parents with multi parent tree', async function(assert) {
  assert.expect(3);
  let store = this.store();
  let model = this.subject();
  run(async () => {
    let parent1 = store.createRecord('objective', {
      children: [model]
    });
    let parent2 = store.createRecord('objective', {
      children: [model]
    });
    let parent3 = store.createRecord('objective', {
      children: [parent1]
    });
    let parent4 = store.createRecord('objective', {
      children: [parent2]
    });

    let topParents = await model.get('topParents');
    assert.ok(topParents.length === 2);
    assert.ok(topParents.includes(parent3));
    assert.ok(topParents.includes(parent4));
  });

});

test('tree competencies', async function(assert) {
  assert.expect(3);
  let store = this.store();
  let model = this.subject();
  run(async () => {
    let competency1 = store.createRecord('competency');
    let competency2 = store.createRecord('competency');

    let parent1 = store.createRecord('objective', {
      children: [model]
    });
    let parent2 = store.createRecord('objective', {
      children: [model]
    });
    let parent3 = store.createRecord('objective', {
      children: [model]
    });
    store.createRecord('objective', {
      children: [model]
    });
    store.createRecord('objective', {
      children: [parent1],
      competency: competency1
    });
    store.createRecord('objective', {
      children: [parent2],
      competency: competency1
    });
    store.createRecord('objective', {
      children: [parent2],
    });
    store.createRecord('objective', {
      children: [parent3],
      competency: competency2
    });
    store.createRecord('objective', {
      children: [parent3],
      competency: competency1
    });

    let treeCompetencies = await model.get('treeCompetencies');
    assert.equal(2, treeCompetencies.length);
    assert.ok(treeCompetencies.includes(competency1));
    assert.ok(treeCompetencies.includes(competency2));
  });
});

@@ -560,32 +560,38 @@
 is.subject();%0A  
+await 
 run(async () =%3E 
@@ -584,32 +584,32 @@
 n(async () =%3E %7B%0A
-
     let topParen
@@ -900,32 +900,38 @@
 is.subject();%0A  
+await 
 run(async () =%3E 
@@ -1412,32 +1412,38 @@
 is.subject();%0A  
+await 
 run(async () =%3E 
@@ -2110,16 +2110,22 @@
 ct();%0A  
+await 
 run(asyn
@@ -3216,24 +3216,24 @@
 petency1));%0A
-
     assert.o
@@ -3273,20 +3273,1084 @@
 tency2));%0A  %7D);%0A%7D);%0A
+%0A%0Atest('removeParentWithProgramYears', async function(assert) %7B%0A  assert.expect(3);%0A  let store = this.store();%0A  let model = this.subject();%0A  await run( async () =%3E %7B%0A    model.reopen(%7B%0A      async save() %7B%0A        assert.ok(true, 'save() was called.')%0A      %7D%0A    %7D);%0A    const programYear1 = store.createRecord('programYear');%0A    const parentObjective1 = store.createRecord('objective', %7B programYears: %5B programYear1 %5D %7D);%0A    const programYear2 = store.createRecord('programYear');%0A    const parentObjective2 = store.createRecord('objective', %7B programYears: %5B programYear2 %5D %7D);%0A    const programYear3 = store.createRecord('programYear');%0A    const parentObjective3 = store.createRecord('objective', %7B programYears: %5B programYear3 %5D %7D);%0A%0A    model.get('parents').pushObjects(%5BparentObjective1, parentObjective2, parentObjective3 %5D);%0A%0A    await model.removeParentWithProgramYears(%5BprogramYear1, programYear2%5D);%0A    const parents = await model.get('parents');%0A    assert.equal(parents.length, 1);%0A    assert.ok(parents.includes(parentObjective3));%0A  %7D);%0A%7D);%0A

test coverage for removeParentWithProgramYears()