import {
  PaymentsModule
} from "./chunk-IUPPETLC.mjs";
import {
  TOKEN_DECIMALS
} from "./chunk-EE7I5JHK.mjs";

// src/index.ts
import axios from "axios";
var BeepClient = class {
  constructor(options) {
    this.client = axios.create({
      baseURL: options.serverUrl || "https://api.beep.com",
      // Default to production URL
      headers: {
        "Authorization": `Bearer ${options.apiKey}`,
        "Content-Type": "application/json"
      }
    });
    this.payments = new PaymentsModule(this.client);
  }
  /**
   * The simplest way to request a payment. 
   * This one-stop method creates an invoice and prepares it for payment.
   * @param payload The details of the charge.
   * @returns A promise that resolves to the created invoice.
   */
  /**
   * Converts a decimal amount to base units based on token decimals
   * @param amount The decimal amount (e.g., 0.01)
   * @param token The token type
   * @returns The amount in base units as an integer (e.g., 10000 for 0.01 USDT)
   */
  convertAmountToBaseUnits(amount, token) {
    const decimals = TOKEN_DECIMALS[token] || 6;
    return Math.round(amount * Math.pow(10, decimals));
  }
  async requestPayment(payload) {
    const requestBody = {
      description: payload.description || "Payment request"
      // Provide a default description
    };
    const token = payload.token || "USDT" /* USDT */;
    if (payload.splTokenAddress) {
      requestBody.splTokenAddress = payload.splTokenAddress;
    } else {
      requestBody.token = token;
    }
    requestBody.amount = this.convertAmountToBaseUnits(payload.amount, token);
    if (payload.payerType) {
      requestBody.payerType = payload.payerType;
    }
    try {
      const response = await this.client.post("/v1/payment/request-payment", requestBody);
      if (!response.data) {
        throw new Error("No data returned from payment request");
      }
      return response.data;
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new Error(`Failed to request payment: ${errorMessage}`);
    }
  }
  // Example function
  async healthCheck() {
    try {
      const response = await this.client.get("/healthz");
      return response.data;
    } catch (error) {
      if (axios.isAxiosError(error)) {
        throw new Error(`API Error: ${error.response?.data?.message || error.message}`);
      } else {
        throw new Error("An unexpected error occurred.");
      }
    }
  }
};

export {
  BeepClient
};
