/**
 * Scope resolver — determines whether a module's data lives per-space or globally.
 *
 * When a module declares `defaultScope: 'global'`, its Automerge documents
 * use the `global:` prefix instead of `{space}:`, so data is shared across
 * all spaces. Space owners can override this per-module via `moduleScopeOverrides`.
 */

import { getModule } from './module';
import type { ModuleScope } from './module';

/**
 * Resolve the effective data-space prefix for a module.
 *
 * Returns 'global' if the module should use global-scoped data,
 * otherwise returns the current space slug for space-scoped data.
 */
export function resolveDataSpace(
	moduleId: string,
	spaceSlug: string,
	scopeOverrides?: Record<string, ModuleScope> | null,
): string {
	const mod = getModule(moduleId);
	if (!mod) return spaceSlug;

	const effectiveScope = scopeOverrides?.[moduleId] ?? mod.scoping.defaultScope;
	return effectiveScope === 'global' ? 'global' : spaceSlug;
}
