/* @flow */

import React, { PropTypes, Component } from 'react';
import { TouchableHighlight } from 'react-native';
import { isEmptyObject } from './util';

type Props = {
  to: any,
  state: ?Object,
  activeStyle: ?Object,
  onlyActiveOnIndex: ?boolean,
  onPress: ?Function,
};

type DefaultProps = {
  style: Object,
  onlyActiveOnIndex: ?boolean,
};

class Link extends Component<DefaultProps, Props, any> {

  static propTypes = {
    to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
    state: PropTypes.object,
    activeStyle: PropTypes.object,
    onlyActiveOnIndex: PropTypes.bool,
    onPress: PropTypes.func,
  };

  static defaultProps = {
    onlyActiveOnIndex: false,
    style: {},
  };

  static contextTypes = {
    router: PropTypes.object,
  };

  componentWillMount(): void {
    (this: any).handlePress = this.handlePress.bind(this);
  }

  props: Props;

  handlePress(event: any): void {
    const { onPress } = this.props;
    if (onPress) {
      onPress(event);
    }

    if (event.defaultPrevented !== true) {
      const { to } = this.props;
      const { router } = this.context;
      router.push(to);
    }
  }

  render(): ReactElement {
    const { to, activeStyle, onlyActiveOnIndex, ...props } = this.props;

    // Ignore if rendered outside the context of router, simplifies unit testing.
    const { router } = this.context;

    if (router && to) {
      const location = to;

      // props.href = router.createHref(location)

      if (activeStyle != null && !isEmptyObject(activeStyle)) {
        if (router.isActive(location, onlyActiveOnIndex)) {
          if (activeStyle) {
            props.style = { ...props.style, ...activeStyle };
          }
        }
      }
    }

    return (
      <TouchableHighlight {...props} onPress={this.handlePress} />
    );
  }
}

export default Link;

@@ -113,48 +113,8 @@
 ve';
-%0Aimport %7B isEmptyObject %7D from './util';
 %0A%0Aty
@@ -521,30 +521,27 @@
 : PropTypes.
-object
+any
 ,%0A    onlyAc
@@ -1227,90 +1227,8 @@
 s;%0A%0A
-    // Ignore if rendered outside the context of router, simplifies unit testing.%0A
     
@@ -1316,120 +1316,37 @@
 to;%0A
-%0A      // props.href = router.createHref(location)%0A%0A      if (activeStyle != null && !isEmptyObject(activeStyle)
+      if (activeStyle != null
 ) %7B%0A
@@ -1464,13 +1464,9 @@
 e = 
-%7B ...
+%5B
 prop
@@ -1474,19 +1474,16 @@
 .style, 
-...
 activeSt
@@ -1485,18 +1485,17 @@
 iveStyle
- %7D
+%5D
 ;%0A      

add support for Stylesheet in Link styles