// src/index.ts
import axios from "axios";

// src/types/invoice.ts
var InvoiceStatus = /* @__PURE__ */ ((InvoiceStatus2) => {
  InvoiceStatus2["PENDING"] = "pending";
  InvoiceStatus2["CONFIRMED"] = "confirmed";
  InvoiceStatus2["EXPIRED"] = "expired";
  InvoiceStatus2["ERROR"] = "error";
  return InvoiceStatus2;
})(InvoiceStatus || {});

// src/types/token.ts
var SupportedToken = /* @__PURE__ */ ((SupportedToken2) => {
  SupportedToken2["USDT"] = "USDT";
  return SupportedToken2;
})(SupportedToken || {});
var TOKEN_ADDRESSES = {
  // [SupportedToken.USDC]: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyB7u6T',
  ["USDT" /* USDT */]: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"
};
var TOKEN_DECIMALS = {
  // [SupportedToken.USDC]: 6, // USDC has 6 decimal places
  ["USDT" /* USDT */]: 6
  // USDT has 6 decimal places
};
var TokenUtils = class {
  /**
   * Get the SPL token address from a token enum
   */
  static getTokenAddress(token) {
    return TOKEN_ADDRESSES[token];
  }
  /**
   * Get token enum from SPL address
   */
  static getTokenFromAddress(address) {
    for (const [token, addr] of Object.entries(TOKEN_ADDRESSES)) {
      if (addr === address) {
        return token;
      }
    }
    return null;
  }
  /**
   * Get the number of decimal places for a given token
   */
  static getTokenDecimals(token) {
    return TOKEN_DECIMALS[token] || 6;
  }
  /**
   * Check if a token is supported
   */
  static isTokenSupported(token) {
    return Object.values(SupportedToken).includes(token);
  }
  /**
   * Get the default token (USDT)
   */
  static getDefaultToken() {
    return "USDT" /* USDT */;
  }
};

// src/modules/payments.ts
var PaymentsModule = class {
  constructor(client) {
    this.client = client;
  }
  // --- Product Methods ---
  async createProduct(payload) {
    const requestPayload = { ...payload };
    if (requestPayload.token && !requestPayload.splTokenAddress) {
      requestPayload.splTokenAddress = TokenUtils.getTokenAddress(requestPayload.token);
    }
    if (requestPayload.price) {
      const token = requestPayload.token || "USDT" /* USDT */;
      const decimals = TokenUtils.getTokenDecimals(token);
      const priceValue = typeof requestPayload.price === "string" ? parseFloat(requestPayload.price) : requestPayload.price;
      const priceInBaseUnits = Math.round(priceValue * 10 ** decimals);
      requestPayload.price = priceInBaseUnits.toString();
    }
    const response = await this.client.post("/v1/products", requestPayload);
    return response.data;
  }
  async getProduct(productId) {
    const response = await this.client.get(`/v1/products/${productId}`);
    return response.data;
  }
  async listProducts() {
    const response = await this.client.get("/v1/products");
    return response.data;
  }
  async updateProduct(productId, payload) {
    const requestPayload = { ...payload };
    if (requestPayload.token && !requestPayload.splTokenAddress) {
      requestPayload.splTokenAddress = TokenUtils.getTokenAddress(requestPayload.token);
    }
    const response = await this.client.put(
      `/v1/products/${productId}`,
      requestPayload
    );
    return response.data;
  }
  async deleteProduct(productId) {
    await this.client.delete(`/v1/products/${productId}`);
  }
  // --- Invoice Methods ---
  async createInvoice(payload) {
    const requestPayload = { ...payload };
    if ("token" in requestPayload && !("splTokenAddress" in requestPayload)) {
      requestPayload.splTokenAddress = TokenUtils.getTokenAddress(requestPayload.token);
    }
    const response = await this.client.post("/v1/invoices", requestPayload);
    return response.data;
  }
  async getInvoice(invoiceId) {
    const response = await this.client.get(`/v1/invoices/${invoiceId}`);
    return response.data;
  }
  async listInvoices() {
    const response = await this.client.get("/v1/invoices");
    return response.data;
  }
  async deleteInvoice(invoiceId) {
    await this.client.delete(`/v1/invoices/${invoiceId}`);
  }
};

// src/index.ts
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,
  InvoiceStatus,
  SupportedToken,
  TOKEN_ADDRESSES,
  TOKEN_DECIMALS,
  TokenUtils
};
