function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/* eslint-disable import/no-unresolved, import/extensions */
import React, { PureComponent } from 'react';
import { Animated, Easing, PanResponder, StyleSheet, View, ViewPropTypes } from 'react-native';
import { PropTypes } from 'prop-types';
/* eslint-enable import/no-unresolved, import/extensions */

function noop() {}
export default class Swipeable extends PureComponent {
  constructor() {
    var _this;
    super(...arguments);
    _this = this;
    _defineProperty(this, "state", {
      pan: new Animated.ValueXY(),
      width: 0,
      lastOffset: {
        x: 0,
        y: 0
      },
      leftActionActivated: false,
      leftButtonsActivated: false,
      leftButtonsOpen: false,
      rightActionActivated: false,
      rightButtonsActivated: false,
      rightButtonsOpen: false
    });
    _defineProperty(this, "recenter", function () {
      let animationFn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.swipeReleaseAnimationFn;
      let animationConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.props.swipeReleaseAnimationConfig;
      let onDone = arguments.length > 2 ? arguments[2] : undefined;
      const {
        pan
      } = _this.state;
      _this.setState({
        lastOffset: {
          x: 0,
          y: 0
        },
        leftActionActivated: false,
        leftButtonsActivated: false,
        leftButtonsOpen: false,
        rightActionActivated: false,
        rightButtonsActivated: false,
        rightButtonsOpen: false
      });
      pan.flattenOffset();
      animationFn(pan, animationConfig).start(onDone);
    });
    _defineProperty(this, "_unmounted", false);
    _defineProperty(this, "_handlePan", Animated.event([null, {
      dx: this.state.pan.x,
      dy: this.state.pan.y
    }], {
      useNativeDriver: false
    }));
    _defineProperty(this, "_handleMoveShouldSetPanResponder", (event, gestureState) => Math.abs(gestureState.dx) > this.props.swipeStartMinDistance);
    _defineProperty(this, "_handlePanResponderStart", (event, gestureState) => {
      const {
        lastOffset,
        pan
      } = this.state;
      pan.setOffset(lastOffset);
      this.props.onSwipeStart(event, gestureState, this);
    });
    _defineProperty(this, "_handlePanResponderMove", (event, gestureState) => {
      const {
        leftActionActivationDistance,
        leftButtonsActivationDistance,
        onLeftActionActivate,
        onLeftActionDeactivate,
        onLeftButtonsActivate,
        onLeftButtonsDeactivate,
        rightActionActivationDistance,
        rightButtonsActivationDistance,
        onRightActionActivate,
        onRightActionDeactivate,
        onRightButtonsActivate,
        onRightButtonsDeactivate,
        onSwipeMove
      } = this.props;
      const {
        lastOffset,
        leftActionActivated,
        leftButtonsActivated,
        rightActionActivated,
        rightButtonsActivated
      } = this.state;
      const {
        dx,
        vx
      } = gestureState;
      const x = dx + lastOffset.x;
      const canSwipeRight = this._canSwipeRight();
      const canSwipeLeft = this._canSwipeLeft();
      const hasLeftButtons = this._hasLeftButtons();
      const hasRightButtons = this._hasRightButtons();
      const isSwipingLeft = vx < 0;
      const isSwipingRight = vx > 0;
      let nextLeftActionActivated = leftActionActivated;
      let nextLeftButtonsActivated = leftButtonsActivated;
      let nextRightActionActivated = rightActionActivated;
      let nextRightButtonsActivated = rightButtonsActivated;
      this._handlePan(event, gestureState);
      onSwipeMove(event, gestureState, this);
      if (!leftActionActivated && canSwipeRight && x >= leftActionActivationDistance) {
        nextLeftActionActivated = true;
        onLeftActionActivate(event, gestureState, this);
      }
      if (leftActionActivated && canSwipeRight && x < leftActionActivationDistance) {
        nextLeftActionActivated = false;
        onLeftActionDeactivate(event, gestureState, this);
      }
      if (!rightActionActivated && canSwipeLeft && x <= -rightActionActivationDistance) {
        nextRightActionActivated = true;
        onRightActionActivate(event, gestureState, this);
      }
      if (rightActionActivated && canSwipeLeft && x > -rightActionActivationDistance) {
        nextRightActionActivated = false;
        onRightActionDeactivate(event, gestureState, this);
      }
      if (!leftButtonsActivated && hasLeftButtons && !isSwipingLeft && x >= leftButtonsActivationDistance) {
        nextLeftButtonsActivated = true;
        onLeftButtonsActivate(event, gestureState, this);
      }
      if (leftButtonsActivated && hasLeftButtons && isSwipingLeft) {
        nextLeftButtonsActivated = false;
        onLeftButtonsDeactivate(event, gestureState, this);
      }
      if (!rightButtonsActivated && hasRightButtons && !isSwipingRight && x <= -rightButtonsActivationDistance) {
        nextRightButtonsActivated = true;
        onRightButtonsActivate(event, gestureState, this);
      }
      if (rightButtonsActivated && hasRightButtons && isSwipingRight) {
        nextRightButtonsActivated = false;
        onRightButtonsDeactivate(event, gestureState, this);
      }
      const needsUpdate = nextLeftActionActivated !== leftActionActivated || nextLeftButtonsActivated !== leftButtonsActivated || nextRightActionActivated !== rightActionActivated || nextRightButtonsActivated !== rightButtonsActivated;
      if (needsUpdate) {
        this.setState({
          leftActionActivated: nextLeftActionActivated,
          leftButtonsActivated: nextLeftButtonsActivated,
          rightActionActivated: nextRightActionActivated,
          rightButtonsActivated: nextRightButtonsActivated
        });
      }
    });
    _defineProperty(this, "_handlePanResponderEnd", (event, gestureState) => {
      const {
        onLeftActionRelease,
        onLeftActionDeactivate,
        onLeftButtonsOpenRelease,
        onLeftButtonsCloseRelease,
        onRightActionRelease,
        onRightActionDeactivate,
        onRightButtonsOpenRelease,
        onRightButtonsCloseRelease,
        onSwipeRelease
      } = this.props;
      const {
        leftActionActivated,
        leftButtonsOpen,
        leftButtonsActivated,
        rightActionActivated,
        rightButtonsOpen,
        rightButtonsActivated,
        pan
      } = this.state;
      const animationFn = this._getReleaseAnimationFn();
      const animationConfig = this._getReleaseAnimationConfig();
      onSwipeRelease(event, gestureState, this);
      if (leftActionActivated) {
        onLeftActionRelease(event, gestureState, this);
      }
      if (rightActionActivated) {
        onRightActionRelease(event, gestureState, this);
      }
      if (leftButtonsActivated && !leftButtonsOpen) {
        onLeftButtonsOpenRelease(event, gestureState, this);
      }
      if (!leftButtonsActivated && leftButtonsOpen) {
        onLeftButtonsCloseRelease(event, gestureState, this);
      }
      if (rightButtonsActivated && !rightButtonsOpen) {
        onRightButtonsOpenRelease(event, gestureState, this);
      }
      if (!rightButtonsActivated && rightButtonsOpen) {
        onRightButtonsCloseRelease(event, gestureState, this);
      }
      this.setState({
        lastOffset: {
          x: animationConfig.toValue.x,
          y: animationConfig.toValue.y
        },
        leftActionActivated: false,
        rightActionActivated: false,
        leftButtonsOpen: leftButtonsActivated,
        rightButtonsOpen: rightButtonsActivated
      });
      pan.flattenOffset();
      animationFn(pan, animationConfig).start(() => {
        if (this._unmounted) {
          return;
        }
        const {
          onLeftActionComplete,
          onLeftButtonsOpenComplete,
          onLeftButtonsCloseComplete,
          onRightActionComplete,
          onRightButtonsOpenComplete,
          onRightButtonsCloseComplete,
          onSwipeComplete
        } = this.props;
        onSwipeComplete(event, gestureState, this);
        if (leftActionActivated) {
          onLeftActionComplete(event, gestureState, this);
          onLeftActionDeactivate(event, gestureState, this);
        }
        if (rightActionActivated) {
          onRightActionComplete(event, gestureState, this);
          onRightActionDeactivate(event, gestureState, this);
        }
        if (leftButtonsActivated && !leftButtonsOpen) {
          onLeftButtonsOpenComplete(event, gestureState, this);
        }
        if (!leftButtonsActivated && leftButtonsOpen) {
          onLeftButtonsCloseComplete(event, gestureState, this);
        }
        if (rightButtonsActivated && !rightButtonsOpen) {
          onRightButtonsOpenComplete(event, gestureState, this);
        }
        if (!rightButtonsActivated && rightButtonsOpen) {
          onRightButtonsCloseComplete(event, gestureState, this);
        }
      });
    });
    _defineProperty(this, "_panResponder", PanResponder.create({
      onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
      onMoveShouldSetPanResponderCapture: this._handleMoveShouldSetPanResponder,
      onPanResponderGrant: this._handlePanResponderStart,
      onPanResponderMove: this._handlePanResponderMove,
      onPanResponderRelease: this._handlePanResponderEnd,
      onPanResponderTerminate: this._handlePanResponderEnd,
      onPanResponderTerminationRequest: this._handlePanResponderEnd
    }));
    _defineProperty(this, "_handleLayout", _ref => {
      let {
        nativeEvent: {
          layout: {
            width
          }
        }
      } = _ref;
      return this.setState({
        width
      });
    });
  }
  componentDidMount() {
    const {
      onPanAnimatedValueRef,
      onRef
    } = this.props;
    onRef(this);
    onPanAnimatedValueRef(this.state.pan);
  }
  componentWillUnmount() {
    this._unmounted = true;
  }
  _canSwipeRight() {
    return this.props.leftContent || this._hasLeftButtons();
  }
  _canSwipeLeft() {
    return this.props.rightContent || this._hasRightButtons();
  }
  _hasLeftButtons() {
    const {
      leftButtons,
      leftContent
    } = this.props;
    return !leftContent && leftButtons && leftButtons.length;
  }
  _hasRightButtons() {
    const {
      rightButtons,
      rightContent
    } = this.props;
    return !rightContent && rightButtons && rightButtons.length;
  }
  _getReleaseAnimationFn() {
    const {
      leftActionReleaseAnimationFn,
      leftButtonsOpenReleaseAnimationFn,
      leftButtonsCloseReleaseAnimationFn,
      rightActionReleaseAnimationFn,
      rightButtonsOpenReleaseAnimationFn,
      rightButtonsCloseReleaseAnimationFn,
      swipeReleaseAnimationFn
    } = this.props;
    const {
      leftActionActivated,
      leftButtonsActivated,
      leftButtonsOpen,
      rightActionActivated,
      rightButtonsActivated,
      rightButtonsOpen
    } = this.state;
    if (leftActionActivated && leftActionReleaseAnimationFn) {
      return leftActionReleaseAnimationFn;
    }
    if (rightActionActivated && rightActionReleaseAnimationFn) {
      return rightActionReleaseAnimationFn;
    }
    if (leftButtonsActivated && leftButtonsOpenReleaseAnimationFn) {
      return leftButtonsOpenReleaseAnimationFn;
    }
    if (!leftButtonsActivated && leftButtonsOpen && leftButtonsCloseReleaseAnimationFn) {
      return leftButtonsCloseReleaseAnimationFn;
    }
    if (rightButtonsActivated && rightButtonsOpenReleaseAnimationFn) {
      return rightButtonsOpenReleaseAnimationFn;
    }
    if (!rightButtonsActivated && rightButtonsOpen && rightButtonsCloseReleaseAnimationFn) {
      return rightButtonsCloseReleaseAnimationFn;
    }
    return swipeReleaseAnimationFn;
  }
  _getReleaseAnimationConfig() {
    const {
      leftActionReleaseAnimationConfig,
      leftButtons,
      leftButtonsOpenReleaseAnimationConfig,
      leftButtonsCloseReleaseAnimationConfig,
      leftButtonWidth,
      rightActionReleaseAnimationConfig,
      rightButtons,
      rightButtonsOpenReleaseAnimationConfig,
      rightButtonsCloseReleaseAnimationConfig,
      rightButtonWidth,
      swipeReleaseAnimationConfig
    } = this.props;
    const {
      leftActionActivated,
      leftButtonsActivated,
      leftButtonsOpen,
      rightActionActivated,
      rightButtonsActivated,
      rightButtonsOpen
    } = this.state;
    if (leftActionActivated && leftActionReleaseAnimationConfig) {
      return leftActionReleaseAnimationConfig;
    }
    if (rightActionActivated && rightActionReleaseAnimationConfig) {
      return rightActionReleaseAnimationConfig;
    }
    if (leftButtonsActivated) {
      return {
        ...swipeReleaseAnimationConfig,
        toValue: {
          x: leftButtons.length * leftButtonWidth,
          y: 0
        },
        ...leftButtonsOpenReleaseAnimationConfig
      };
    }
    if (rightButtonsActivated) {
      return {
        ...swipeReleaseAnimationConfig,
        toValue: {
          x: rightButtons.length * rightButtonWidth * -1,
          y: 0
        },
        ...rightButtonsOpenReleaseAnimationConfig
      };
    }
    if (!leftButtonsActivated && leftButtonsOpen && leftButtonsCloseReleaseAnimationConfig) {
      return leftButtonsCloseReleaseAnimationConfig;
    }
    if (!rightButtonsActivated && rightButtonsOpen && rightButtonsCloseReleaseAnimationConfig) {
      return rightButtonsCloseReleaseAnimationConfig;
    }
    return swipeReleaseAnimationConfig;
  }
  _renderButtons(buttons, isLeftButtons) {
    const {
      leftButtonContainerStyle,
      rightButtonContainerStyle
    } = this.props;
    const {
      pan,
      width
    } = this.state;
    const canSwipeLeft = this._canSwipeLeft();
    const canSwipeRight = this._canSwipeRight();
    const count = buttons.length;
    const leftEnd = canSwipeLeft ? -width : 0;
    const rightEnd = canSwipeRight ? width : 0;
    const inputRange = isLeftButtons ? [0, rightEnd] : [leftEnd, 0];
    return buttons.map((buttonContent, index) => {
      const outputMultiplier = -index / count;
      const outputRange = isLeftButtons ? [0, rightEnd * outputMultiplier] : [leftEnd * outputMultiplier, 0];
      const transform = [{
        translateX: pan.x.interpolate({
          inputRange,
          outputRange,
          extrapolate: 'clamp'
        })
      }];
      const buttonStyle = [StyleSheet.absoluteFill, {
        width,
        transform
      }, isLeftButtons ? leftButtonContainerStyle : rightButtonContainerStyle];
      return /*#__PURE__*/React.createElement(Animated.View, {
        key: index,
        style: buttonStyle
      }, buttonContent);
    });
  }
  render() {
    const {
      children,
      contentContainerStyle,
      leftButtons,
      leftContainerStyle,
      leftContent,
      rightButtons,
      rightContainerStyle,
      rightContent,
      style,
      ...props
    } = this.props;
    const {
      pan,
      width
    } = this.state;
    const canSwipeLeft = this._canSwipeLeft();
    const canSwipeRight = this._canSwipeRight();
    const transform = [{
      translateX: pan.x.interpolate({
        inputRange: [canSwipeLeft ? -width : 0, canSwipeRight ? width : 0],
        outputRange: [canSwipeLeft ? -width + StyleSheet.hairlineWidth : 0, canSwipeRight ? width - StyleSheet.hairlineWidth : 0],
        extrapolate: 'clamp'
      })
    }];
    return /*#__PURE__*/React.createElement(View, _extends({
      onLayout: this._handleLayout,
      style: [styles.container, style]
    }, this._panResponder.panHandlers, props), canSwipeRight && /*#__PURE__*/React.createElement(Animated.View, {
      style: [{
        transform,
        marginLeft: -width,
        width
      }, leftContainerStyle]
    }, leftContent || this._renderButtons(leftButtons, true)), /*#__PURE__*/React.createElement(Animated.View, {
      style: [{
        transform
      }, styles.content, contentContainerStyle]
    }, children), canSwipeLeft && /*#__PURE__*/React.createElement(Animated.View, {
      style: [{
        transform,
        marginRight: -width,
        width
      }, rightContainerStyle]
    }, rightContent || this._renderButtons(rightButtons, false)));
  }
}
_defineProperty(Swipeable, "propTypes", {
  // elements
  children: PropTypes.any,
  leftContent: PropTypes.any,
  rightContent: PropTypes.any,
  leftButtons: PropTypes.array,
  rightButtons: PropTypes.array,
  // left action lifecycle
  onLeftActionActivate: PropTypes.func,
  onLeftActionDeactivate: PropTypes.func,
  onLeftActionRelease: PropTypes.func,
  onLeftActionComplete: PropTypes.func,
  leftActionActivationDistance: PropTypes.number,
  leftActionReleaseAnimationFn: PropTypes.func,
  leftActionReleaseAnimationConfig: PropTypes.object,
  // right action lifecycle
  onRightActionActivate: PropTypes.func,
  onRightActionDeactivate: PropTypes.func,
  onRightActionRelease: PropTypes.func,
  onRightActionComplete: PropTypes.func,
  rightActionActivationDistance: PropTypes.number,
  rightActionReleaseAnimationFn: PropTypes.func,
  rightActionReleaseAnimationConfig: PropTypes.object,
  // left buttons lifecycle
  onLeftButtonsActivate: PropTypes.func,
  onLeftButtonsDeactivate: PropTypes.func,
  onLeftButtonsOpenRelease: PropTypes.func,
  onLeftButtonsOpenComplete: PropTypes.func,
  onLeftButtonsCloseRelease: PropTypes.func,
  onLeftButtonsCloseComplete: PropTypes.func,
  leftButtonWidth: PropTypes.number,
  leftButtonsActivationDistance: PropTypes.number,
  leftButtonsOpenReleaseAnimationFn: PropTypes.func,
  leftButtonsOpenReleaseAnimationConfig: PropTypes.object,
  leftButtonsCloseReleaseAnimationFn: PropTypes.func,
  leftButtonsCloseReleaseAnimationConfig: PropTypes.object,
  // right buttons lifecycle
  onRightButtonsActivate: PropTypes.func,
  onRightButtonsDeactivate: PropTypes.func,
  onRightButtonsOpenRelease: PropTypes.func,
  onRightButtonsOpenComplete: PropTypes.func,
  onRightButtonsCloseRelease: PropTypes.func,
  onRightButtonsCloseComplete: PropTypes.func,
  rightButtonWidth: PropTypes.number,
  rightButtonsActivationDistance: PropTypes.number,
  rightButtonsOpenReleaseAnimationFn: PropTypes.func,
  rightButtonsOpenReleaseAnimationConfig: PropTypes.object,
  rightButtonsCloseReleaseAnimationFn: PropTypes.func,
  rightButtonsCloseReleaseAnimationConfig: PropTypes.object,
  // base swipe lifecycle
  onSwipeStart: PropTypes.func,
  onSwipeMove: PropTypes.func,
  onSwipeRelease: PropTypes.func,
  onSwipeComplete: PropTypes.func,
  swipeReleaseAnimationFn: PropTypes.func,
  swipeReleaseAnimationConfig: PropTypes.object,
  // misc
  onRef: PropTypes.func,
  onPanAnimatedValueRef: PropTypes.func,
  swipeStartMinDistance: PropTypes.number,
  // styles
  style: ViewPropTypes.style,
  leftContainerStyle: ViewPropTypes.style,
  leftButtonContainerStyle: ViewPropTypes.style,
  rightContainerStyle: ViewPropTypes.style,
  rightButtonContainerStyle: ViewPropTypes.style,
  contentContainerStyle: ViewPropTypes.style
});
_defineProperty(Swipeable, "defaultProps", {
  leftContent: null,
  rightContent: null,
  leftButtons: null,
  rightButtons: null,
  // left action lifecycle
  onLeftActionActivate: noop,
  onLeftActionDeactivate: noop,
  onLeftActionRelease: noop,
  onLeftActionComplete: noop,
  leftActionActivationDistance: 20,
  leftActionReleaseAnimationFn: null,
  leftActionReleaseAnimationConfig: null,
  // right action lifecycle
  onRightActionActivate: noop,
  onRightActionDeactivate: noop,
  onRightActionRelease: noop,
  onRightActionComplete: noop,
  rightActionActivationDistance: 20,
  rightActionReleaseAnimationFn: null,
  rightActionReleaseAnimationConfig: null,
  // left buttons lifecycle
  onLeftButtonsActivate: noop,
  onLeftButtonsDeactivate: noop,
  onLeftButtonsOpenRelease: noop,
  onLeftButtonsOpenComplete: noop,
  onLeftButtonsCloseRelease: noop,
  onLeftButtonsCloseComplete: noop,
  leftButtonWidth: 20,
  leftButtonsActivationDistance: 20,
  leftButtonsOpenReleaseAnimationFn: null,
  leftButtonsOpenReleaseAnimationConfig: null,
  leftButtonsCloseReleaseAnimationFn: null,
  leftButtonsCloseReleaseAnimationConfig: null,
  // right buttons lifecycle
  onRightButtonsActivate: noop,
  onRightButtonsDeactivate: noop,
  onRightButtonsOpenRelease: noop,
  onRightButtonsOpenComplete: noop,
  onRightButtonsCloseRelease: noop,
  onRightButtonsCloseComplete: noop,
  rightButtonWidth: 20,
  rightButtonsActivationDistance: 20,
  rightButtonsOpenReleaseAnimationFn: null,
  rightButtonsOpenReleaseAnimationConfig: null,
  rightButtonsCloseReleaseAnimationFn: null,
  rightButtonsCloseReleaseAnimationConfig: null,
  // base swipe lifecycle
  onSwipeStart: noop,
  onSwipeMove: noop,
  onSwipeRelease: noop,
  onSwipeComplete: noop,
  swipeReleaseAnimationFn: Animated.timing,
  swipeReleaseAnimationConfig: {
    toValue: {
      x: 0,
      y: 0
    },
    duration: 200,
    easing: Easing.elastic(0.3),
    useNativeDriver: false
  },
  // misc
  onRef: noop,
  onPanAnimatedValueRef: noop,
  swipeStartMinDistance: 20
});
const styles = StyleSheet.create({
  container: {
    flexDirection: 'row'
  },
  content: {
    flex: 1
  }
});
//# sourceMappingURL=index.js.map