import btcPayServer from "../../btcpay_server.app.mjs";
export default {
  key: "btcpay_server-create-payment-request",
  name: "Create Payment Request",
  description: "Generates a new payment request for a user. [See the documentation](https://docs.btcpayserver.org/API/Greenfield/v1/#operation/PaymentRequests_CreatePaymentRequest)",
  version: "0.0.2",
  annotations: {
    destructiveHint: false,
    openWorldHint: true,
    readOnlyHint: false,
  },
  type: "action",
  props: {
    btcPayServer,
    storeId: {
      propDefinition: [
        btcPayServer,
        "storeId",
      ],
    },
    amount: {
      type: "string",
      label: "Amount",
      description: "The amount of the payment request",
    },
    title: {
      type: "string",
      label: "Title",
      description: "The title of the payment request",
    },
    currency: {
      type: "string",
      label: "Currency",
      description: "The currency of the payment request. If empty, the store's default currency code will be used.",
      optional: true,
    },
    description: {
      type: "string",
      label: "Description",
      description: "The description of the payment request",
      optional: true,
    },
    email: {
      type: "string",
      label: "Email",
      description: "The email used in invoices generated by the payment request",
      optional: true,
    },
    expiryDate: {
      type: "string",
      label: "Expiry Date",
      description: "The expiry date of the payment request in ISO 8601 format.",
      optional: true,
    },
  },
  async run({ $ }) {
    const response = await this.btcPayServer.createPaymentRequest({
      $,
      storeId: this.storeId,
      data: {
        amount: this.amount,
        currency: this.currency,
        title: this.title,
        description: this.description,
        email: this.email,
        expiryDate: this.expiryDate && (Date.parse(this.expiryDate)) / 1000,
      },
    });
    $.export("$summary", `Successfully created payment request with ID: ${response.id}`);
    return response;
  },
};