"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _instance = _interopRequireDefault(require("./instance"));

var _Button = _interopRequireDefault(require("./../Button"));

var _excluded = ["list", "onClick", "onShowedLeft", "onShowedRight"];

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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

// 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref
var ListPull = (0, _react.forwardRef)(function (_ref, ref) {
  var list = _ref.list,
      onClick = _ref.onClick,
      onShowedLeft = _ref.onShowedLeft,
      onShowedRight = _ref.onShowedRight,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return rootRef.current;
  });
  var instance = (0, _react.useRef)(null); // 更新句柄, 防止synchronization模式, 每次组件在render的时候都生成上次render的state、function、effects

  if (instance.current) {
    instance.current.params.onClick = handleClick;
  } // useEffect(() => {
  //   instance.current.params.onClick = onClick;
  // }, [onClick]) // eslint-disable-line


  (0, _react.useEffect)(function () {
    if (!rootRef || !rootRef.current) return;
    instance.current = new _instance["default"](rootRef.current, {
      onClick: handleClick,
      onShowedLeft: onShowedLeft,
      onShowedRight: onShowedRight
    });
  }, []); // eslint-disable-line

  function handleClick(s) {
    var e = s.event;
    var index = s.target.closest('.list-pull-li').getAttribute('data-index');
    var option = list[index];
    var item = null;
    var i = e.target.getAttribute('data-i');
    var direction = e.target.getAttribute('data-direction');

    if (i && direction) {
      if (direction === 'left') {
        item = option.lButtons[i];
      } else {
        item = option.rButtons[i];
      }
    } else {
      item = option.container;
    }

    if (onClick) onClick(s, item, index, option);
  }

  return /*#__PURE__*/_react["default"].createElement("ul", {
    ref: rootRef,
    className: "list-pull" + (others.className ? ' ' + others.className : '')
  }, list.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("li", {
      key: "button" + index,
      "data-index": "" + index,
      className: "border-b list-pull-li"
    }, item.lButtons && item.lButtons.length && /*#__PURE__*/_react["default"].createElement("div", {
      className: "list-pull-left"
    }, item.lButtons.map(function (button, i) {
      return /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        key: "button" + i,
        "data-index": "" + index,
        "data-i": "" + i,
        "data-direction": "left",
        className: "list-pull-button" + (button.className ? ' ' + button.className : ''),
        style: button.style
      }, button.caption);
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "list-pull-handler",
      "data-index": "" + index
    }, item.container), item.rButtons && item.rButtons.length && /*#__PURE__*/_react["default"].createElement("div", {
      className: "list-pull-right"
    }, item.rButtons.map(function (button, i) {
      return /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        key: "button" + i,
        "data-index": "" + index,
        "data-i": "" + i,
        "data-direction": "right",
        className: "list-pull-button" + (button.className ? ' ' + button.className : ''),
        style: button.style
      }, button.caption);
    })));
  }));
});
var _default = ListPull;
exports["default"] = _default;
module.exports = exports.default;