var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import React, { useRef, forwardRef, useImperativeHandle, isValidElement, useContext } from "react";
import classnames from "classnames";
import Loading from "../loading";
import { getRect } from "../hooks/get-rect";
import useScrollParent from "../hooks/use-scroll-parent";
import useEventListener from "../hooks/use-event-listener";
import { isHidden } from "../utils";
import { useSetState, useUpdateEffect } from "../hooks";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const List = /* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("list", prefixCls);
  const [state, updateState] = useSetState({
    loading: props.loading,
    error: props.error
  });
  const errorRecent = useRef(props.error);
  const root = useRef();
  const scrollParent = useRef(null);
  const placeholder = useRef();
  scrollParent.current = useScrollParent(root);
  const check = () => __async(void 0, null, function* () {
    if (!props.onLoad)
      return;
    if (state.loading || props.finished || errorRecent.current) {
      return;
    }
    const {
      offset,
      direction
    } = props;
    const scrollParentRect = getRect(scrollParent.current);
    if (!scrollParentRect.height || isHidden(root.current)) {
      return;
    }
    let isReachEdge = false;
    const placeholderRect = 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) {
        console.warn("onLoad error:", error);
        updateState({
          loading: false,
          error: true
        });
        errorRecent.current = true;
      }
    }
  });
  const renderFinishedText = () => {
    if (props.finished && props.finishedText) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("finished-text"))
      }, props.finishedText);
    }
    return null;
  };
  const clickErrorText = () => {
    updateState({
      error: false
    });
    errorRecent.current = false;
    check();
  };
  const renderErrorText = () => {
    if (state.error && props.errorText) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("error-text")),
        onClick: clickErrorText
      }, props.errorText);
    }
    return null;
  };
  const renderLoading = () => {
    if (state.loading && !props.finished) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("loading"))
      }, /* @__PURE__ */ isValidElement(props.loadingText) ? props.loadingText : /* @__PURE__ */ React.createElement(Loading, {
        className: classnames(bem("loading-icon")),
        size: 16
      }, props.loadingText));
    }
    return null;
  };
  useUpdateEffect(() => {
    if (props.autoCheck) {
      check();
    }
  }, [state.loading, props.finished, props.error]);
  useUpdateEffect(() => {
    updateState({
      loading: props.loading,
      error: props.error
    });
    errorRecent.current = props.error;
  }, [props.loading, props.error]);
  useUpdateEffect(() => {
    if (scrollParent.current && props.immediateCheck) {
      check();
    }
  }, [scrollParent.current]);
  useEventListener("scroll", check, {
    target: scrollParent.current,
    depends: [state.loading, props.finished, state.error]
  });
  useImperativeHandle(ref, () => ({
    check,
    state
  }));
  const Placeholder = /* @__PURE__ */ React.createElement("div", {
    ref: placeholder,
    className: classnames(bem("placeholder"))
  });
  return /* @__PURE__ */ React.createElement("div", {
    ref: root,
    role: "feed",
    className: classnames(bem()),
    "aria-busy": state.loading
  }, 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,
  loadingText: "\u52A0\u8F7D\u4E2D...",
  finishedText: "\u6CA1\u6709\u66F4\u591A\u4E86"
};
var stdin_default = List;
export {
  stdin_default as default
};
