import { LightningAddress } from "@getalby/lightning-tools";
import { v4 } from "uuid";
import { E as ErrorCode } from "./errorCodes-E8kmN1h5.js";
import { l as logger, p as prisma, a as safeStringify } from "./i18n-49-r55rr.js";
import z from "zod";
const albyCredentialKeysSchema = z.object({
  account_id: z.string(),
  account_email: z.string(),
  account_lightning_address: z.string(),
  webhook_endpoint_id: z.string(),
  webhook_endpoint_secret: z.string()
});
const log = logger.getSubLogger({ prefix: ["payment-service:alby"] });
class PaymentService {
  constructor(credentials) {
    const keyParsing = albyCredentialKeysSchema.safeParse(credentials.key);
    if (keyParsing.success) {
      this.credentials = keyParsing.data;
    } else {
      this.credentials = null;
    }
  }
  async create(payment, bookingId) {
    try {
      const booking = await prisma.booking.findUnique({
        select: {
          uid: true,
          title: true
        },
        where: {
          id: bookingId
        }
      });
      if (!booking || !this.credentials?.account_lightning_address) {
        throw new Error("Alby: Booking or Lightning address not found");
      }
      const uid = v4();
      const lightningAddress = new LightningAddress(this.credentials.account_lightning_address);
      await lightningAddress.fetch();
      const invoice = await lightningAddress.requestInvoice({
        satoshi: payment.amount,
        payerdata: {
          appId: "cal.com",
          referenceId: uid
        }
      });
      console.log("Created invoice", invoice, uid);
      const paymentData = await prisma.payment.create({
        data: {
          uid,
          app: {
            connect: {
              slug: "alby"
            }
          },
          booking: {
            connect: {
              id: bookingId
            }
          },
          amount: payment.amount,
          externalId: invoice.paymentRequest,
          currency: payment.currency,
          data: Object.assign(
            {},
            { invoice: { ...invoice, isPaid: false } }
          ),
          fee: 0,
          refunded: false,
          success: false
        }
      });
      if (!paymentData) {
        throw new Error();
      }
      return paymentData;
    } catch (error) {
      log.error("Alby: Payment could not be created", bookingId, safeStringify(error));
      throw new Error(ErrorCode.PaymentCreationFailure);
    }
  }
  async update() {
    throw new Error("Method not implemented.");
  }
  async refund() {
    throw new Error("Method not implemented.");
  }
  async collectCard(_payment, _bookingId, _bookerEmail, _paymentOption) {
    throw new Error("Method not implemented");
  }
  chargeCard(_payment, _bookingId) {
    throw new Error("Method not implemented.");
  }
  getPaymentPaidStatus() {
    throw new Error("Method not implemented.");
  }
  getPaymentDetails() {
    throw new Error("Method not implemented.");
  }
  afterPayment(_event, _booking, _paymentData) {
    return Promise.resolve();
  }
  deletePayment(_paymentId) {
    return Promise.resolve(false);
  }
  isSetupAlready() {
    return !!this.credentials;
  }
}
export {
  PaymentService
};
