import { Resource } from "../resource.js";
import { logger } from "../util/logger.js";
import { createStripeClient, handleStripeDeleteError, isStripeConflictError, } from "./client.js";
/**
 * Create and manage Stripe cards attached to customers
 *
 * @example
 * // Create a card using raw card details
 * const customerCard = await Card("customer-card", {
 *   customer: "cus_xyz123",
 *   number: "4242424242424242",
 *   expMonth: 12,
 *   expYear: 2025,
 *   cvc: "123",
 *   name: "John Doe",
 *   addressLine1: "123 Main St",
 *   addressCity: "San Francisco",
 *   addressState: "CA",
 *   addressZip: "94105",
 *   addressCountry: "US"
 * });
 *
 * @example
 * // Create a card using a token
 * const tokenCard = await Card("token-card", {
 *   customer: "cus_xyz123",
 *   source: "tok_visa",
 *   metadata: {
 *     type: "primary",
 *     source: "mobile_app"
 *   }
 * });
 *
 * @example
 * // Create a card for international customer
 * const internationalCard = await Card("international-card", {
 *   customer: "cus_international456",
 *   number: "4000000000000002",
 *   expMonth: 6,
 *   expYear: 2026,
 *   name: "Maria Garcia",
 *   addressLine1: "Calle Principal 123",
 *   addressCity: "Madrid",
 *   addressCountry: "ES",
 *   addressZip: "28001",
 *   metadata: {
 *     region: "europe",
 *     currency_preference: "eur"
 *   }
 * });
 */
export const Card = Resource("stripe::Card", async function (_id, props) {
    const stripe = await createStripeClient({ apiKey: props.apiKey });
    if (this.phase === "delete") {
        try {
            if (this.output?.id && this.output?.customer) {
                await stripe.customers.deleteSource(this.output.customer, this.output.id);
            }
        }
        catch (error) {
            handleStripeDeleteError(error, "Card", this.output?.id);
        }
        return this.destroy();
    }
    try {
        let card;
        if (this.phase === "update" && this.output?.id) {
            const updateParams = {
                name: props.name,
                address_city: props.addressCity,
                address_country: props.addressCountry,
                address_line1: props.addressLine1,
                address_line2: props.addressLine2,
                address_state: props.addressState,
                address_zip: props.addressZip,
                exp_month: props.expMonth?.toString(),
                exp_year: props.expYear?.toString(),
                metadata: props.metadata,
            };
            card = (await stripe.customers.updateSource(props.customer, this.output.id, updateParams));
        }
        else {
            const createParams = {
                metadata: props.metadata,
            };
            if (props.source) {
                createParams.source = props.source;
            }
            else if (props.number) {
                createParams.source = {
                    object: "card",
                    number: props.number,
                    exp_month: props.expMonth,
                    exp_year: props.expYear,
                    cvc: props.cvc,
                    name: props.name,
                    address_city: props.addressCity,
                    address_country: props.addressCountry,
                    address_line1: props.addressLine1,
                    address_line2: props.addressLine2,
                    address_state: props.addressState,
                    address_zip: props.addressZip,
                    currency: props.currency,
                    default_for_currency: props.defaultForCurrency,
                };
            }
            try {
                card = (await stripe.customers.createSource(props.customer, createParams));
            }
            catch (error) {
                if (isStripeConflictError(error) &&
                    (props.adopt ?? this.scope.adopt)) {
                    throw new Error("Card adoption is not supported - cards cannot be uniquely identified for adoption");
                }
                else {
                    throw error;
                }
            }
        }
        return {
            id: card.id,
            object: card.object,
            customer: props.customer,
            brand: card.brand,
            country: card.country || undefined,
            dynamicLast4: card.dynamic_last4 || undefined,
            expMonth: card.exp_month,
            expYear: card.exp_year,
            fingerprint: card.fingerprint || undefined,
            funding: card.funding,
            last4: card.last4,
            name: card.name || undefined,
            addressCity: card.address_city || undefined,
            addressCountry: card.address_country || undefined,
            addressLine1: card.address_line1 || undefined,
            addressLine2: card.address_line2 || undefined,
            addressState: card.address_state || undefined,
            addressZip: card.address_zip || undefined,
            currency: card.currency || undefined,
            defaultForCurrency: card.default_for_currency || undefined,
            metadata: card.metadata || undefined,
            tokenizationMethod: card.tokenization_method || undefined,
        };
    }
    catch (error) {
        logger.error("Error creating/updating card:", error);
        throw error;
    }
});
//# sourceMappingURL=card.js.map