# Functions

Import these from `@grafloria/engine`.

## Functions

### `applyArchitectureModel`

Put sides, arrows, icons, group frames and junctions on the model the flowchart pipeline built — and lay it out.

```ts
function applyArchitectureModel(diagram: DiagramModel, model: MermaidArchitectureModel): void
```

### `applyBlockGrid`

Put the grid on the model the flowchart pipeline built — `metadata.grid` on the
diagram and every nested block — mark it block-beta, and lay it out.

```ts
function applyBlockGrid(diagram: DiagramModel, model: MermaidBlockModel): void
```

### `architectureModelToFlowchart`

The same diagram as Grafloria flowchart text: a subgraph per group (nested), a node per service.

```ts
function architectureModelToFlowchart(model: MermaidArchitectureModel): string
```

### `blockModelToFlowchart`

The same diagram as Grafloria flowchart text: every block a node line, every nested block a subgraph.

```ts
function blockModelToFlowchart(model: MermaidBlockModel): string
```

### `classModelFromDiagram`

```ts
function classModelFromDiagram(diagram: DiagramModel): MermaidClassModel
```

### `classModelToDiagram`

```ts
function classModelToDiagram(model: MermaidClassModel): DiagramModel
```

### `createToken`

Helper to create tokens

```ts
function createToken(
  type: TokenType,
  value: string,
  line: number,
  column: number,
  startIndex: number,
  endIndex: number
): Token
```

### `erMarkers`

Map a parsed cardinality pair onto the kit's explicit marker pair.

```ts
function erMarkers(rel: MermaidErRelationship): { tail: ErCardinalityMarker; head: ErCardinalityMarker }
```

### `erModelFromDiagram`

Read the Mermaid ER model back off a DiagramModel (for generation).

```ts
function erModelFromDiagram(diagram: DiagramModel): MermaidErModel
```

### `erModelToDiagram`

Build a DiagramModel from a parsed ER model.

```ts
function erModelToDiagram(model: MermaidErModel): DiagramModel
```

### `erSpecFrom`

Project the Mermaid model onto the diagram kit's `erDiagram()` options.

```ts
function erSpecFrom(model: MermaidErModel): ErSpec
```

### `generateArchitectureFromDiagram`

Write an architecture-beta diagram back as architecture-beta: groups, services, junctions, sided edges.

```ts
function generateArchitectureFromDiagram(diagram: DiagramModel): string
```

### `generateBlockFromDiagram`

Write a block-beta diagram back as block-beta: its grid, its blocks as written, its edges and styles.

```ts
function generateBlockFromDiagram(diagram: DiagramModel): string
```

### `generateClassFromDiagram`

```ts
function generateClassFromDiagram(diagram: DiagramModel): string
```

### `generateErFromDiagram`

Generate an `erDiagram` body directly from a DiagramModel.

```ts
function generateErFromDiagram(diagram: DiagramModel): string
```

### `generateMermaidClass`

```ts
function generateMermaidClass(model: MermaidClassModel): string
```

### `generateMermaidEr`

Emit a valid Mermaid `erDiagram` body. Two rules here are not style choices —
both were found by running the export through real Mermaid 11.16 (the oracle):

- THE LABEL IS ALWAYS WRITTEN, AND ALWAYS QUOTED. It is not optional
   (`A ||--o{ B` is a parse error, so an unlabelled relationship needs `: ""`),
   and Mermaid's ER lexer treats `one`, `many`, `zero`, … as CARDINALITY
   keywords even in label position — `: one` fails, `: "one"` parses. Quoting
   unconditionally makes the whole reserved-word class disappear.
 - NO `id["alias"]` ENTITY ALIAS. We parse it (it is forward syntax), but
   Mermaid 11.16 rejects it, and emitting a body real Mermaid cannot read
   breaks the governing invariant. The display name rides in the `%%grafloria:`
   sidecar instead — Tier 3, exactly what it is for.

```ts
function generateMermaidEr(model: MermaidErModel): string
```

### `generateMermaidState`

Emit a valid `stateDiagram-v2` body. The scoped pseudo-states are collapsed
back to `[*]`, which is the only spelling Mermaid accepts — writing
`__start__` would export a body that renders as a real state called
"__start__", i.e. a different diagram.

```ts
function generateMermaidState(model: MermaidStateModel): string
```

### `generateStateFromDiagram`

```ts
function generateStateFromDiagram(diagram: DiagramModel): string
```

### `isClassDef`

```ts
function isClassDef(node: ASTNode): node is ClassDefNode
```

### `isEdgeDefinition`

```ts
function isEdgeDefinition(node: ASTNode): node is EdgeDefinitionNode
```

### `isNodeDefinition`

Type guard helpers

```ts
function isNodeDefinition(node: ASTNode): node is NodeDefinitionNode
```

### `isStyleNode`

```ts
function isStyleNode(node: ASTNode): node is StyleNode
```

### `isSubgraph`

```ts
function isSubgraph(node: ASTNode): node is SubgraphNode
```

### `parseMermaidArchitecture`

```ts
function parseMermaidArchitecture(text: string): MermaidArchitectureModel
```

### `parseMermaidBlock`

```ts
function parseMermaidBlock(text: string): MermaidBlockModel
```

### `parseMermaidClass`

Parse a `classDiagram` body. Unknown lines (`click`, `style`, `namespace`,
`callback`, future syntax) are skipped — never turned into a class.

```ts
function parseMermaidClass(text: string): MermaidClassModel
```

### `parseMermaidEr`

Parse an `erDiagram` body into the Mermaid-shaped model. Lines that are not
grammar (styling directives, future syntax) are SKIPPED, never nodified.

```ts
function parseMermaidEr(text: string): MermaidErModel
```

### `parseMermaidState`

Parse a state-diagram body. `stateDiagram` (v1) and `stateDiagram-v2` share
this grammar — v2 is a different LAYOUT engine in Mermaid, not a different
syntax, so refusing v1 would be arbitrary.

```ts
function parseMermaidState(text: string): MermaidStateModel
```

### `stateModelFromDiagram`

```ts
function stateModelFromDiagram(diagram: DiagramModel): MermaidStateModel
```

### `stateModelToDiagram`

```ts
function stateModelToDiagram(model: MermaidStateModel): DiagramModel
```

### `umlRelationKind`

Operator → (kit kind, whether the kit's ends are the REVERSE of Mermaid's),
derived from the grammar rather than enumerated. This reproduces the previous
17-row table exactly and covers the other 55 spellings for free.

```ts
function umlRelationKind(operator: string): { kind: UmlKind; reversed: boolean }
```

### `umlSpecFrom`

Project onto `umlDiagram()` options — this is where Mermaid's
arrowhead-first operators are flipped into the kit's from→to convention.

```ts
function umlSpecFrom(model: MermaidClassModel): UmlSpec
```
