/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

import React from 'react';
import { AsyncActionStates } from 'constants/ActorAppConstants';

export class Root extends React.Component {
  static propTypes = {
    className: React.PropTypes.string,
    currentState: React.PropTypes.number.isRequired,
    children: React.PropTypes.array
  };

  constructor(props) {
    super(props);
  }

  render() {
    const { currentState, className, children } = this.props;

    const currentStateChild = React.Children.map(children, (child) => {
      if (currentState === AsyncActionStates.PENDING && child.type === Pending) return child;
      if (currentState === AsyncActionStates.PROCESSING && child.type === Processing) return child;
      if (currentState === AsyncActionStates.SUCCESS && child.type === Success) return child;
      if (currentState === AsyncActionStates.FAILURE && child.type === Failure) return child;
    });

    return (
      <div className={className}>{currentStateChild}</div>
    );
  }
}

export class Pending extends React.Component {
  static propTypes = {
    children: React.PropTypes.array
  };

  render() {
    return this.props.children;
  }
}

export class Processing extends React.Component {
  static propTypes = {
    children: React.PropTypes.array
  };

  render() {
    return this.props.children;
  }
}

export class Success extends React.Component {
  static propTypes = {
    children: React.PropTypes.array
  };

  render() {
    return this.props.children;
  }
}

export class Failure extends React.Component {
  static propTypes = {
    children: React.PropTypes.array
  };

  render() {
    return this.props.children;
  }
}

@@ -53,16 +53,40 @@
 m%3E%0A */%0A%0A
+import _ from 'lodash';%0A
 import R
@@ -104,16 +104,16 @@
 react';%0A
-
 import %7B
@@ -507,63 +507,34 @@
 nst 
-currentStateChild = React.Children.map(children, (child
+equalsState = (state, type
 ) =%3E
@@ -538,36 +538,33 @@
  =%3E %7B%0A      
-if (currentS
+return (s
 tate === Asy
@@ -585,30 +585,24 @@
 .PENDING && 
-child.
 type === Pen
@@ -611,40 +611,26 @@
 ng) 
-return child;%0A      if (currentS
+%7C%7C%0A             (s
 tate
@@ -662,30 +662,24 @@
 OCESSING && 
-child.
 type === Pro
@@ -691,40 +691,26 @@
 ng) 
-return child;%0A      if (currentS
+%7C%7C%0A             (s
 tate
@@ -739,30 +739,24 @@
 .SUCCESS && 
-child.
 type === Suc
@@ -765,40 +765,26 @@
 ss) 
-return child;%0A      if (currentS
+%7C%7C%0A             (s
 tate
@@ -821,14 +821,8 @@
  && 
-child.
 type
@@ -834,16 +834,133 @@
 Failure)
+%0A    %7D;%0A%0A    const currentStateChild = _.find(children, (child) =%3E %7B%0A      if (equalsState(currentState, child.type))
  return 
@@ -1052,17 +1052,16 @@
 v%3E%0A    )
-;
 %0A  %7D%0A%7D%0A%0A
@@ -1152,37 +1152,36 @@
 React.PropTypes.
-array
+node
 %0A  %7D;%0A%0A  render(
@@ -1318,37 +1318,36 @@
 React.PropTypes.
-array
+node
 %0A  %7D;%0A%0A  render(
@@ -1481,37 +1481,36 @@
 React.PropTypes.
-array
+node
 %0A  %7D;%0A%0A  render(
@@ -1644,37 +1644,36 @@
 React.PropTypes.
-array
+node
 %0A  %7D;%0A%0A  render(

use helper function for find needed child component.