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

<Meta title='@dxtmisha/functional/en/Classes/Global'/>

# Global Class

A static class for managing global application data. Provides a simple and secure way to store and retrieve global configurations, settings, and data with protection against re-initialization.

## Key Features

- **One-time initialization** — data can be added only once to prevent accidental overwrites
- **Type-safe data retrieval** — TypeScript generics support for typed access
- **Global storage** — centralized place for storing application configurations
- **Simple API** — just two methods for working with global data
- **Conflict protection** — prevents re-initialization of global data
- **Memory efficient** — uses simple object copying without deep cloning

## Static Methods

### `add`

Adds global data. This method works only once to prevent accidental overwrites.

**Parameters:**
- `data: Record<string, any>` — object with global data

**Returns:** `void`

```javascript
import { Global } from '@dxtmisha/functional'

// Initialize global data (executed once)
Global.add({
  apiUrl: 'https://api.example.com',
  appName: 'MyApplication',
  version: '1.0.0',
  features: {
    darkMode: true,
    notifications: false
  },
  maxRetries: 3
})

// Attempt to re-initialize will be ignored
Global.add({
  apiUrl: 'https://different-api.com' // This will not overwrite data
})
```

### `get`

Returns a value by its name with typing support.

**Parameters:**
- `name: string` — property name to retrieve

**Returns:** `R` — value of specified type or undefined

```javascript
// Get data with automatic type inference
const apiUrl = Global.get('apiUrl')
console.log(apiUrl) // 'https://api.example.com'

// Get with explicit type specification
const features = Global.get<{ darkMode: boolean; notifications: boolean }>('features')
console.log(features.darkMode) // true

// Get primitive types
const maxRetries = Global.get<number>('maxRetries')
console.log(maxRetries) // 3

// Get non-existent property
const nonExistent = Global.get('nonExistent')
console.log(nonExistent) // undefined
```

## Practical Examples

### Application Configuration

```javascript
class AppConfig {
  static initialize() {
    Global.add({
      api: {
        baseUrl: process.env.API_BASE_URL || 'https://api.myapp.com',
        timeout: 10000,
        retries: 3
      },
      ui: {
        theme: 'light',
        language: 'en',
        animations: true
      },
      features: {
        newDashboard: true,
        advancedSearch: false
      },
      app: {
        name: 'MyApplication',
        version: '2.1.0',
        environment: process.env.NODE_ENV || 'production'
      }
    })
  }

  static getApiConfig() {
    return Global.get('api')
  }

  static isFeatureEnabled(featureName) {
    const features = Global.get('features')
    return features ? features[featureName] || false : false
  }
}

// Usage
AppConfig.initialize()
const apiConfig = AppConfig.getApiConfig()
console.log(`API URL: ${apiConfig.baseUrl}`)
```

### Constants System

```javascript
const AppConstants = {
  setup() {
    Global.add({
      constants: {
        limits: {
          maxFileSize: 10 * 1024 * 1024, // 10MB
          sessionTimeout: 30 * 60 * 1000 // 30 minutes
        },
        endpoints: {
          auth: '/api/auth',
          users: '/api/users'
        },
        patterns: {
          email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
          phone: /^\+?[\d\s\-\(\)]+$/
        }
      }
    })
  },

  validateEmail(email) {
    const patterns = Global.get('constants')?.patterns
    return patterns ? patterns.email.test(email) : false
  }
}

// Usage
AppConstants.setup()
console.log(AppConstants.validateEmail('user@example.com')) // true
```

The Global class provides a simple and reliable way to manage global application data, ensuring centralized storage of configurations with protection against accidental overwrites.
