/** @jsx hJSX */
import cuid from 'cuid';
import decorator from './decorator';
import domQuery from './domQuery';
import intent from './intent';
import model from './model';
import props from './props';
import view from './view';
import {clone} from 'ramda';
import {predicateObjectOfObservable} from '@cyclic/util-predicate';
import {Rx} from '@cycle/core';

const COMPONENT_NAME = `molecule-InputContainer`;

function initAddOns({DOM, state$}) {
  return state$.map(
    (state) => {
      const {addOns} = state;

      return addOns.length > 0 ?
        addOns.map(
          (addOnFunc) => {
            if (typeof addOnFunc === `function`) {
              return addOnFunc(
                {DOM, props$: Rx.Observable.just(clone(state))}
              ).DOM;
            }
          }
        ) :
        [];
    }
  );
}

function InputContainer(sources) {
  const {DOM} = sources;
  const props$ = predicateObjectOfObservable(props)(sources.props$);
  const {id = cuid()} = sources;
  const actions = intent({DOM, id});
  const layout = domQuery({componentName: COMPONENT_NAME, DOM, id});
  const state$ = model(
    {actions, componentName: COMPONENT_NAME, layout, props$}
  );
  const decoration$ = decorator({actions, layout, state$});
  const addOn$s$ = initAddOns({DOM, state$});

  return {
    DOM: view({addOn$s$, decoration$, id, state$}),
    id,
    state$: state$.map((state) => clone(state)),
  };
}

export {COMPONENT_NAME};

export default InputContainer;

@@ -591,82 +591,8 @@
 ) =%3E
- %7B%0A            if (typeof addOnFunc === %60function%60) %7B%0A              return
  add
@@ -599,20 +599,16 @@
 OnFunc(%0A
-    
         
@@ -673,44 +673,13 @@
     
-    
 ).DOM
-;%0A            %7D%0A          %7D
 %0A   

remove redundant type check