var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/salesSdk/hooks/useSyncOsModule.ts
var useSyncOsModule_exports = {};
__export(useSyncOsModule_exports, {
  useSyncOsModule: () => useSyncOsModule
});
module.exports = __toCommonJS(useSyncOsModule_exports);
var import_react = require("react");
function shallowEqual(a, b) {
  if (Object.is(a, b)) return true;
  if (typeof a !== "object" || a === null || typeof b !== "object" || b === null) {
    return false;
  }
  const ka = Object.keys(a);
  const kb = Object.keys(b);
  if (ka.length !== kb.length) return false;
  for (const k of ka) {
    if (!Object.prototype.hasOwnProperty.call(b, k)) return false;
    if (!Object.is(a[k], b[k])) return false;
  }
  return true;
}
function useSyncOsModule(module2, events, getSnapshot, isEqual = shallowEqual) {
  const lastSnapshotRef = (0, import_react.useRef)({
    value: void 0,
    initialized: false
  });
  const stableGetSnapshot = (0, import_react.useCallback)(() => {
    const next = getSnapshot();
    const cache = lastSnapshotRef.current;
    if (!cache.initialized) {
      cache.value = next;
      cache.initialized = true;
      return next;
    }
    if (isEqual(cache.value, next)) {
      return cache.value;
    }
    cache.value = next;
    return next;
  }, [isEqual]);
  const eventsKey = events.join("|");
  const subscribe = (0, import_react.useCallback)(
    (onStoreChange) => {
      if (!module2) return () => {
      };
      const offs = events.map((evt) => module2.effectsOn(evt, onStoreChange));
      return () => {
        for (const off of offs) {
          try {
            off();
          } catch {
          }
        }
      };
    },
    [module2, eventsKey]
  );
  return (0, import_react.useSyncExternalStore)(subscribe, stableGetSnapshot, stableGetSnapshot);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSyncOsModule
});
