# ARCAID SDK

A lightweight JavaScript SDK for integrating ARCAID into your web applications.

## Installation

### Option 1: CDN (Recommended)
Simply add the loader script to your HTML:

```html
<script src="https://cdn.jsdelivr.net/npm/arcaid-sdk@0.2.0/dist/arcaid-loader.min.obf.js"></script>
```

That's it! The loader will automatically download and initialize the SDK when needed.

### Option 2: NPM
```bash
npm install arcaid-sdk
```

Then import the loader:
```javascript
import 'arcaid-sdk/dist/arcaid-loader.min.obf.js';
```

## Usage

The loader will automatically manage the SDK lifecycle. You don't need to manually include or initialize the core SDK - the loader handles everything for you.

```javascript
// The loader exposes the global ARCAID object
window.ARCAID.init({
    // your configuration
});
```

## Development

For local development and debugging, run:
```bash
pnpm serve
```
This will start a local server at http://localhost:3100 serving the SDK files.

## Basic Usage

Here's a quick example of how to initialize the Arcaid SDK when including it directly via a `<script>` tag.

1.  **Include the `arcaid-loader.js` script in your HTML:**

    This script will handle loading the core SDK and make the `Arcaid` object available globally.

    ```html
    <!-- Adjust the path to where you host arcaid-loader.js -->
    <script src="path/to/arcaid-loader.js"></script>
    ```

2.  **Use the global `Arcaid` object to initialize and use the SDK:**

    Once the loader script is included, you can access `Arcaid` to initialize and interact with the SDK.

    ```javascript
    // This code would typically be in a <script> tag in your HTML, after arcaid-loader.js
    // or in your game's main JavaScript file that runs after the loader.

    async function mainArcaidIntegration() {
      try {
        // The Arcaid object is now available globally (window.Arcaid)
        if (!window.Arcaid || !window.Arcaid.init) {
          console.error("Arcaid SDK Loader not found or failed to initialize. Ensure arcaid-loader.js is loaded.");
          return;
        }

        // Initialize the SDK.
        // Provide your gameId if you have one. Other configurations can also be passed.
        // If running in an iframe on the Arcaid platform, some configs are auto-provided.
        const sdkInstance = await window.Arcaid.init({
          gameId: 'your-game-id', // Optional: if your game has a specific ID
          // coreSdkUrl: 'path/to/your/self-hosted/arcaid-core-sdk.js' // Optional: if self-hosting the core SDK
        });

        console.log("Arcaid SDK Initialized (loader).");

        // IMPORTANT: Wait for the SDK to be fully ready before making most calls.
        // This usually means it has received necessary configuration, like user session from the platform.
        await sdkInstance.ready();
        console.log("Arcaid SDK is fully ready for calls.");

        // Now you can use the SDK modules, for example, to get user state or balance.
        const userState = await sdkInstance.auth.getUserState();
        console.log("User State:", userState);

        if (userState.isLoggedIn) {
          // Example: Get balance for the default/primary token
          try {
            const balanceInfo = await sdkInstance.wallet.getUserBalance(); // No argument needed now
            console.log(`User Balance:`, balanceInfo.balance, balanceInfo.ticker, `(Token: ${balanceInfo.tokenAddress})`);
          } catch (balanceError) {
            console.warn(`Could not fetch user balance:`, balanceError.message);
          }
        }

        // You can also use other modules like multiplayer, stats, etc.
        // sdkInstance.multiplayer.createRoom(...);

      } catch (error) {
        console.error("Arcaid SDK Initialization or usage failed:", error);
        // Handle initialization errors appropriately
      }
    }

    // Call your integration function. 
    // Ensure this runs after the DOM is ready or arcaid-loader.js has had a chance to load.
    mainArcaidIntegration();
    ```

## Usage

```ts
import { something } from 'arcaid-sdk';
// ...
```

## Wallet Operations

### Get User Balance

To get a user's balance (typically for their primary or game-specific token), you can use the `getUserBalance` method available on the `wallet` module of the SDK instance. This is also shown in the Basic Usage example above.

```javascript
// Assuming sdkInstance is your initialized and ready Arcaid SDK instance
async function checkUserBalance(sdkInstance) {
  try {
    const balanceInfo = await sdkInstance.wallet.getUserBalance(); // No argument needed
    console.log('User Balance:', balanceInfo.balance);
    console.log('Token Ticker:', balanceInfo.ticker);
    console.log('Token Address:', balanceInfo.tokenAddress); // The platform returns which token this balance is for
    return balanceInfo;
  } catch (error) {
    console.error('Error fetching user balance:', error.message);
    // Handle the error appropriately
  }
}

// Example usage:
// mainArcaidIntegration function from Basic Usage would already show this.
// if (sdkInstance && sdkInstance.ready) { // Ensure sdkInstance is defined and has ready method
//    sdkInstance.ready().then(() => {
//        checkUserBalance(sdkInstance);
//    });
// }
```

## Scripts
- `npm run build` — Build the SDK
- `npm run test` — Run tests
- `npm run lint` — Lint code

## License
MIT 