{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-start-places",
  "title": "Start places",
  "description": "The start place's route and the choice of when a session opens on it: the starts no record list offers, and whether the session lists any record.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/pages-register-routes"
  ],
  "files": [
    {
      "path": "pages/start-places.ts",
      "content": "import type {\n  EntityModel,\n  RecordActionReference,\n  RegistryModel,\n} from \"@registrystack/app-runtime\";\nimport {\n  decodeSegment,\n  type RegisterRoutes,\n} from \"@/blocks/pages/register-routes\";\n\n/** The place that lists what a session may start, and the form of each invoke start under it. */\nexport const startsPath = \"/start\";\n\n/** The form of one governed action started with no record. */\nexport function startActionPath(actionId: string): string {\n  return `${startsPath}/${encodeURIComponent(actionId)}`;\n}\n\n/** The action a start route names, `{}` for the list of starts; null off a start route. */\nexport function matchStartRoute(path: string): { actionId?: string } | null {\n  if (path === startsPath) return {};\n  const parts = path.split(\"/\").filter(Boolean).map(decodeSegment);\n  return parts.length === 2 && parts[0] === \"start\" && parts[1] !== null\n    ? { actionId: parts[1] }\n    : null;\n}\n\n/** Whether the session may list the records of any record entity it is offered. */\nexport function listsRecords(entities: readonly EntityModel[]): boolean {\n  return entities.some(\n    (entity) => entity.kind === \"record\" && entity.operations.list === true,\n  );\n}\n\n/**\n * The starts no record list offers that the start place has a form for: an\n * action whose result is not a record entity the session lists, a create of a\n * record entity it cannot list, and any request create. No list of requests\n * offers a start, so a request create is offered here: one that only creates,\n * and one whose record the host leaves to be picked on its form because the\n * session may list that record but not open its page. A request create is kept\n * only where the app names the form it opens (`createRequest` of `routes`),\n * and only where the model holds a record entity, which the request form and\n * the request pages a send lands on are read through.\n */\nexport function unlistedStarts(\n  starts: readonly RecordActionReference[],\n  entities: readonly EntityModel[],\n  routes: Pick<RegisterRoutes, \"createRequest\"> = {},\n): RecordActionReference[] {\n  return starts.filter((start) => {\n    const entity = entities.find((candidate) => candidate.id === start.entity);\n    const record = entity?.kind === \"record\";\n    const listed = record && entity.operations.list === true;\n    if (start.name === \"invoke\") return !listed;\n    if (entity?.kind === \"request\")\n      return (\n        routes.createRequest !== undefined &&\n        entities.some((candidate) => candidate.kind === \"record\")\n      );\n    return record && !listed;\n  });\n}\n\n/**\n * Whether the start place is where a session opens: it has no home list and\n * can list no record, and the start place offers it a start, or it can list\n * nothing at all and has no entity or only starts the start place has no form\n * for. The start place words that as an empty state rather than a failed page.\n * Where the starts could not be read (`failed`), it opens there too, so the\n * failure shows with a retry instead of a list the session would be refused.\n * `routes` decides whether a request create has a form, as for `unlistedStarts`.\n */\nexport function opensOnStarts(\n  model: RegistryModel | undefined,\n  starts: readonly RecordActionReference[] | undefined,\n  failed = false,\n  routes: Pick<RegisterRoutes, \"createRequest\"> = {},\n): boolean {\n  if (!model || model.home || listsRecords(model.entities)) return false;\n  if (failed) return true;\n  const held = starts ?? [];\n  if (unlistedStarts(held, model.entities, routes).length > 0) return true;\n  const listsNothing = !model.entities.some(\n    (entity) => entity.operations.list === true,\n  );\n  return listsNothing && (model.entities.length === 0 || held.length > 0);\n}\n",
      "type": "registry:block",
      "target": "@components/pages/start-places.ts"
    }
  ],
  "type": "registry:block"
}