var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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());
  });
};
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_loading = __toESM(require("../loading"));
var import_get_rect = require("../hooks/get-rect");
var import_use_scroll_parent = __toESM(require("../hooks/use-scroll-parent"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_utils = require("../utils");
var import_hooks = require("../hooks");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
const List = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("list", prefixCls);
  const [state, updateState] = (0, import_hooks.useSetState)({
    loading: props.loading,
    error: props.error
  });
  const errorRecent = (0, import_react.useRef)(props.error);
  const root = (0, import_react.useRef)();
  const scrollParent = (0, import_react.useRef)(null);
  const placeholder = (0, import_react.useRef)();
  scrollParent.current = (0, import_use_scroll_parent.default)(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 = (0, import_get_rect.getRect)(scrollParent.current);
    if (!scrollParentRect.height || (0, import_utils.isHidden)(root.current)) {
      return;
    }
    let isReachEdge = false;
    const placeholderRect = (0, import_get_rect.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__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(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__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("error-text")),
        onClick: clickErrorText
      }, props.errorText);
    }
    return null;
  };
  const renderLoading = () => {
    if (state.loading && !props.finished) {
      return /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("loading"))
      }, /* @__PURE__ */ (0, import_react.isValidElement)(props.loadingText) ? props.loadingText : /* @__PURE__ */ import_react.default.createElement(import_loading.default, {
        className: (0, import_classnames.default)(bem("loading-icon")),
        size: 16
      }, props.loadingText));
    }
    return null;
  };
  (0, import_hooks.useUpdateEffect)(() => {
    if (props.autoCheck) {
      check();
    }
  }, [state.loading, props.finished, props.error]);
  (0, import_hooks.useUpdateEffect)(() => {
    updateState({
      loading: props.loading,
      error: props.error
    });
    errorRecent.current = props.error;
  }, [props.loading, props.error]);
  (0, import_hooks.useUpdateEffect)(() => {
    if (scrollParent.current && props.immediateCheck) {
      check();
    }
  }, [scrollParent.current]);
  (0, import_use_event_listener.default)("scroll", check, {
    target: scrollParent.current,
    depends: [state.loading, props.finished, state.error]
  });
  (0, import_react.useImperativeHandle)(ref, () => ({
    check,
    state
  }));
  const Placeholder = /* @__PURE__ */ import_react.default.createElement("div", {
    ref: placeholder,
    className: (0, import_classnames.default)(bem("placeholder"))
  });
  return /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    role: "feed",
    className: (0, import_classnames.default)(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;
