var _excluded = ["thead", "tbody", "frozenThead", "frozenLeft", "frozenRight", "onBottomRefresh", "children"];

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

import React, { forwardRef, useRef, useImperativeHandle, useEffect, useState } from 'react';
import useSyncCallback from './../useSyncCallback';
import Instance from './instance.js';
var updateSuccess = null; // 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref

var FixTable = forwardRef(function (_ref, ref) {
  var thead = _ref.thead,
      tbody = _ref.tbody,
      _ref$frozenThead = _ref.frozenThead,
      frozenThead = _ref$frozenThead === void 0 ? false : _ref$frozenThead,
      _ref$frozenLeft = _ref.frozenLeft,
      frozenLeft = _ref$frozenLeft === void 0 ? [] : _ref$frozenLeft,
      _ref$frozenRight = _ref.frozenRight,
      frozenRight = _ref$frozenRight === void 0 ? [] : _ref$frozenRight,
      onBottomRefresh = _ref.onBottomRefresh,
      children = _ref.children,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useState = useState(null),
      forceUpdate = _useState[0],
      setForceUpdate = _useState[1];

  var rootRef = useRef(null);
  var instance = useRef(null);
  useImperativeHandle(ref, function () {
    return rootRef.current;
  }); // 更新容器

  useEffect(function () {
    if (forceUpdate === 1 || !rootRef || !rootRef.current) return; // console.log('更新容器', rootRef.current)
    // rootRef.current.scrollTop = 0
    // rootRef.current.scrollLeft = 0
    // 更新容器尺寸

    Instance.updateContainerSize(rootRef.current, frozenLeft, frozenRight); // 滚动修改左右滚动样式, 和底部加载

    Instance.onScroll(rootRef.current, onBottomRefresh);
  }, [forceUpdate]); // eslint-disable-line
  // 用显隐强制更新

  function handleUpdateRemove(success) {
    updateSuccess = success;
    setForceUpdate(1);
    handleUpdateRecovery();
  }

  var handleUpdateRecovery = useSyncCallback(function () {
    setForceUpdate(0);
    handleUpdateComplete();
  });
  var handleUpdateComplete = useSyncCallback(function () {
    if (typeof updateSuccess === 'function') updateSuccess(instance.current);
  }); // 构建instance

  if (rootRef.current) {
    instance.current = {
      container: rootRef.current,
      update: handleUpdateRemove
    };
    rootRef.current.instance = instance;
  } // 滚动修改左右滚动样式, 和底部加载


  function handleScroll(e) {
    Instance.onScroll(e.currentTarget, onBottomRefresh);
  }

  return /*#__PURE__*/React.createElement("div", _extends({
    ref: rootRef
  }, others, {
    className: 'fixtable' + (others.className ? ' ' + others.className : ''),
    onScroll: handleScroll
  }), !tbody || forceUpdate === 1 ? null : /*#__PURE__*/React.createElement(React.Fragment, null, frozenThead && /*#__PURE__*/React.createElement("table", {
    className: "fixtable-thead"
  }, thead, tbody), /*#__PURE__*/React.createElement("table", {
    className: "fixtable-tbody" + (frozenThead ? ' fixed' : '')
  }, thead, tbody), children));
});
export default FixTable;