"use strict";

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

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

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

var _excluded = ["params", "wrapperAttribute", "paginationAttribute", "prevAttribute", "nextAttribute", "children", "containerChildren"];

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 _extends() { _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; }; return _extends.apply(this, arguments); }

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

var Swiper = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$params = _ref.params,
      params = _ref$params === void 0 ? {} : _ref$params,
      _ref$wrapperAttribute = _ref.wrapperAttribute,
      wrapperAttribute = _ref$wrapperAttribute === void 0 ? {} : _ref$wrapperAttribute,
      _ref$paginationAttrib = _ref.paginationAttribute,
      paginationAttribute = _ref$paginationAttrib === void 0 ? {} : _ref$paginationAttrib,
      _ref$prevAttribute = _ref.prevAttribute,
      prevAttribute = _ref$prevAttribute === void 0 ? {} : _ref$prevAttribute,
      _ref$nextAttribute = _ref.nextAttribute,
      nextAttribute = _ref$nextAttribute === void 0 ? {} : _ref$nextAttribute,
      children = _ref.children,
      containerChildren = _ref.containerChildren,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  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 _instance["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 /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: refEl
  }, others, {
    className: "swiper-container" + (others.className ? ' ' + others.className : '')
  }), /*#__PURE__*/_react["default"].createElement("div", _extends({}, wrapperAttribute, {
    className: "swiper-wrapper" + (wrapperAttribute.className ? ' ' + wrapperAttribute.className : '')
  }), children), params.pagination && /*#__PURE__*/_react["default"].createElement("div", _extends({}, paginationAttribute, {
    className: "swiper-pagination" + (paginationAttribute.className ? ' ' + paginationAttribute.className : '')
  })), params.navigation && params.navigation.prevEl && /*#__PURE__*/_react["default"].createElement("div", _extends({}, prevAttribute, {
    className: "swiper-button-prev" + (prevAttribute.className ? ' ' + prevAttribute.className : '')
  })), params.navigation && params.navigation.nextEl && /*#__PURE__*/_react["default"].createElement("div", _extends({}, nextAttribute, {
    className: "swiper-button-next" + (nextAttribute.className ? ' ' + nextAttribute.className : '')
  })), containerChildren);
});
var _default = Swiper;
exports["default"] = _default;
module.exports = exports.default;