export interface MatrixAuthorityContext {
  /**
   * Authority that served the app bundle.
   */
  readonly assetHostRoot: string;

  /**
   * Authority for explicit platform services.
   */
  readonly platformServiceRoot: string;

  /**
   * Authenticated session authority, or null for anonymous/public sessions.
   */
  readonly sessionAuthorityRoot: string | null;

  /**
   * Authority currently selected by the page.
   */
  readonly selectedAuthorityRoot: string | null;

  /**
   * Ephemeral root for this runtime/tab.
   */
  readonly runtimeInstanceRoot: string | null;

  readonly isAuthenticated: boolean;
  readonly isPlatformAdmin: boolean;
  readonly selectableAuthorityRoots: readonly string[];
}

export interface MatrixAuthorityTarget {
  readonly targetMount: string;
  readonly targetRoot: string;
}

export function requireSelectedAuthorityRoot(authority: MatrixAuthorityContext): string {
  const root = authority.selectedAuthorityRoot?.trim() ?? '';
  if (!root) {
    throw new Error('MISSING_SELECTED_AUTHORITY_ROOT');
  }
  return root;
}

export function userAuthorityTarget(
  authority: MatrixAuthorityContext,
  mount: string,
): MatrixAuthorityTarget {
  return {
    targetMount: mount,
    targetRoot: requireSelectedAuthorityRoot(authority),
  };
}

export function platformServiceTarget(
  authority: MatrixAuthorityContext,
  mount: string,
): MatrixAuthorityTarget {
  const root = authority.platformServiceRoot.trim();
  if (!root) {
    throw new Error('MISSING_PLATFORM_SERVICE_ROOT');
  }
  return {
    targetMount: mount,
    targetRoot: root,
  };
}
