import {Meta} from '@storybook/addon-docs/blocks'

<Meta title='@dxtmisha/en/functional-basic/Classes/ErrorCenterInstance - Error Instance'/>

# ErrorCenterInstance Class

`ErrorCenterInstance` is the main engine of the error system. It combines an error registry and a handler manager. You can create independent instances of this class for different parts of the application to isolate their error processing logic.

## Key Features

- **Isolated Storage** — maintaining a local list of error causes.
- **Intelligent Merging** — automatic supplementation of error data based on a priority system.
- **Handling Delegation** — using `ErrorCenterHandler` to trigger callback functions.
- **Chainable API** — support for call chaining for configuration.

## Initialization

To create an instance, use the constructor `ErrorCenterInstance(causes, handler)`.

**Parameters:**
- `causes?: ErrorCenterCauseList` — initial list of error causes.
- `handler?: ErrorCenterHandler` — handler instance (if not specified, a new one is created).

```typescript
import { ErrorCenterInstance } from '@dxtmisha/functional-basic'

const myErrorCenter = new ErrorCenterInstance([
  { group: 'api', code: '404', label: 'Not Found' }
])
```

## Methods

### Action
- `on(cause: ErrorCenterCauseItem): this` — Starts the processing. Merges data from the registry with input data and passes it to the handler.

### Verification
- `has(code: string, group?: string): boolean` — Search in the local registry.
- `get(code: string, group?: string): ErrorCenterCauseItem | undefined` — Retrieve data from the registry.

### Configuration
- `add(cause: ErrorCenterCauseItem): this` — Add an error to the local registry.
- `addList(causes: ErrorCenterCauseList): this` — Batch addition of errors.
- `addHandler(group: ErrorCenterGroup, handler: ErrorCenterHandlerCallback): this` — Register a reaction to a group.
- `addHandlerList(handlers: ErrorCenterHandlerList): this` — Batch registration of reactions.

## Types

#### `ErrorCenterCauseItem`
Object describing a specific error.
- `group?: ErrorCenterGroup` — error group (e.g., 'api').
- `code: string` — unique error code.
- `priority?: number` — priority level (default `500`). Used during data merging.
- `label?: string` — error title.
- `message?: string` — error description.
- `details?: any` — additional data.

#### `ErrorCenterHandlerCallback`
- `(cause: ErrorCenterCauseItem) => void` — processing function.

## Examples

### Initialization with a Dictionary
```typescript
const myEC = new ErrorCenterInstance([
  { group: 'db', code: 'connection_lost', label: 'DB Unavailable' }
]);

myEC.addHandler('db', (cause) => alert(cause.label));
myEC.on({ group: 'db', code: 'connection_lost' });
```
