"use strict";

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

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

var _Container = _interopRequireDefault(require("./../Container"));

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

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

var _excluded = ["onTopRefresh", "onBottomRefresh", "refreshing", "children"];

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 ContainerPull = (0, _react.forwardRef)(function (_ref, ref) {
  var onTopRefresh = _ref.onTopRefresh,
      onBottomRefresh = _ref.onBottomRefresh,
      refreshing = _ref.refreshing,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  // context
  var context = (0, _react.useContext)(_instance2["default"]) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  };

  var rootRef = (0, _react.useRef)(null);
  var topContainerRef = (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.onTopRefresh = onTopRefresh; // 头部刷新,加载第一页

    instance.current.params.onBottomRefresh = bottomRefresh; // 底部刷新,加载下一页
  } // 初始化


  if (refreshing === 0) refreshing = false;else if (refreshing === 1) refreshing = true;
  (0, _react.useEffect)(function () {
    instance.current = new _instance["default"]({
      // threshold: 100,
      // end: 200, // 头部下拉的结束位置
      // endRefresh: null, // 滑动到指位置后自动刷新
      // moveTimeout: 0, // 滑动超时, 解决ios手指滑动到原生tabbar上, 不触发onTouchEnd
      container: rootRef.current,
      onScroll: others.onScroll ? others.onScroll : null,
      onTopRefresh: onTopRefresh,
      // 头部刷新,加载第一页
      onBottomRefresh: bottomRefresh,
      // 底部刷新,加载下一页
      // 构建实体
      topContainer: onTopRefresh ? topContainerRef.current : null,
      // 实体交互
      duration: 150,
      onPull: function onPull(e) {
        var topContainer = e.topContainer;
        topContainer.style.height = e.touches.currentPosY + 'px';
        var topIcon = topContainer.querySelector('.containerpull-pull-icon');
        var topCaption = topContainer.querySelector('.containerpull-pull-caption');

        if (!e.isLoading) {
          if (e.touches.currentPosY >= e.params.threshold) {
            if (topIcon) topIcon.classList.add('containerpull-pull-icon-down');
            if (topCaption) topCaption.innerHTML = locale('释放立即刷新', 'release');
          } else {
            if (topIcon) topIcon.classList.remove('containerpull-pull-icon-down');
            if (topCaption) topCaption.innerHTML = locale('下拉可以刷新', 'pull_down');
          }
        }
      },
      onShowTop: function onShowTop(e) {
        var topContainer = e.topContainer;
        var topIcon = topContainer.querySelector('.containerpull-pull-icon');
        var topCaption = topContainer.querySelector('.containerpull-pull-caption');
        topContainer.style.height = e.params.threshold + 'px';
        if (topIcon) topIcon.classList.remove('containerpull-pull-icon-down');
        if (topIcon) topIcon.classList.add('containerpull-pull-icon-loading');
        if (topCaption) topCaption.innerHTML = locale('正在刷新...', 'refreshing');
      },
      onHideTop: function onHideTop(e) {
        var topContainer = e.topContainer;
        topContainer.style.height = '0';
      },
      onTopHid: function onTopHid(e) {
        var topContainer = e.topContainer;
        var topIcon = topContainer.querySelector('.containerpull-pull-icon');
        if (topIcon) topIcon.classList.remove('containerpull-pull-icon-down');
        if (topIcon) topIcon.classList.remove('containerpull-pull-icon-loading');
      }
    });
  }, []); // eslint-disable-line
  // 刷新完成, 重置状态

  function refreshed() {
    instance.current.isLoading = false;

    if (instance.current.touches.posY) {
      // 如果头部展开, 则隐藏头部
      instance.current.hideTop(); // 隐藏头部
    }
  } // 底部刷新


  function bottomRefresh() {
    // 如果没有设置刷新状态refreshing, 则不刷新
    if (refreshing !== true && refreshing !== false) {
      refreshed();
      return;
    } // 正在刷新不触发底部刷新


    if (refreshing === true) return; // 刷新完成触发底部刷新

    if (refreshing === false) {
      onBottomRefresh();
    }
  } // 控制刷新


  var pullRefreshTimeout = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    if (!instance.current) return; // 如果没有设置刷新状态refreshing, 则不刷新

    if (refreshing !== true && refreshing !== false) {
      refreshed();
      return;
    } // 正在刷新


    if (refreshing === true) {
      instance.current.isLoading = true;
      return;
    } // 刷新完成


    if (refreshing === false) {
      // 刷新完成, 重置状态
      refreshed(); // 判断是否没有滚动条, 如果没有滚动条会再次触发onBottomRefresh方法

      if (pullRefreshTimeout.current) {
        window.clearTimeout(pullRefreshTimeout.current);
      } // 因为页面渲染需要时间, 所以需要等渲染完成后再判断有无滚动条


      pullRefreshTimeout.current = setTimeout(function () {
        if (!instance.current.hasScroll()) {
          console.log('刷新完成,但没有滚动条,继续加载...');
          bottomRefresh();
        } else if (instance.current.isBottom()) {
          console.log('刷新完成,滚动条在底部,继续加载...');
          bottomRefresh();
        }
      }, 500);
    }
  }, [refreshing]); // eslint-disable-line

  return /*#__PURE__*/_react["default"].createElement(_Container["default"], _extends({
    ref: rootRef
  }, others), /*#__PURE__*/_react["default"].createElement("div", {
    ref: topContainerRef,
    className: "SID-Dragrefresh-TopContainer containerpull-pull"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "containerpull-pull-box"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "containerpull-pull-icon"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "containerpull-pull-caption"
  }, locale('下拉可以刷新', 'pull_down')))), children);
});
var _default = ContainerPull;
exports["default"] = _default;
module.exports = exports.default;