# Fungies JavaScript SDK

The Fungies JavaScript SDK provides easy integration for Fungies checkout in your web applications.

## Installation

```bash
npm install @fungies/fungies-js
```

```bash
yarn add @fungies/fungies-js
```

```bash
pnpm add @fungies/fungies-js
```

## Usage

### JavaScript Initialization

```javascript
import { Fungies } from "@fungies/fungies-js";

// Initialize Fungies
Fungies.Initialize({
  // Optional: Disable data attribute support
  // enableDataAttributes: false
});

// Open a checkout programmatically
Fungies.Checkout.open({
  checkoutUrl: "https://store.example.com/checkout-element/my-checkout-id",
  settings: {
    mode: "overlay", // 'overlay' | 'embed'
    frameTarget: "target-element-id", // Optional, for embed mode
  },
});

// Close the checkout
Fungies.Checkout.close();
```

### HTML Data Attribute Support

You can also use HTML data attributes to create checkout buttons without writing JavaScript:

```html
<!-- Basic checkout button -->
<button
  data-fungies-checkout-url="https://store.example.com/checkout-element/my-checkout-id"
  data-fungies-mode="overlay"
>
  Open Checkout
</button>

<!-- Script tag to initialize the SDK -->
<script
  src="https://cdn.jsdelivr.net/npm/@fungies/fungies-js@CURRENT_VERSION"
  defer
  data-auto-init
></script>

<!-- Embed checkout with target element -->
<div id="target-element-id"></div>

<!-- Script tag to initialize and display checkout automatically -->
<script
  src="https://cdn.jsdelivr.net/npm/@fungies/fungies-js@CURRENT_VERSION"
  defer
  data-auto-init
  data-auto-display-checkout
  data-fungies-checkout-url="https://demo.dev.fungies.net/checkout-element/a85d8c76-bc30-48a3-9861-be68952a1eca"
  data-fungies-mode="embed"
  data-fungies-frame-target="target-element-id"
></script>
```

### Manual DOM Scanning

If you dynamically add checkout elements after page load, you can manually trigger a DOM scan:

```javascript
// Scan the page for new checkout elements
Fungies.ScanDOM();
```

## Data Attributes Reference

- `data-fungies-checkout-url`: (Required for new format) The checkout URL
- `data-fungies-mode`: (Optional) The checkout mode ('overlay' or 'embed')
- `data-fungies-frame-target`: (Optional) Target element ID for embed checkouts
- `data-fungies-discount-code`: (Optional) Discount code to be applied
- `data-fungies-customer-email`: (Optional) Customer email to pre-fill (deprecated, use data-fungies-billing-email)
- `data-fungies-billing-email`: (Optional) Billing email to pre-fill
- `data-fungies-billing-first-name`: (Optional) Billing first name to pre-fill
- `data-fungies-billing-last-name`: (Optional) Billing last name to pre-fill
- `data-fungies-billing-country`: (Optional) Billing country code (ISO 3166-1 alpha-2) to pre-fill
- `data-fungies-billing-state`: (Optional) Billing state/province to pre-fill
- `data-fungies-billing-city`: (Optional) Billing city to pre-fill
- `data-fungies-billing-zip-code`: (Optional) Billing zip/postal code to pre-fill
- `data-fungies-quantity`: (Optional) Default quantity for the checkout
- `data-fungies-items`: (Optional) JSON string of items to be purchased
- `data-fungies-custom-fields`: (Optional) JSON string of custom fields
- `data-fungies-button`: (Legacy format) Contains a checkout URL like "https://STORE_URL/checkout-element/:checkoutID" or "https://STORE_URL/overlay/:checkoutID"

## TypeScript Support

This package includes TypeScript definitions. You can take advantage of type checking and IntelliSense in supported editors:

```typescript
import { Fungies } from "@fungies/fungies-js";
import type { InitialCheckoutOpenOptions } from "@fungies/fungies-js";

// TypeScript will validate all parameters
const openOptions: InitialCheckoutOpenOptions = {
  checkoutUrl: "https://store.example.com/checkout-element/my-checkout-id",
  settings: {
    mode: "overlay",
    // frameTarget is optional and only used with embed mode
    frameTarget: "container-id",
  },
};

Fungies.Checkout.open(openOptions);
```
