function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React, { useState, useEffect, useMemo } from 'react';
import { View } from 'remax/one';
import classNames from 'classnames';
import { createSelectorQuery } from '../one';
import { throttle, guid } from './../_utils';
import { getPrefixCls } from './../common';
var prefixCls = getPrefixCls('swipe');

var SwipeAction = function SwipeAction(props) {
  var _classNames2;

  var open = props.open,
      _props$options = props.options,
      options = _props$options === void 0 ? [] : _props$options,
      onTouchStart = props.onTouchStart,
      onOpened = props.onOpened,
      onClosed = props.onClosed,
      children = props.children;

  var _useState = useState(0),
      _useState2 = _slicedToArray(_useState, 2),
      x = _useState2[0],
      setX = _useState2[1];

  var _useState3 = useState(0),
      _useState4 = _slicedToArray(_useState3, 2),
      origin = _useState4[0],
      setOrigin = _useState4[1];

  var _useState5 = useState('none'),
      _useState6 = _slicedToArray(_useState5, 2),
      transition = _useState6[0],
      setTransition = _useState6[1];

  var _useState7 = useState(0),
      _useState8 = _slicedToArray(_useState7, 2),
      startX = _useState8[0],
      setStartX = _useState8[1];

  var _useState9 = useState(0),
      _useState10 = _slicedToArray(_useState9, 2),
      actionsWidth = _useState10[0],
      setActionsWidth = _useState10[1];

  var swipeId = useMemo(function () {
    return guid();
  }, []);

  var handleActionsWidth = function handleActionsWidth(id) {
    createSelectorQuery() // @ts-ignore
    .select(".".concat(prefixCls, "-actions-").concat(id)).boundingClientRect().exec(function (rect) {
      var _a;

      var w = Array.isArray(rect) ? (_a = rect === null || rect === void 0 ? void 0 : rect[0]) === null || _a === void 0 ? void 0 : _a.width : rect === null || rect === void 0 ? void 0 : rect.width;
      setActionsWidth(w);
    });
  };

  useEffect(function () {
    handleActionsWidth(swipeId);
  }, []);
  useEffect(function () {
    var width = Number("-".concat(actionsWidth));
    setX(open ? width : 0);
    setOrigin(open ? width : 0);
    setTransition('all 0.3s');
  }, [open]);
  var throttleTouchMove = throttle(function (e) {
    var touch = e.touches[0];
    var currenrX = touch.pageX;
    var diff = currenrX - startX;
    var total = diff + origin;

    if (total >= 0) {
      return;
    }

    setX(total);
  }, 90, {
    trailing: false
  });

  var handleTouchStart = function handleTouchStart(e) {
    // touch当前行时关闭其他行
    onTouchStart === null || onTouchStart === void 0 ? void 0 : onTouchStart(e);
    var touch = e.touches[0];
    setStartX(touch.pageX);
    setTransition('none');
  };

  var handleTouchMove = function handleTouchMove(e) {
    throttleTouchMove(e);
  };

  var handleTouchEnd = function handleTouchEnd(e) {
    var width = Number("-".concat(actionsWidth));

    if (x >= 0) {
      return;
    }

    if (x > origin) {
      onClosed === null || onClosed === void 0 ? void 0 : onClosed(e);
      setX(0);
      setOrigin(0);
      setTransition('all 0.3s');
      return;
    }

    var x2 = 0;
    var origin2 = 0;

    if (x <= -30) {
      x2 = width;
      origin2 = width;
      onOpened === null || onOpened === void 0 ? void 0 : onOpened(e);
    }

    if (x > -30) {
      x2 = 0;
      origin2 = 0;
      onClosed === null || onClosed === void 0 ? void 0 : onClosed(e);
    }

    setX(x2);
    setOrigin(origin2);
    setTransition('all 0.3s');
  };

  var handleTouchCancel = function handleTouchCancel() {
    var width = Number("-".concat(actionsWidth));
    setX(width);
    setOrigin(width);
  };

  var handleTap = function handleTap(item) {
    var _a;

    (_a = item === null || item === void 0 ? void 0 : item.onTap) === null || _a === void 0 ? void 0 : _a.call(item, item);
  };

  return /*#__PURE__*/React.createElement(View, {
    className: classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-class"), true))
  }, /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-container"),
    onTouchStart: handleTouchStart,
    onTouchMove: handleTouchMove,
    onTouchEnd: handleTouchEnd,
    onTouchCancel: handleTouchCancel,
    style: {
      transform: "translate3d(".concat(x, "PX, 0, 0)"),
      // left: `${x}PX`,
      transition: transition
    }
  }, children, /*#__PURE__*/React.createElement(View, {
    className: classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-actions"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-actions-").concat(swipeId), true), _classNames2))
  }, options === null || options === void 0 ? void 0 : options.map(function (item, index) {
    return /*#__PURE__*/React.createElement(View, {
      key: index,
      className: "".concat(prefixCls, "-action"),
      onTap: function onTap() {
        return handleTap(item);
      },
      style: item === null || item === void 0 ? void 0 : item.style
    }, item === null || item === void 0 ? void 0 : item.name);
  }))));
};

export default SwipeAction;