# @zerohash-sdk/crypto-buy-js

A JavaScript SDK that enables frontend applications to seamlessly integrate with the Connect Crypto Buy product.

Connect Crypto Buy provides a secure, customizable flow for purchasing crypto assets directly within your application.

## Installation

### Via NPM (recommended)

```bash
npm install @zerohash-sdk/crypto-buy-js
```

```javascript
import { CryptoBuy } from '@zerohash-sdk/crypto-buy-js';
```

### Via CDN

```html
<script
  type="module"
  src="https://sdk-cdn.zerohash.com/crypto-buy-web/index.js"
></script>
```

Or import directly in your JavaScript code:

```javascript
import { CryptoBuy } from 'https://sdk-cdn.zerohash.com/crypto-buy-web/index.js';
```

## Getting Started

### 1. Import the CryptoBuy module

```javascript
import { CryptoBuy } from '@zerohash-sdk/crypto-buy-js';
```

### 2. Initialize and render the widget

```javascript
const cryptoBuy = new CryptoBuy({
  jwt: 'your-jwt-token', // Obtain this from your backend
  env: 'prod', // or 'cert' for testing
  theme: 'auto',
  onCompleted: ({ amountBought, assetSymbol }) => {
    console.log('Buy completed:', amountBought, assetSymbol);
  },
  onError: ({ errorCode, reason }) => {
    console.error('Error:', errorCode, 'Reason:', reason);
  },
  onClose: () => console.log('Widget closed'),
  onEvent: ({ type, data }) => console.log('Event:', type, data),
  onLoaded: () => console.log('Widget loaded and ready'),
});

await cryptoBuy.render(document.getElementById('crypto-buy-container'));

// Update configuration dynamically
cryptoBuy.updateConfig({ jwt: 'new-jwt-token' });

// Clean up when done
cryptoBuy.destroy();
```

### 2.1 Using TypeScript (optional)

```typescript
import { CryptoBuy, CryptoBuyConfig } from '@zerohash-sdk/crypto-buy-js';

const config: CryptoBuyConfig = {
  jwt: 'your-jwt-token',
  env: 'cert',
  theme: 'dark',
  onCompleted: ({ amountBought, assetSymbol }) => {
    console.log(`Bought ${amountBought} ${assetSymbol}`);
  },
};

const cryptoBuy = new CryptoBuy(config);
await cryptoBuy.render(document.getElementById('crypto-buy-container')!);
```

## API Reference

### Configuration

| Prop          | Type                                      | Required | Default  | Description                                       |
| ------------- | ----------------------------------------- | -------- | -------- | ------------------------------------------------- |
| `jwt`         | `string`                                  | Yes      | -        | JWT token for authentication with Connect         |
| `env`         | `"prod" \| "cert" \| "dev" \| "local"`    | No       | `"prod"` | Target environment                                |
| `theme`       | `"auto" \| "light" \| "dark"`             | No       | `"auto"` | Theme mode for the interface                      |
| `onCompleted` | `({ amountBought, assetSymbol }) => void` | No       | -        | Callback when the buy flow completes successfully |
| `onError`     | `({ errorCode, reason }) => void`         | No       | -        | Callback for error events                         |
| `onClose`     | `() => void`                              | No       | -        | Callback when the widget is closed                |
| `onEvent`     | `({ type, data }) => void`                | No       | -        | Callback for general events                       |
| `onLoaded`    | `() => void`                              | No       | -        | Callback when the widget is loaded and ready      |

### Methods

#### `render(container: HTMLElement): Promise<void>`

#### `updateConfig(config: Partial<CryptoBuyConfig>): void`

#### `destroy(): void`

#### `isRendered(): boolean`

#### `getConfig(): CryptoBuyConfig`

## Browser Support

- Chrome/Edge 90+
- Firefox 88+
- Safari 14+
- All modern browsers with Web Components support

## More Information & Support

For comprehensive documentation or support about Connect, visit our [Documentation Page](https://docs.zerohash.com/).
