import { set } from '@ember/object';
import { resolve } from 'rsvp';

export function receiveValue(component) {
  resolveValue(component, component.value);
}

export function resolveValue(component, unresolvedValue, process) {
  const valueID = component.incrementProperty('valueID');

  startedResolvingValue(component, unresolvedValue);

  return resolve(unresolvedValue)
    .then(result => {
      finishedResolvingValue(component, valueID, false, result, process);
    })
    .catch(error => {
      finishedResolvingValue(component, valueID, true, error, process);
    });
}

export function startedResolvingValue(component, unresolvedValue) {
  set(component, 'previousResolvedValue', component.resolvedValue);
  set(component, 'resolvedValue', unresolvedValue);
  set(component, 'isPending', true);
  set(component, 'isRejected', false);
  set(component, 'isFulfilled', false);
  set(component, 'isSettled', false);
}

export function finishedResolvingValue(
  component,
  valueID,
  failed,
  result,
  process
) {
  if (component.isDestroyed || valueID < component.valueID) {
    return;
  }

  let value = result;

  if (typeof process === 'function') {
    value = process(component, value);
  }

  set(component, 'resolvedValue', value);
  set(component, 'isPending', false);
  set(component, 'isRejected', failed);
  set(component, 'isFulfilled', !failed);
  set(component, 'isSettled', true);
}

@@ -210,16 +210,20 @@
 Value, p
+ostP
 rocess) 
@@ -458,16 +458,20 @@
 esult, p
+ostP
 rocess);
@@ -563,16 +563,20 @@
 error, p
+ostP
 rocess);
@@ -1019,16 +1019,20 @@
 ult,%0A  p
+ostP
 rocess%0A)
@@ -1150,16 +1150,20 @@
 typeof p
+ostP
 rocess =
@@ -1192,16 +1192,20 @@
 alue = p
+ostP
 rocess(c

rename arg