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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/Selector/components/IntersectionObserver.tsx
var IntersectionObserver_exports = {};
__export(IntersectionObserver_exports, {
  IntersectionItem: () => IntersectionItem,
  IntersectionObserverContext: () => IntersectionObserverContext,
  IntersectionObserverProvider: () => IntersectionObserverProvider,
  default: () => IntersectionObserver_default,
  useIntersectionObserver: () => useIntersectionObserver
});
module.exports = __toCommonJS(IntersectionObserver_exports);
var import_react = __toESM(require("react"));
function useMemoizedFn(fn) {
  const fnRef = (0, import_react.useRef)(fn);
  const memoizedFn = (0, import_react.useRef)();
  fnRef.current = fn;
  if (!memoizedFn.current) {
    memoizedFn.current = (...args) => {
      return fnRef.current(...args);
    };
  }
  return memoizedFn.current;
}
var IntersectionObserverContext = (0, import_react.createContext)({
  initialized: false,
  registerElement: () => {
  },
  unregisterElement: () => {
  },
  registerElements: () => {
  },
  clearAll: () => {
  },
  firstVisibleIndex: -1
});
var IntersectionObserverProvider = ({
  children,
  root = null,
  rootMargin = "0px",
  threshold = 0,
  onFirstVisibleIndexChange,
  disabled = false
}) => {
  const [initialized, setInitialized] = (0, import_react.useState)(false);
  const [firstVisibleIndex, setFirstVisibleIndex] = (0, import_react.useState)(-1);
  const observerRef = (0, import_react.useRef)(null);
  const elementsMapRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
  const pendingRegistrationsRef = (0, import_react.useRef)([]);
  const firstVisibleIndexRef = (0, import_react.useRef)(-1);
  const onFirstVisibleIndexChangeRef = (0, import_react.useRef)(onFirstVisibleIndexChange);
  (0, import_react.useEffect)(() => {
    onFirstVisibleIndexChangeRef.current = onFirstVisibleIndexChange;
  });
  (0, import_react.useEffect)(() => {
    firstVisibleIndexRef.current = firstVisibleIndex;
  });
  const calculateFirstVisibleIndex = (0, import_react.useCallback)(() => {
    let minIndex = -1;
    elementsMapRef.current.forEach((elementInfo) => {
      if (elementInfo.isVisible) {
        if (minIndex === -1 || elementInfo.index < minIndex) {
          minIndex = elementInfo.index;
        }
      }
    });
    return minIndex;
  }, []);
  const updateFirstVisibleIndex = useMemoizedFn(() => {
    var _a;
    const newIndex = calculateFirstVisibleIndex();
    if (newIndex !== firstVisibleIndexRef.current) {
      setFirstVisibleIndex(newIndex);
      (_a = onFirstVisibleIndexChangeRef.current) == null ? void 0 : _a.call(onFirstVisibleIndexChangeRef, newIndex);
    }
  });
  const observerOptions = (0, import_react.useMemo)(
    () => ({
      root,
      rootMargin,
      threshold
    }),
    [root, rootMargin, threshold]
  );
  const createObserver = (0, import_react.useCallback)(() => {
    if (disabled) {
      return null;
    }
    if (typeof window === "undefined" || !window.IntersectionObserver) {
      console.warn("IntersectionObserver is not supported in this browser");
      return null;
    }
    return new IntersectionObserver((entries) => {
      let hasChanged = false;
      entries.forEach((entry) => {
        const elementInfo = elementsMapRef.current.get(entry.target);
        if (elementInfo) {
          const wasVisible = elementInfo.isVisible;
          const isVisible = entry.isIntersecting;
          if (wasVisible !== isVisible) {
            elementInfo.isVisible = isVisible;
            hasChanged = true;
          }
        }
      });
      if (hasChanged) {
        updateFirstVisibleIndex();
      }
    }, observerOptions);
  }, [observerOptions, disabled]);
  const doRegisterElement = (0, import_react.useCallback)((element, index) => {
    if (!observerRef.current || !element) {
      return false;
    }
    if (elementsMapRef.current.has(element)) {
      const elementInfo2 = elementsMapRef.current.get(element);
      if (elementInfo2.index !== index) {
        elementInfo2.index = index;
      }
      return true;
    }
    const elementInfo = {
      element,
      index,
      isVisible: false
    };
    elementsMapRef.current.set(element, elementInfo);
    observerRef.current.observe(element);
    return true;
  }, []);
  const processPendingRegistrations = useMemoizedFn(() => {
    const pending = pendingRegistrationsRef.current;
    pendingRegistrationsRef.current = [];
    pending.forEach(({ element, index }) => {
      doRegisterElement(element, index);
    });
  });
  const registerElement = (0, import_react.useCallback)((element, index) => {
    if (!element) {
      return;
    }
    if (observerRef.current) {
      doRegisterElement(element, index);
      return;
    }
    const existingIndex = pendingRegistrationsRef.current.findIndex(
      (item) => item.element === element
    );
    if (existingIndex === -1) {
      pendingRegistrationsRef.current.push({ element, index });
    } else {
      pendingRegistrationsRef.current[existingIndex].index = index;
    }
  }, [doRegisterElement]);
  const unregisterElement = (0, import_react.useCallback)((element) => {
    if (!element) {
      return;
    }
    const pendingIndex = pendingRegistrationsRef.current.findIndex(
      (item) => item.element === element
    );
    if (pendingIndex !== -1) {
      pendingRegistrationsRef.current.splice(pendingIndex, 1);
    }
    if (observerRef.current && elementsMapRef.current.has(element)) {
      elementsMapRef.current.delete(element);
      observerRef.current.unobserve(element);
      updateFirstVisibleIndex();
    }
  }, []);
  const registerElements = (0, import_react.useCallback)((elements) => {
    elements.forEach(({ element, index }) => {
      registerElement(element, index);
    });
  }, [registerElement]);
  const clearAll = (0, import_react.useCallback)(() => {
    pendingRegistrationsRef.current = [];
    if (observerRef.current) {
      elementsMapRef.current.forEach((_, element) => {
        observerRef.current.unobserve(element);
      });
    }
    elementsMapRef.current.clear();
    updateFirstVisibleIndex();
  }, []);
  (0, import_react.useEffect)(() => {
    const observer = createObserver();
    if (observer) {
      observerRef.current = observer;
      processPendingRegistrations();
      setInitialized(true);
    }
    return () => {
      if (observerRef.current) {
        observerRef.current.disconnect();
        observerRef.current = null;
      }
      elementsMapRef.current.clear();
      pendingRegistrationsRef.current = [];
      setInitialized(false);
      setFirstVisibleIndex(-1);
    };
  }, [createObserver]);
  const contextValue = (0, import_react.useMemo)(() => ({
    initialized,
    registerElement,
    unregisterElement,
    registerElements,
    clearAll,
    firstVisibleIndex
  }), [initialized, registerElement, unregisterElement, registerElements, clearAll, firstVisibleIndex]);
  return /* @__PURE__ */ import_react.default.createElement(IntersectionObserverContext.Provider, { value: contextValue }, children);
};
var useIntersectionObserver = () => {
  const context = (0, import_react.useContext)(IntersectionObserverContext);
  if (!context) {
    throw new Error("useIntersectionObserver must be used within IntersectionObserverProvider");
  }
  return context;
};
var IntersectionItem = ({
  index,
  children,
  className,
  style
}) => {
  const elementRef = (0, import_react.useRef)(null);
  const { initialized, registerElement, unregisterElement } = useIntersectionObserver();
  (0, import_react.useEffect)(() => {
    const element = elementRef.current;
    if (initialized && element) {
      registerElement(element, index);
      return () => {
        unregisterElement(element);
      };
    }
  }, [initialized, index, registerElement, unregisterElement]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ref: elementRef,
      className,
      style
    },
    children
  );
};
var IntersectionObserver_default = IntersectionObserverProvider;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  IntersectionItem,
  IntersectionObserverContext,
  IntersectionObserverProvider,
  useIntersectionObserver
});
