const ORGANIZATION_SCOPE_CHANGED_EVENT = "om:organization-scope-changed";
let currentScope = {
  organizationId: null,
  tenantId: null
};
let currentVersion = 0;
let hasEmitted = false;
function getCurrentOrganizationScope() {
  return { ...currentScope };
}
function getCurrentOrganizationScopeVersion() {
  return currentVersion;
}
function emitOrganizationScopeChanged(detail) {
  if (typeof window === "undefined" || typeof CustomEvent === "undefined") return;
  const hasChanged = currentScope.organizationId !== detail.organizationId || currentScope.tenantId !== detail.tenantId;
  const isFirstEmit = !hasEmitted;
  currentScope = { ...detail };
  hasEmitted = true;
  if (hasChanged) {
    currentVersion++;
  }
  if (hasChanged || isFirstEmit) {
    window.dispatchEvent(new CustomEvent(ORGANIZATION_SCOPE_CHANGED_EVENT, { detail }));
  }
}
function subscribeOrganizationScopeChanged(handler) {
  if (typeof window === "undefined") return () => {
  };
  const listener = (event) => {
    const detail = event.detail ?? { organizationId: null, tenantId: null };
    handler(detail);
  };
  window.addEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener);
  return () => {
    window.removeEventListener(ORGANIZATION_SCOPE_CHANGED_EVENT, listener);
  };
}
export {
  ORGANIZATION_SCOPE_CHANGED_EVENT,
  emitOrganizationScopeChanged,
  getCurrentOrganizationScope,
  getCurrentOrganizationScopeVersion,
  subscribeOrganizationScopeChanged
};
//# sourceMappingURL=organizationEvents.js.map
