"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _tslib() {
  const data = require("tslib");

  _tslib = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

var _loading = _interopRequireDefault(require("../loading"));

var _useRect = require("../hooks/use-rect");

var _useScrollParent = _interopRequireDefault(require("../hooks/use-scroll-parent"));

var _useEventListener = _interopRequireDefault(require("../hooks/use-event-listener"));

var _utils = require("../utils");

var _hooks = require("../hooks");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

const List = (0, _react().forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace,
    locale
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('list', prefixCls);
  const [state, updateState] = (0, _hooks.useSetState)({
    loading: props.loading,
    error: props.error
  });
  const root = (0, _react().useRef)();
  const scrollParent = (0, _react().useRef)(null);
  const placeholder = (0, _react().useRef)();
  scrollParent.current = (0, _useScrollParent.default)(root); // 判断是否需要加载

  const check = () => (0, _tslib().__awaiter)(void 0, void 0, void 0, function* () {
    if (!props.onLoad) return;

    if (state.loading || props.finished || state.error) {
      return;
    }

    const {
      offset,
      direction
    } = props;
    const scrollParentRect = (0, _useRect.getRect)(scrollParent.current);

    if (!scrollParentRect.height || (0, _utils.isHidden)(root.current)) {
      return;
    }

    let isReachEdge = false;
    const placeholderRect = (0, _useRect.getRect)(placeholder.current);

    if (direction === 'up') {
      isReachEdge = scrollParentRect.top - placeholderRect.top <= offset;
    } else {
      isReachEdge = placeholderRect.bottom - scrollParentRect.bottom <= offset;
    }

    if (isReachEdge) {
      try {
        updateState({
          loading: true
        });
        if (props.onLoad) yield props.onLoad();
        updateState({
          loading: false
        });
      } catch (error) {
        // eslint-disable-next-line no-console
        console.warn('onLoad error:', error);
        updateState({
          loading: false,
          error: true
        });
      }
    }
  });

  const renderFinishedText = () => {
    if (props.finished && props.finishedText) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('finished-text'))
      }, {
        children: props.finishedText
      }));
    }

    return null;
  };

  const clickErrorText = () => {
    updateState({
      error: false
    });
    check();
  };

  const renderErrorText = () => {
    if (state.error && props.errorText) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('error-text')),
        onClick: clickErrorText
      }, {
        children: props.errorText
      }));
    }

    return null;
  };

  const renderLoading = () => {
    if (state.loading && !props.finished) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('loading'))
      }, {
        children: (0, _react().isValidElement)(props.loadingText) ? props.loadingText : (0, _jsxRuntime().jsx)(_loading.default, Object.assign({
          className: (0, _clsx().default)(bem('loading-icon')),
          size: 16
        }, {
          children: props.loadingText || locale.loading
        }))
      }));
    }

    return null;
  };

  (0, _hooks.useUpdateEffect)(() => {
    if (props.autoCheck) {
      check();
    }
  }, [state.loading, props.finished, props.error]);
  (0, _hooks.useUpdateEffect)(() => {
    updateState({
      loading: props.loading,
      error: props.error
    });
  }, [props.loading, props.error]);
  (0, _hooks.useUpdateEffect)(() => {
    if (scrollParent.current && props.immediateCheck) {
      check();
    }
  }, [scrollParent.current]);
  (0, _useEventListener.default)('scroll', check, {
    target: scrollParent.current,
    depends: [state.loading, props.finished, state.error]
  });
  (0, _react().useImperativeHandle)(ref, () => ({
    check,
    state
  }));
  const Placeholder = (0, _jsxRuntime().jsx)("div", {
    ref: placeholder,
    className: (0, _clsx().default)(bem('placeholder'))
  });
  return (0, _jsxRuntime().jsxs)("div", Object.assign({
    ref: root,
    role: "feed",
    className: (0, _clsx().default)(bem(), props.className),
    style: props.style,
    "aria-busy": state.loading
  }, {
    children: [props.direction === 'down' ? props.children : Placeholder, renderLoading(), renderFinishedText(), renderErrorText(), props.direction === 'up' ? props.children : Placeholder]
  }));
});
List.defaultProps = {
  offset: 300,
  direction: 'down',
  immediateCheck: true,
  autoCheck: true
};
var _default = List;
exports.default = _default;