import React, { Component } from 'react';
import { isFunction, reduce } from '../utils';
import { assertRedundantProps } from '../assert';


let globalCounter = 0;


export default ({
  selfID,
  reducer,

  selectorFactory,

  // connect
  mapDispatchToProps = null,
  mergeProps = null,
  connectOptions = {},

  ...other
}) => (Target) => {
  assertRedundantProps('local-state-connect', other);

  if (reducer === null || mapDispatchToProps === null) {
    return Target;
  }

  return class extends Component {
    static displayName = `SelfLocalState${globalCounter++}`;

    constructor (...args) {
      super(...args);

      this.state = reducer(undefined, { type: '@SELF_INIT', payload: {} });

      mapDispatchToProps = isFunction(mapDispatchToProps) ? mapDispatchToProps(this.dispatch.bind(this)) : mapDispatchToProps;

      this.actionTriggers = reduce(mapDispatchToProps, (actionTriggers, actionCreator, actionTriggerName) => ({
        ...actionTriggers,
        [actionTriggerName]: (...args) => this.dispatch(actionCreator(...args))
      }), {});

      this.selector = selectorFactory();
    }

    dispatch (action) {
      return new Promise((resolve) => {
        this.setState((state) => reducer(state, action), resolve); // eslint-disable-line react/no-set-state
      });
    }

    render () {
      return (
        <Target {...this.selector(this.state, null, this.props)} {...this.actionTriggers} />
      );
    }
  };
};

@@ -1345,16 +1345,32 @@
  %3CTarget
+ %7B...this.props%7D
  %7B...thi

fix props rendering in local-state