"use strict";

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

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

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

var _excluded = ["list", "optionAttribute", "step", "duration", "autoPlay", "direction", "loop", "onClick"],
    _excluded2 = ["onClick"];

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 Marquee = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref$list = _ref.list,
      list = _ref$list === void 0 ? [] : _ref$list,
      _ref$optionAttribute = _ref.optionAttribute,
      optionAttribute = _ref$optionAttribute === void 0 ? {} : _ref$optionAttribute,
      _ref$step = _ref.step,
      step = _ref$step === void 0 ? 50 : _ref$step,
      _ref$duration = _ref.duration,
      duration = _ref$duration === void 0 ? 300 : _ref$duration,
      _ref$autoPlay = _ref.autoPlay,
      autoPlay = _ref$autoPlay === void 0 ? 2000 : _ref$autoPlay,
      _ref$direction = _ref.direction,
      direction = _ref$direction === void 0 ? 'top' : _ref$direction,
      _ref$loop = _ref.loop,
      loop = _ref$loop === void 0 ? true : _ref$loop,
      _onClick = _ref.onClick,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var rootRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return rootRef.current;
  });
  var instance = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if (instance.current || list.length === 0) return;
    initInstance();
  }, []);
  (0, _react.useEffect)(function () {
    console.log('更新');
    update();
  }, [list]);

  function update() {
    if (!instance.current) return;
    instance.current.updateParams({
      start: 0,
      end: step * (list.length - 1)
    });

    if (autoPlay) {
      instance.current.play();
    }
  }

  function initInstance() {
    instance.current = new _instance["default"](rootRef.current, {
      start: 0,
      end: step * (list.length - 1),
      step: step,
      duration: duration,
      delay: autoPlay,
      direction: direction,
      loop: loop
    });

    if (autoPlay) {
      instance.current.play();
    }

    rootRef.current.instance = instance;
  } // 过滤已经回调的属性


  function filterProps(props) {
    if (!props) return {};

    var onClick = props.onClick,
        otherProps = _objectWithoutPropertiesLoose(props, _excluded2);

    return _extends({}, otherProps);
  } // 剔除掉onClick事件, 因为在容器onClick已经回调了


  var otherOptionAttribute = filterProps(optionAttribute);
  return /*#__PURE__*/_react["default"].createElement("ul", _extends({
    ref: rootRef
  }, others, {
    className: "marquee" + (others.className ? ' ' + others.className : '')
  }), list && list.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("li", _extends({
      key: index
    }, otherOptionAttribute, {
      className: "marquee-li" + (otherOptionAttribute.className ? ' ' + otherOptionAttribute.className : ''),
      style: Object.assign({
        height: step + 'px'
      }, otherOptionAttribute.style || {}),
      onClick: function onClick(e) {
        if (_onClick) _onClick(e, item.name, [item], index);
      }
    }), item.name);
  }));
});
var _default = Marquee;
exports["default"] = _default;
module.exports = exports.default;