# Template Library

Import these from `@grafloria/engine`.

## Functions

### `createTemplateLibrary`

Create a new template library instance
Factory function for creating template library managers

```ts
function createTemplateLibrary(includeBuiltIn = true): TemplateLibraryManager
```

**Parameters**

- `includeBuiltIn`: Whether to include built-in templates (default: true)

**Returns** New template library manager instance

### `getAllTemplates`

Get all templates as an array

```ts
function getAllTemplates(): NodeTemplate[]
```

### `getTemplatesByCategory`

Get templates grouped by category

```ts
function getTemplatesByCategory(): Record<TemplateCategory, NodeTemplate[]>
```

### `getUnregisteredTemplates`

Check if templates are registered in a TemplateRegistry

```ts
function getUnregisteredTemplates(registry: TemplateRegistry): string[]
```

**Example**

```typescript
const missing = getUnregisteredTemplates(engine.templateRegistry);
if (missing.length > 0) {
  console.log('Missing templates:', missing);
  registerTemplateLibrary(engine.templateRegistry);
}
```

### `initializeTemplateLibrary`

Initialize a template library with all built-in templates

```ts
function initializeTemplateLibrary(manager?: TemplateLibraryManager): TemplateLibraryManager
```

**Parameters**

- `manager`: Library manager to populate (optional, creates new if not provided)

**Returns** Populated template library manager

### `registerCustomTemplate`

Create a custom template and register it

```ts
function registerCustomTemplate(
  template: NodeTemplate,
  category: TemplateCategory,
  tags: string[] = []
): void
```

### `registerTemplateLibrary`

Use this to make the template library available to NodeFactory:

```ts
function registerTemplateLibrary(registry: TemplateRegistry): number
```

**Returns** Number of templates registered

**Example**

```typescript
import { registerTemplateLibrary } from '@grafloria/engine';

// In your engine setup
const count = registerTemplateLibrary(engine.templateRegistry);
// count = 20 (number of templates registered)

// Now you can use templates with NodeFactory
const node = engine.nodeFactory.createFromTemplate(
  'user-avatar',
  { name: 'John', status: 'online' },
  { x: 100, y: 100 }
);
```

### `registerTemplatesByCategory`

Register templates from a specific category

```ts
function registerTemplatesByCategory(
  registry: TemplateRegistry,
  category: 'common' | 'workflow' | 'data-viz' | 'diagram' | 'erd'
): number
```

**Returns** Number of templates registered

**Example**

```typescript
// Only register workflow templates
const count = registerTemplatesByCategory(engine.templateRegistry, 'workflow');
// count = 7 (number of workflow templates)
```

### `registerTemplatesById`

Register specific templates by ID

```ts
function registerTemplatesById(
  registry: TemplateRegistry,
  templateIds: string[]
): TemplateRegistrationResult
```

**Returns** Registration result with counts and missing template IDs

**Example**

```typescript
// Only register specific templates
const result = registerTemplatesById(engine.templateRegistry, [
  'user-avatar',
  'card-node',
  'process-step'
]);
// result = { registered: 3, notFound: [], total: 3 }
```

## Constants

### `CommonTemplates`

Export all common templates

```ts
const CommonTemplates: { UserAvatar: NodeTemplate; CardNode: NodeTemplate; ButtonNode: NodeTemplate; InputField: NodeTemplate; BadgeLabel: NodeTemplate; IconNode: NodeTemplate; }
```

### `DataVizTemplates`

Export all data visualization templates

```ts
const DataVizTemplates: { MetricCard: NodeTemplate; Gauge: NodeTemplate; BarChart: NodeTemplate; DataTable: NodeTemplate; PieChart: NodeTemplate; StatCounter: NodeTemplate; ProgressBar: NodeTemplate; }
```

### `ERDTemplates`

Export all ERD templates

```ts
const ERDTemplates: { ERDTable: NodeTemplate; ERDField: NodeTemplate; ERDRelationship: NodeTemplate; ERDTableOptionA: NodeTemplate; ERDFieldOptionA: NodeTemplate; ERDTableContainerOptionB: NodeTemplate; ERDTableHeaderOptionB: NodeTemplate; ERDFieldOptionB: NodeTemplate; ERDTableRepeater: NodeTemplate; }
```

### `TemplateLibrary`

Global template library instance (backward compatible)
Uses lazy initialization via getter

```ts
const TemplateLibrary: TemplateLibraryManager
```

### `WorkflowTemplates`

Export all workflow templates

```ts
const WorkflowTemplates: { ProcessStep: NodeTemplate; DecisionNode: NodeTemplate; StartEvent: NodeTemplate; EndEvent: NodeTemplate; Subprocess: NodeTemplate; Gateway: NodeTemplate; Activity: NodeTemplate; }
```

## Interfaces

### `TemplateInfo`

Template metadata for discovery

```ts
interface TemplateInfo
```

**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `template` | `NodeTemplate` |  |  |
| `category` | `TemplateCategory` |  |  |
| `tags` | `string[]` |  |  |

## Types

### `TemplateCategory`

Also has every member of `String`, listed on its own entry.

Template categories for organization

```ts
type TemplateCategory = 'common' | 'workflow' | 'data-viz' | 'diagram' | 'erd';
```

## Other exports

### `Activity`

```ts
Activity
```

### `BadgeLabel`

```ts
BadgeLabel
```

### `BarChart`

```ts
BarChart
```

### `ButtonNode`

```ts
ButtonNode
```

### `CardNode`

```ts
CardNode
```

### `DataTable`

```ts
DataTable
```

### `DecisionNode`

```ts
DecisionNode
```

### `EndEvent`

```ts
EndEvent
```

### `ERDField`

```ts
ERDField
```

### `ERDRelationship`

```ts
ERDRelationship
```

### `ERDTable`

```ts
ERDTable
```

### `Gateway`

```ts
Gateway
```

### `Gauge`

```ts
Gauge
```

### `IconNode`

```ts
IconNode
```

### `InputField`

```ts
InputField
```

### `MetricCard`

```ts
MetricCard
```

### `PieChart`

```ts
PieChart
```

### `ProcessStep`

```ts
ProcessStep
```

### `ProgressBar`

```ts
ProgressBar
```

### `StartEvent`

```ts
StartEvent
```

### `StatCounter`

```ts
StatCounter
```

### `Subprocess`

```ts
Subprocess
```

### `UserAvatar`

```ts
UserAvatar
```
