import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
export var scrollInfiniteFun = function scrollInfiniteFun(infiniteHasMore, scrollInfinite, onPopupScroll) {
  return /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(event) {
      var parentNode, currentNode, currentMenuIndex, offsetHeight, scrollTop, scrollHeight, isBottom, res;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              parentNode = event.currentTarget;
              currentNode = event.target;
              currentMenuIndex = Array.from(parentNode.childNodes).indexOf(event.target);
              onPopupScroll === null || onPopupScroll === void 0 ? void 0 : onPopupScroll(event, currentNode, currentMenuIndex);
              offsetHeight = currentNode.offsetHeight, scrollTop = currentNode.scrollTop, scrollHeight = currentNode.scrollHeight; // console.log('offsetHeight + scrollTop >= scrollHeight...', offsetHeight, scrollTop, scrollHeight)
              // const isBottom = offsetHeight + scrollTop >= scrollHeight - 20;

              isBottom = offsetHeight + scrollTop >= scrollHeight - 1;

              if (!(isBottom && infiniteHasMore && scrollInfinite)) {
                _context.next = 11;
                break;
              }

              _context.next = 9;
              return scrollInfinite(event, currentMenuIndex);

            case 9:
              res = _context.sent;
              return _context.abrupt("return", res);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }();
}; // // 避免影响已有select业务，重新增加一个casccader
// export const scrollInfiniteFunCascader = (
//   infiniteHasMore: boolean,
//   scrollInfinite: any,
//   onPopupScroll: any,
// ) => {
//   return async (event: any): Promise<void> => {
//     onPopupScroll?.(event);
//     const parentNode = event.currentTarget;
//     const currentNode = event.target;
//     const currentMenuIndex = Array.from(parentNode.childNodes).indexOf(event.target)
//     // console.log(Array.from(res).indexOf(event.target), '------res'); // szp
//     const { offsetHeight, scrollTop, scrollHeight } = currentNode as HTMLDivElement;
//     const isBottom = offsetHeight + scrollTop >= scrollHeight - 20;
//     if (isBottom && infiniteHasMore && scrollInfinite) {
//       // setLoading(true);
//       const res = await (scrollInfinite as any)(event, currentMenuIndex);
//       // setLoading(false);
//       // return res;
//     }
//   };
// };
// export default scrollInfiniteFun;