# @wix/consent-policy-manager

A type-safe Wix Consent Policy SDK for managing site visitor cookie preferences and 3rd-party data transfers for GDPR and CCPA compliance. This package provides a browser-only interface that proxies to the global `window.consentPolicyManager` methods with full TypeScript support.

## Installation

```bash
npm install @wix/consent-policy-manager
```

## Quick Start

```typescript
import {
  getCurrentConsentPolicy,
  setConsentPolicy,
  resetConsentPolicy,
  onConsentPolicyChanged,
} from "@wix/consent-policy-manager";

// Get current consent policy
const policyDetails = getCurrentConsentPolicy();
if (policyDetails) {
  console.log("Current policy:", policyDetails.policy);
  console.log("Is default policy:", policyDetails.defaultPolicy);
  console.log("Created date:", policyDetails.createdDate);
} else {
  console.log("No consent policy available");
}

// Set a new consent policy
const newPolicy = {
  essential: true,      // Always true - required for Wix websites
  functional: true,     // Remember user settings
  analytics: false,     // Analytics cookies (Google Analytics, etc.)
  advertising: false,   // Advertising cookies and pixels
  dataToThirdParty: false, // Data transfer to 3rd parties
};

const result = await setConsentPolicy(newPolicy);
if (result) {
  console.log("Policy updated:", result);
} else {
  console.log("Failed to update policy");
}

// Reset to default policy
const resetSuccess = resetConsentPolicy();
if (resetSuccess) {
  console.log("Policy reset to default");
} else {
  console.log("Failed to reset policy or no policy to reset");
}

// Listen for consent policy changes
onConsentPolicyChanged((event) => {
  console.log("Consent policy changed!");
  console.log("New policy:", event.policy);
  console.log("Is default policy:", event.defaultPolicy);
});

```

## Features

- ✅ **Type-safe** - Full TypeScript support for all consent policy operations
- ✅ **GDPR/CCPA compliant** - Helps manage visitor privacy preferences
- ✅ **Browser-only** - Works in Wix applications where `window.consentPolicyManager` is available
- ✅ **Zero dependencies** - Lightweight proxy implementation
- ✅ **Silent failure** - Graceful handling of missing dependencies with console warnings
- ✅ **Event-driven** - Listen for consent policy changes in real-time
- ✅ **Comprehensive API** - Full access to consent policy, headers, and event publishing

## API Reference

### `getCurrentConsentPolicy()`

Gets the site visitor's current consent policy regarding allowed cookies and 3rd-party data transfers.

**Returns:** `PolicyDetails | undefined`

```typescript
const policyDetails = getCurrentConsentPolicy();
if (policyDetails) {
  // {
  //   defaultPolicy: false,
  //   policy: {
  //     essential: true,
  //     functional: true,
  //     analytics: true,
  //     advertising: false,
  //     dataToThirdParty: false
  //   },
  //   createdDate: Date
  // }
}
```

### `setConsentPolicy(policy)`

Sets the current site visitor's consent policy regarding allowed cookies and data transfer to 3rd parties.

**Parameters:**
- `policy` - Policy object with boolean values for each consent type

**Returns:** `Promise<PolicyDetails | undefined>`

```typescript
const policy = {
  essential: true,        // Required - always true
  functional: true,       // Optional - user preference cookies
  analytics: false,       // Optional - analytics tracking
  advertising: false,     // Optional - advertising cookies
  dataToThirdParty: false // Optional - 3rd party data transfer
};

const result = await setConsentPolicy(policy);
if (result) {
  console.log("Policy set successfully:", result);
}
```

### `resetConsentPolicy()`

Removes the current policy from the site visitor's browser and resets to the default site policy.

**Returns:** `boolean | undefined`

```typescript
const success = resetConsentPolicy();
if (success) {
  console.log("Policy reset successfully");
}
```

### `onConsentPolicyChanged(handler)`

Registers a callback function that is triggered when a site visitor's consent policy is changed using `setConsentPolicy()` or reset using `resetConsentPolicy()`.

**Parameters:**
- `handler` - Callback function that receives the consent policy change event

**Returns:** `void`

```typescript
onConsentPolicyChanged((event) => {
  console.log("Consent policy changed!");
  console.log("New policy:", event.policy);
  console.log("Is default policy:", event.defaultPolicy);
  
  // Example: Update analytics based on new policy
  if (event.policy.analytics) {
    // Enable analytics tracking
  } else {
    // Disable analytics tracking
  }
});

// Example event object:
// {
//   "defaultPolicy": false,
//   "policy": {
//     "essential": true,
//     "functional": false,
//     "analytics": true,
//     "advertising": false,
//     "dataToThirdParty": false
//   }
// }
```


## Policy Types

### Policy

The consent policy settings object:

```typescript
interface Policy {
  essential: boolean;      // Always true - required cookies
  functional: boolean;     // User preference cookies
  analytics: boolean;      // Analytics tracking cookies
  advertising: boolean;    // Advertising and marketing cookies
  dataToThirdParty: boolean; // 3rd party data transfer consent
}
```

### PolicyDetails

Complete consent policy information:

```typescript
interface PolicyDetails {
  defaultPolicy: boolean;  // Whether using default site policy
  policy: Policy;         // The actual policy settings
  createdDate?: Date;     // When policy was set (optional)
}
```
