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

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

# CookieBlock Class

Static class for managing cookie access status and privacy compliance. Provides a simple interface to block or allow cookies throughout the application, integrating with the Cookie class to ensure privacy regulations compliance like GDPR.

## Key Features

- **Global cookie control** — centralized control over cookie usage across entire application
- **Privacy compliance** — helps implement GDPR, CCPA, and other privacy regulations
- **Persistent settings** — uses DataStorage to remember user's cookie preferences
- **Integration with Cookie class** — Cookie class automatically respects CookieBlock settings
- **Simple API** — just two methods for getting and setting cookie block status
- **Browser compatibility** — works safely in all environments

## Basic Usage

### `get`

Gets current cookie block status.

**Returns:** `boolean` — true if cookies are blocked, false if allowed

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

// Check if cookies are currently blocked
const isBlocked = CookieBlock.get()
console.log('Cookies blocked:', isBlocked) // false by default

if (isBlocked) {
  console.log('Cookies are blocked by user preference')
} else {
  console.log('Cookies are allowed')
}
```

### `set`

Sets cookie block status and persists the setting.

**Parameters:**
- `value: boolean` — true to block cookies, false to allow

```javascript
// Block all cookies
CookieBlock.set(true)
console.log('Cookies blocked:', CookieBlock.get()) // true

// Allow cookies
CookieBlock.set(false)
console.log('Cookies allowed:', CookieBlock.get()) // false

// Setting persists across browser sessions
// User preference is saved in localStorage
```

## Integration with Cookie Class

The CookieBlock class automatically prevents Cookie operations when blocking is enabled:

```javascript
import { Cookie, CookieBlock } from '@dxtmisha/functional'

// Create cookie instance
const userPrefs = new Cookie('user-preferences')

// Initially cookies are allowed
CookieBlock.set(false)
userPrefs.set({ theme: 'dark', language: 'en' })
console.log('Stored preferences:', userPrefs.get()) // { theme: 'dark', language: 'en' }

// Block cookies
CookieBlock.set(true)

// Cookie operations are now prevented
userPrefs.set({ theme: 'light', language: 'es' }) // This won't actually set the cookie
console.log('Preferences after blocking:', userPrefs.get()) // Still { theme: 'dark', language: 'en' }

// Allow cookies again
CookieBlock.set(false)
userPrefs.set({ theme: 'light', language: 'es' }) // Now this works
console.log('Updated preferences:', userPrefs.get()) // { theme: 'light', language: 'es' }
```

## Practical Examples

### GDPR Cookie Consent

```javascript
class CookieConsent {
  constructor() {
    this.consentCookie = new Cookie('cookie-consent')
  }

  showConsentBanner() {
    const consent = this.consentCookie.get()

    if (consent === undefined) {
      CookieBlock.set(true) // Block until consent
      this.displayBanner()
      return 'banner-shown'
    } else {
      CookieBlock.set(!consent.accepted)
      return consent.accepted ? 'cookies-allowed' : 'cookies-blocked'
    }
  }

  acceptCookies() {
    this.consentCookie.set({
      accepted: true,
      timestamp: Date.now()
    }, { age: 365 * 24 * 60 * 60 })

    CookieBlock.set(false)
    this.hideBanner()
  }

  rejectCookies() {
    this.consentCookie.set({
      accepted: false,
      timestamp: Date.now()
    }, { age: 365 * 24 * 60 * 60 })

    CookieBlock.set(true)
    this.hideBanner()
  }
}

// Usage
const consent = new CookieConsent()
const status = consent.showConsentBanner()
```

### Privacy Settings Management

```javascript
class PrivacyManager {
  constructor() {
    this.privacySettings = new Cookie('privacy-settings')
  }

  updatePrivacySettings(settings) {
    this.privacySettings.set(settings, { age: 365 * 24 * 60 * 60 })

    const shouldBlock = !(settings.functional || settings.analytics || settings.marketing)
    CookieBlock.set(shouldBlock)
  }

  hasConsentFor(category) {
    const settings = this.privacySettings.get({ essential: true })
    return settings[category] || false
  }

  acceptAll() {
    this.updatePrivacySettings({
      essential: true,
      functional: true,
      analytics: true,
      marketing: true
    })
  }

  rejectAll() {
    this.updatePrivacySettings({
      essential: true,
      functional: false,
      analytics: false,
      marketing: false
    })
  }
}

// Usage
const privacy = new PrivacyManager()
privacy.acceptAll()
console.log('Analytics allowed:', privacy.hasConsentFor('analytics'))
```

### Application Initialization

```javascript
class AppInitializer {
  static initializeCookieCompliance() {
    const consent = new Cookie('cookie-consent')
    const consentData = consent.get()

    if (!consentData) {
      CookieBlock.set(true) // First visit
      return 'pending-consent'
    } else {
      CookieBlock.set(!consentData.accepted)
      return consentData.accepted ? 'cookies-allowed' : 'cookies-blocked'
    }
  }
}

// Application startup
document.addEventListener('DOMContentLoaded', () => {
  const status = AppInitializer.initializeCookieCompliance()
  console.log('Cookie status:', status)
})
```

The CookieBlock class helps ensure compliance with GDPR, CCPA, and other privacy laws by providing a foundation for implementing privacy-compliant cookie management.
