# Classes

Import these from `@grafloria/engine`.

## Classes

### `ASTTransformer`

```ts
class ASTTransformer
```

**Methods**

- `transform(ast: DiagramNode, options: TransformOptions = {}): DiagramModel` — Transform AST into DiagramModel
- `resetAutoPosition(startPosition: { x: number; y: number }): void` — Reset auto-positioning state

### `BidirectionalSync`

```ts
class BidirectionalSync
```

**Methods**

- `constructor(options: SyncOptions = {})`
- `initialize(diagram: DiagramModel, initialText?: string): void` — Initialize sync with diagram and initial text
- `onTextChange(newText: string): void` — Update text (called when user edits text)
- `markVisualEdit(): void` — Mark next changes as from visual editor
- `markTextEdit(): void` — Mark next changes as from text editor
- `getCurrentText(): string` — Get current text
- `getDiagram(): DiagramModel | undefined` — Get current diagram
- `getState(): Readonly<SyncState>` — Get sync state
- `onSync(callback: SyncCallback): () => void` — Add sync callback
- `pause(): void` — Pause sync
- `resume(): void` — Resume sync
- `forceSync(direction: 'text-to-visual' | 'visual-to-text'): void` — Force sync in specific direction
- `dispose(): void` — Dispose and cleanup

### `DiagramAnalyzer`

```ts
class DiagramAnalyzer
```

**Methods**

- `analyze(diagram: DiagramModel): DiagramAnalysis` — Analyze diagram structure

### `DSLFormatter`

```ts
class DSLFormatter
```

**Methods**

- `constructor(options: FormatterOptions = {})`
- `format(text: string): string` — Format DSL text
- `wrapLongLines(text: string): string` — Wrap long lines
- `normalizeWhitespace(text: string): string` — Normalize whitespace
- `removeComments(text: string): string` — Remove comments
- `addSectionHeaders(text: string): string` — Add section headers
- `prettyPrint(text: string, addHeaders: boolean = false): string` — Pretty print with all formatting options
- `minify(text: string): string` — Minify (remove all formatting)

### `DSLGenerator`

```ts
class DSLGenerator
```

**Methods**

- `constructor()`
- `generate(diagram: DiagramModel, options: GeneratorOptions = {}): string` — Generate DSL text from diagram

### `IntegratedSyncManager`

```ts
class IntegratedSyncManager
```

**Methods**

- `constructor(options: IntegratedSyncOptions = {})`
- `initialize(diagram: DiagramModel, initialText?: string): void` — Initialize with diagram and optional initial text
- `onTextEdit(newText: string): void` — Handle text editor changes
- `onVisualEdit(): void` — Handle visual editor changes
- `getText(): string` — Get current text
- `getDiagram(): DiagramModel | undefined` — Get current diagram
- `getStatus(): SyncStatus` — Get sync status
- `getMetrics(): SyncMetrics` — Get sync metrics
- `async applyLayout(presetId?: string): Promise<void>` — Apply layout to current diagram
- `suggestLayout(): LayoutSuggestion | null` — Get layout suggestion
- `onTextChange(callback: TextChangeCallback): () => void` — Subscribe to text changes
- `onDiagramChange(callback: DiagramChangeCallback): () => void` — Subscribe to diagram changes
- `onStatusChange(callback: StatusCallback): () => void` — Subscribe to status changes
- `pause(): void` — Pause sync
- `resume(): void` — Resume sync
- `forceSync(direction: 'text-to-visual' | 'visual-to-text'): void` — Force sync in specific direction
- `resetMetrics(): void` — Reset metrics
- `getFormattedStatus(): string` — Get formatted status string
- `getMetricsSummary(): string` — Get metrics summary
- `dispose(): void` — Dispose and cleanup

### `LayoutDetector`

```ts
class LayoutDetector
```

**Methods**

- `detect(diagram: DiagramModel, ast?: DiagramNode): LayoutSuggestion` — Detect optimal layout for a diagram

### `Lexer`

```ts
class Lexer
```

**Methods**

- `constructor(input: string)`
- `tokenize(): Token[]` — Tokenize the entire input

### `ParseError`

```ts
class ParseError extends Error
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | `string` |  |  |
| `message` | `string` |  |  |
| `stack?` | `string` |  |  |

**Methods**

- `constructor( message: string, public token: Token, public line: number, public column: number )`

### `Parser`

```ts
class Parser
```

**Methods**

- `parse(tokens: Token[]): DiagramNode` — Parse tokens into an AST

### `SyncStateManager`

```ts
class SyncStateManager
```

**Methods**

- `startSync(direction: SyncDirection): void` — Mark sync as started
- `completeSync(direction: SyncDirection, duration: number): void` — Mark sync as completed
- `failSync(error: string): void` — Mark sync as failed
- `recordConflict( sources: [SyncDirection, SyncDirection], resolution: ConflictInfo['resolution'] ): void` — Record a conflict
- `getStatus(): Readonly<SyncStatus>` — Get current status
- `getMetrics(): Readonly<SyncMetrics>` — Get metrics
- `getConflicts(limit: number = 10): ConflictInfo[]` — Get recent conflicts
- `onStatusChange(callback: StatusChangeCallback): () => void` — Subscribe to status changes
- `resetMetrics(): void` — Reset metrics
- `getFormattedStatus(): string` — Get formatted status for display
- `getMetricsSummary(): string` — Get metrics summary
