"use strict";

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

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

var _useSyncCallback = _interopRequireDefault(require("./../useSyncCallback"));

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

var _excluded = ["thead", "tbody", "frozenThead", "frozenLeft", "frozenRight", "onBottomRefresh", "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 updateSuccess = null; // 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref

var FixTable = (0, _react.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 = (0, _react.useState)(null),
      forceUpdate = _useState[0],
      setForceUpdate = _useState[1];

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

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

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


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

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

  var handleUpdateRecovery = (0, _useSyncCallback["default"])(function () {
    setForceUpdate(0);
    handleUpdateComplete();
  });
  var handleUpdateComplete = (0, _useSyncCallback["default"])(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["default"].onScroll(e.currentTarget, onBottomRefresh);
  }

  return /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: rootRef
  }, others, {
    className: 'fixtable' + (others.className ? ' ' + others.className : ''),
    onScroll: handleScroll
  }), !tbody || forceUpdate === 1 ? null : /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("table", {
    className: "fixtable-table" + (frozenThead ? ' frozen-thead' : '')
  }, thead, tbody), children));
});
var _default = FixTable;
exports["default"] = _default;
module.exports = exports.default;