import type {StoreOption} from '@/shared/providers/WorkspaceProvider';

/** The sentinel the workspace uses for "not filtered to one store". */
export const ALL_STORES = 'all';

/**
 * Domain rules for the store directory.
 *
 * Framework-free so the same resolution is available to a server component, a
 * test or an export — the React binding lives in hooks/useStoreDirectory.
 */

/**
 * Resolve an id to a display name.
 *
 * Falls back to the id rather than to "Unknown store": an id at least tells a
 * developer which store lost its label, and a merchant only ever sees it if
 * the directory and the data genuinely disagree.
 */
export function resolveStoreName(
  stores: StoreOption[],
  storeId: string,
): string {
  return stores.find((s) => s.id === storeId)?.name ?? storeId;
}

/**
 * The phrase pages put mid-sentence in a subtitle: "all stores", or the name.
 * Lowercase because every call site embeds it in running text.
 */
export function resolveScopeLabel(
  stores: StoreOption[],
  storeId: string,
): string {
  return storeId === ALL_STORES
    ? 'all stores'
    : resolveStoreName(stores, storeId);
}
