{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-register-routes",
  "title": "Register routes",
  "description": "Where a register's record and request pages live: one provider an app sets its own paths in, so page blocks link to records and requests without naming a path themselves.",
  "files": [
    {
      "path": "pages/register-routes.tsx",
      "content": "import { createContext, useContext, type ReactNode } from \"react\";\n\n/**\n * Where one record entity's pages live. A page block links to an entity's\n * records through these, so a register with several record entities names\n * each one's routes in one place. Paths carry no leading `#`.\n */\nexport interface EntityRoutes {\n  /** The list of the entity's records. */\n  records: string;\n  /** The form that creates one of them. */\n  createRecord: string;\n  /** One record's page. */\n  record(id: string): string;\n  /**\n   * The form that invokes one of the registry's governed actions that adds\n   * to these records, by action id. An app that names no route for it offers none.\n   */\n  recordAction?(actionId: string): string;\n  /**\n   * The form that invokes one of the registry's governed actions on one\n   * record, by the record's id and the action id. An app that names no route\n   * for it offers none.\n   */\n  action?(recordId: string, actionId: string): string;\n}\n\n/**\n * Where a register's pages live. A page block links to a record or a request\n * through these, never through a path of its own, so an app names its\n * register's routes in one place. Paths carry no leading `#`; a link adds it.\n */\nexport interface RegisterRoutes {\n  /** The list of records. */\n  records: string;\n  /** The form that creates a record. */\n  createRecord: string;\n  /** One record's page. */\n  record(id: string): string;\n  /** The form that asks for a change to one record. */\n  changeRecord(id: string): string;\n  /**\n   * The form that starts a request away from any record's page: a request\n   * that only creates, or one whose record is picked on the form. The route\n   * names the request entity as a request route does. An app that names no\n   * route for it offers none.\n   */\n  createRequest?: string;\n  /**\n   * The form that invokes one of the registry's governed actions on these\n   * records, by action id. An app that names no route for it offers none.\n   */\n  recordAction?(id: string): string;\n  /**\n   * The form that invokes one of the registry's governed actions on one of\n   * these records, by the record's id and the action id. An app that names no\n   * route for it offers none.\n   */\n  action?(recordId: string, actionId: string): string;\n  /**\n   * The routes of any other record entity, by entity id. An app that names\n   * none has pages for the register's own record entity only.\n   */\n  entity?(entity: string): EntityRoutes;\n  /** The list of requests. */\n  requests: string;\n  /** One request's page. */\n  request(id: string): string;\n  /** The form that edits one request before it is decided. */\n  editRequest(id: string): string;\n}\n\n/** An entity's routes under `path`; ids are encoded as one segment. */\nexport function entityRoutesUnder(path: string): EntityRoutes {\n  const under = (base: string, id: string) =>\n    `${base}/${encodeURIComponent(id)}`;\n  return {\n    records: path,\n    createRecord: `${path}/create`,\n    record: (id) => under(path, id),\n    recordAction: (actionId) => under(`${path}/actions`, actionId),\n    action: (recordId, actionId) =>\n      under(`${under(path, recordId)}/actions`, actionId),\n  };\n}\n\n/**\n * Routes for records under `recordsPath` and requests under `requestsPath`;\n * ids are encoded as one segment. Every other record entity lives under\n * `/entities/<entity>`.\n */\nexport function registerRoutesUnder(\n  recordsPath: string,\n  requestsPath: string,\n): RegisterRoutes {\n  const under = (path: string, id: string) =>\n    `${path}/${encodeURIComponent(id)}`;\n  return {\n    records: recordsPath,\n    createRecord: `${recordsPath}/create`,\n    record: (id) => under(recordsPath, id),\n    changeRecord: (id) => `${under(recordsPath, id)}/change`,\n    createRequest: `${requestsPath}/create`,\n    recordAction: (id) => under(`${recordsPath}/actions`, id),\n    action: (recordId, actionId) =>\n      under(`${under(recordsPath, recordId)}/actions`, actionId),\n    entity: (entity) => entityRoutesUnder(under(\"/entities\", entity)),\n    requests: requestsPath,\n    request: (id) => under(requestsPath, id),\n    editRequest: (id) => `${under(requestsPath, id)}/edit`,\n  };\n}\n\n/** The generic routes a page block reads when no app has named its own. */\nexport const registerRoutes: RegisterRoutes = registerRoutesUnder(\n  \"/records\",\n  \"/requests\",\n);\n\nconst RegisterRoutesContext = createContext<RegisterRoutes>(registerRoutes);\n\nexport function RegisterRoutesProvider({\n  routes,\n  children,\n}: {\n  routes: RegisterRoutes;\n  children: ReactNode;\n}) {\n  return (\n    <RegisterRoutesContext.Provider value={routes}>\n      {children}\n    </RegisterRoutesContext.Provider>\n  );\n}\n\nexport function useRegisterRoutes(): RegisterRoutes {\n  return useContext(RegisterRoutesContext);\n}\n\n/** Which of a `RegisterRoutes` value's routes a path stands for, with the id it carries. */\nexport type RegisterRouteMatch =\n  | { kind: \"records\" }\n  | { kind: \"createRecord\" }\n  | { kind: \"record\"; id: string }\n  | { kind: \"changeRecord\"; id: string }\n  | { kind: \"createRequest\" }\n  | { kind: \"recordAction\"; id: string }\n  | { kind: \"action\"; id: string; actionId: string }\n  | { kind: \"requests\" }\n  | { kind: \"request\"; id: string }\n  | { kind: \"editRequest\"; id: string }\n  | { kind: \"none\" };\n\nfunction segmentsOf(path: string): string[] {\n  return path.split(\"/\").filter(Boolean);\n}\n\n/** A path segment decoded; null where it is not valid percent-encoding, so such a path matches no route. */\nexport function decodeSegment(segment: string): string | null {\n  try {\n    return decodeURIComponent(segment);\n  } catch (error) {\n    if (error instanceof URIError) return null;\n    throw error;\n  }\n}\n\n// A path built from a route function for ids no real id could ever equal,\n// so each segment it lands on is always the id's own: comparing segment\n// counts and every other segment then tells a match from a miss.\nconst SENTINEL_ID = \"\\u0000register-route-sentinel\\u0000\";\nconst SENTINEL_ENTITY = \"\\u0000entity-route-sentinel\\u0000\";\nconst SENTINEL_ACTION = \"\\u0000action-route-sentinel\\u0000\";\n\n/**\n * What each sentinel of `pattern` stood for in `path`, decoded; null off a\n * match. A pattern without a sentinel matches only itself, with no captures.\n */\nfunction matchCaptures(\n  pattern: string,\n  path: string,\n  sentinels: readonly string[],\n): Map<string, string> | null {\n  const patternSegments = segmentsOf(pattern);\n  const pathSegments = segmentsOf(path);\n  if (patternSegments.length !== pathSegments.length) return null;\n  const captured = new Map<string, string>();\n  for (let i = 0; i < patternSegments.length; i++) {\n    const sentinel = sentinels.find(\n      (item) => encodeURIComponent(item) === patternSegments[i],\n    );\n    if (sentinel !== undefined) {\n      const decoded = decodeSegment(pathSegments[i]!);\n      if (decoded === null) return null;\n      captured.set(sentinel, decoded);\n    } else if (patternSegments[i] !== pathSegments[i]) return null;\n  }\n  return captured;\n}\n\n/** Where the sentinel id landed in `pattern`'s segments, matched against `path`'s; null off a match. */\nfunction matchOne(pattern: string, path: string): string | null {\n  return matchCaptures(pattern, path, [SENTINEL_ID])?.get(SENTINEL_ID) ?? null;\n}\n\n/**\n * Which route of `routes` a path stands for. Every pattern is built by\n * calling `routes`' own functions with a sentinel id, never by assuming a\n * literal segment such as \"change\" or \"edit\", so a page composed over this\n * stays correct however an app shapes its own `RegisterRoutes`.\n */\nexport function matchRegisterRoute(\n  routes: RegisterRoutes,\n  path: string,\n): RegisterRouteMatch {\n  if (path === routes.records) return { kind: \"records\" };\n  if (path === routes.createRecord) return { kind: \"createRecord\" };\n  if (path === routes.requests) return { kind: \"requests\" };\n  if (routes.createRequest !== undefined && path === routes.createRequest)\n    return { kind: \"createRequest\" };\n  // A record id is the registry's own identifier, never the word an action\n  // route places beside it, so an action form is matched first.\n  const actionId = routes.recordAction\n    ? matchOne(routes.recordAction(SENTINEL_ID), path)\n    : null;\n  if (actionId !== null) return { kind: \"recordAction\", id: actionId };\n  const invoked = routes.action\n    ? matchCaptures(routes.action(SENTINEL_ID, SENTINEL_ACTION), path, [\n        SENTINEL_ID,\n        SENTINEL_ACTION,\n      ])\n    : null;\n  if (invoked)\n    return {\n      kind: \"action\",\n      id: invoked.get(SENTINEL_ID)!,\n      actionId: invoked.get(SENTINEL_ACTION)!,\n    };\n  const changeRecordId = matchOne(routes.changeRecord(SENTINEL_ID), path);\n  if (changeRecordId !== null) return { kind: \"changeRecord\", id: changeRecordId };\n  const recordId = matchOne(routes.record(SENTINEL_ID), path);\n  if (recordId !== null) return { kind: \"record\", id: recordId };\n  const editRequestId = matchOne(routes.editRequest(SENTINEL_ID), path);\n  if (editRequestId !== null) return { kind: \"editRequest\", id: editRequestId };\n  const requestId = matchOne(routes.request(SENTINEL_ID), path);\n  if (requestId !== null) return { kind: \"request\", id: requestId };\n  return { kind: \"none\" };\n}\n\n/** Which route of one entity's `EntityRoutes` a path stands for, with the entity and ids it carries. */\nexport type EntityRouteMatch =\n  | { kind: \"entityRecords\"; entity: string }\n  | { kind: \"entityCreateRecord\"; entity: string }\n  | { kind: \"entityRecord\"; entity: string; id: string }\n  | { kind: \"entityRecordAction\"; entity: string; actionId: string }\n  | { kind: \"entityAction\"; entity: string; id: string; actionId: string }\n  | { kind: \"none\" };\n\n/**\n * Which per-entity route of `routes` a path stands for. As with\n * `matchRegisterRoute`, every pattern comes from `routes`' own functions\n * called with sentinels. The register's own record entity keeps its routes\n * and is matched by `matchRegisterRoute`; this matches `routes.entity`'s.\n */\nexport function matchEntityRoute(\n  routes: RegisterRoutes,\n  path: string,\n): EntityRouteMatch {\n  if (!routes.entity) return { kind: \"none\" };\n  const at = routes.entity(SENTINEL_ENTITY);\n  const sentinels = [SENTINEL_ENTITY, SENTINEL_ID, SENTINEL_ACTION];\n  const entityOf = (captured: Map<string, string>) =>\n    captured.get(SENTINEL_ENTITY)!;\n  const invoked = at.action\n    ? matchCaptures(at.action(SENTINEL_ID, SENTINEL_ACTION), path, sentinels)\n    : null;\n  if (invoked)\n    return {\n      kind: \"entityAction\",\n      entity: entityOf(invoked),\n      id: invoked.get(SENTINEL_ID)!,\n      actionId: invoked.get(SENTINEL_ACTION)!,\n    };\n  const listed = at.recordAction\n    ? matchCaptures(at.recordAction(SENTINEL_ACTION), path, sentinels)\n    : null;\n  if (listed)\n    return {\n      kind: \"entityRecordAction\",\n      entity: entityOf(listed),\n      actionId: listed.get(SENTINEL_ACTION)!,\n    };\n  const created = matchCaptures(at.createRecord, path, sentinels);\n  if (created) return { kind: \"entityCreateRecord\", entity: entityOf(created) };\n  const one = matchCaptures(at.record(SENTINEL_ID), path, sentinels);\n  if (one)\n    return {\n      kind: \"entityRecord\",\n      entity: entityOf(one),\n      id: one.get(SENTINEL_ID)!,\n    };\n  const all = matchCaptures(at.records, path, sentinels);\n  if (all) return { kind: \"entityRecords\", entity: entityOf(all) };\n  return { kind: \"none\" };\n}\n",
      "type": "registry:block",
      "target": "@components/pages/register-routes.tsx"
    }
  ],
  "type": "registry:block"
}