'use strict';

exports.__esModule = true;

var _extends = Object.assign || 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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _instance2 = _interopRequireDefault(_instance);

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; } // require (PrototypeArray.js), 使用了equals


var Swiper = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$params = _ref.params,
      params = _ref$params === undefined ? {} : _ref$params,
      _ref$wrapperAttribute = _ref.wrapperAttribute,
      wrapperAttribute = _ref$wrapperAttribute === undefined ? {} : _ref$wrapperAttribute,
      _ref$paginationAttrib = _ref.paginationAttribute,
      paginationAttribute = _ref$paginationAttrib === undefined ? {} : _ref$paginationAttrib,
      _ref$prevAttribute = _ref.prevAttribute,
      prevAttribute = _ref$prevAttribute === undefined ? {} : _ref$prevAttribute,
      _ref$nextAttribute = _ref.nextAttribute,
      nextAttribute = _ref$nextAttribute === undefined ? {} : _ref$nextAttribute,
      children = _ref.children,
      containerChildren = _ref.containerChildren,
      others = _objectWithoutProperties(_ref, ['params', 'wrapperAttribute', 'paginationAttribute', 'prevAttribute', 'nextAttribute', 'children', 'containerChildren']);

  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, _extends({}, params));
    refEl.current.instance = instance;
  }, []); // eslint-disable-line

  // 更新句柄, 防止synchronization模式, 每次组件在render的时候都生成上次render的state、function、effects
  if (instance.current) {
    instance.current.update();
    if (params.on) {
      for (var eventName in params.on) {
        instance.current.off(eventName);
        instance.current.on(eventName, params.on[eventName]);
      }
    }
  }

  return _react2.default.createElement(
    'div',
    _extends({ ref: refEl }, others, { className: 'swiper-container' + (others.className ? ' ' + others.className : '') }),
    _react2.default.createElement(
      'div',
      _extends({}, wrapperAttribute, { className: 'swiper-wrapper' + (wrapperAttribute.className ? ' ' + wrapperAttribute.className : '') }),
      children
    ),
    params.pagination && _react2.default.createElement('div', _extends({}, paginationAttribute, { className: 'swiper-pagination' + (paginationAttribute.className ? ' ' + paginationAttribute.className : '') })),
    params.navigation && params.navigation.prevEl && _react2.default.createElement('div', _extends({}, prevAttribute, { className: 'swiper-button-prev' + (prevAttribute.className ? ' ' + prevAttribute.className : '') })),
    params.navigation && params.navigation.nextEl && _react2.default.createElement('div', _extends({}, nextAttribute, { className: 'swiper-button-next' + (nextAttribute.className ? ' ' + nextAttribute.className : '') })),
    containerChildren
  );
});

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