'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _instance = require('./instance');

var _instance2 = _interopRequireDefault(_instance);

var _Button = require('./../Button');

var _Button2 = _interopRequireDefault(_Button);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } 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 = _objectWithoutProperties(_ref, ['list', 'onClick', 'onShowedLeft', 'onShowedRight']);

  var refEl = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return refEl.current;
  });
  var instance = (0, _react.useRef)(null);

  (0, _react.useEffect)(function () {
    if (!refEl || !refEl.current) return;
    instance.current = new _instance2.default(refEl.current, {
      onClick: function onClick(s) {
        var e = s.event;
        var index = e.target.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];
          }
        }
        if (_onClick) _onClick(s, item, index, option);
      },
      onShowedLeft: onShowedLeft,
      onShowedRight: onShowedRight
    });
  }, []); // eslint-disable-line

  return _react2.default.createElement(
    'ul',
    { ref: refEl, className: 'list-pull' + (others.className ? ' ' + others.className : '') },
    list.map(function (item, index) {
      return _react2.default.createElement(
        'li',
        { key: 'button' + index, 'data-index': '' + index, className: 'border-b list-pull-li' },
        item.lButtons && item.lButtons.length && _react2.default.createElement(
          'div',
          { className: 'list-pull-left' },
          item.lButtons.map(function (button, i) {
            return _react2.default.createElement(
              _Button2.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
            );
          })
        ),
        _react2.default.createElement(
          'div',
          { className: 'list-pull-handler', 'data-index': '' + index },
          item.container
        ),
        item.rButtons && item.rButtons.length && _react2.default.createElement(
          'div',
          { className: 'list-pull-right' },
          item.rButtons.map(function (button, i) {
            return _react2.default.createElement(
              _Button2.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
            );
          })
        )
      );
    })
  );
});

exports.default = ListPull;
module.exports = exports['default'];