     1	import { RestApiResourceError } from '../../../lib/rest-api-errors.js'
     2	import { parseJsonApiQuery } from '../lib/querying-writing/connectors-query-parser.js'
     3	
     4	export default async function registerScopeRoutes ({ context, api, vars, log }) {
     5	  const { scopeName } = context
     6	  const basePath = vars.transport?.mountPath || ''
     7	
     8	  // Helper to create route handlers
     9	  const createRouteHandler = (scopeName, methodName) => {
    10	    return async ({ queryString, headers, params, body, context }) => {
    11	      const scope = api.scopes[scopeName]
    12	      if (!scope) {
    13	        throw new RestApiResourceError(
    14	          `Scope '${scopeName}' not found`,
    15	          {
    16	            subtype: 'not_found',
    17	            resourceType: 'scope',
    18	            resourceId: scopeName
    19	          }
    20	        )
    21	      }
    22	
    23	      // Build parameters for the scope method
    24	      const methodParams = {}
    25	
    26	      // Add ID for single-resource operations
    27	      if (['get', 'put', 'patch', 'delete'].includes(methodName)) {
    28	        methodParams.id = params.id
    29	      }
    30	
    31	      // Parse query parameters for read operations
    32	      if (['query', 'get'].includes(methodName)) {
    33	        methodParams.queryParams = parseJsonApiQuery(queryString)
    34	        methodParams.isTransport = true
    35	      }
    36	
    37	      // Add body for write operations
    38	      if (['post', 'put', 'patch'].includes(methodName)) {
    39	        methodParams.inputRecord = body
    40	        methodParams.isTransport = true
    41	
    42	        // Add query params for includes/fields on write operations
    43	        if (queryString) {
    44	          methodParams.queryParams = parseJsonApiQuery(queryString)
    45	        }
    46	      }
    47	
    48	      // Call the scope method
    49	      const result = await scope[methodName](methodParams, context)
    50	
    51	      // Return the result (transport plugin handles response formatting)
    52	      return result
    53	    }
    54	  }
    55	
    56	  const scopePath = `${basePath}/${scopeName}`
    57	
    58	  // Register routes for each HTTP method
    59	  // GET /api/{scope} - Query collection
    60	  await api.addRoute({
    61	    method: 'GET',
    62	    path: scopePath,
    63	    handler: createRouteHandler(scopeName, 'query')
    64	  })
    65	
    66	  // GET /api/{scope}/{id} - Get single resource
    67	  await api.addRoute({
    68	    method: 'GET',
    69	    path: `${scopePath}/:id`,
    70	    handler: createRouteHandler(scopeName, 'get')
    71	  })
    72	
    73	  // POST /api/{scope} - Create resource
    74	  await api.addRoute({
    75	    method: 'POST',
    76	    path: scopePath,
    77	    handler: createRouteHandler(scopeName, 'post')
    78	  })
    79	
    80	  // PUT /api/{scope}/{id} - Replace resource
    81	  await api.addRoute({
    82	    method: 'PUT',
    83	    path: `${scopePath}/:id`,
    84	    handler: createRouteHandler(scopeName, 'put')
    85	  })
    86	
    87	  // PATCH /api/{scope}/{id} - Update resource
    88	  await api.addRoute({
    89	    method: 'PATCH',
    90	    path: `${scopePath}/:id`,
    91	    handler: createRouteHandler(scopeName, 'patch')
    92	  })
    93	
    94	  // DELETE /api/{scope}/{id} - Delete resource
    95	  await api.addRoute({
    96	    method: 'DELETE',
    97	    path: `${scopePath}/:id`,
    98	    handler: createRouteHandler(scopeName, 'delete')
    99	  })
   100	
   101	  log.info(`Routes registered for scope '${scopeName}'`)
   102	}
