"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _classnames = _interopRequireDefault(require("classnames"));

require("./index.module.less");

var _configProvider = require("../config-provider");

var _utils = require("../_utils/utils");

var _createSelectorQuery = _interopRequireDefault(require("../create-selector-query"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

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

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

  var _useConfig = (0, _configProvider.useConfig)(),
      brandPrimary = _useConfig.brandPrimary,
      _useConfig$shadowColo = _useConfig.shadowColor,
      shadowColor = _useConfig$shadowColo === void 0 ? brandPrimary : _useConfig$shadowColo;

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

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

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

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

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

  var swipeId = (0, _react.useMemo)(function () {
    return (0, _utils.guid)();
  }, []);

  var handleActionsWidth = function handleActionsWidth(id) {
    (0, _createSelectorQuery.default)().select(".swipe_action_actions_".concat(id)).boundingClientRect().exec(function (rect) {
      var _rect$;

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

  (0, _react.useEffect)(function () {
    handleActionsWidth(swipeId);
  }, []);
  (0, _react.useEffect)(function () {
    var width = Number("-".concat(actionsWidth));
    setX(open ? width : 0);
    setOrigin(open ? width : 0);
    setTransition('all 0.3s');
  }, [open]);
  var throttleTouchMove = (0, _utils.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);
  }, 30, {
    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) {
    // @ts-ignore
    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 _item$onTap;

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

  return /*#__PURE__*/_react.default.createElement(_one.View, {
    style: style,
    className: (0, _classnames.default)(className, 'swipe_action')
  }, /*#__PURE__*/_react.default.createElement(_one.View, {
    className: 'swipe_action_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.default.createElement(_one.View, {
    className: (0, _classnames.default)((_classNames = {}, _defineProperty(_classNames, 'swipe_action_actions', true), _defineProperty(_classNames, "swipe_action_actions_".concat(swipeId), true), _classNames))
  }, options === null || options === void 0 ? void 0 : options.map(function (item, index) {
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      key: index,
      className: "swipe_action_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);
  }))));
};

var _default = SwipeAction;
exports.default = _default;