import { Resource } from "../resource.js";
import { logger } from "../util/logger.js";
import { createStripeClient, handleStripeDeleteError, isStripeConflictError, } from "./client.js";
/**
 * Create and manage Stripe tax rates
 *
 * @example
 * // Create a basic sales tax rate
 * const salesTax = await TaxRate("ca-sales-tax", {
 *   displayName: "CA Sales Tax",
 *   percentage: 8.5,
 *   inclusive: false,
 *   active: true,
 *   country: "US",
 *   state: "CA",
 *   description: "California sales tax",
 *   taxType: "sales_tax"
 * });
 *
 * @example
 * // Create a VAT tax rate
 * const vatTax = await TaxRate("uk-vat", {
 *   displayName: "UK VAT",
 *   percentage: 20.0,
 *   inclusive: true,
 *   active: true,
 *   country: "GB",
 *   description: "United Kingdom Value Added Tax",
 *   taxType: "vat",
 *   metadata: {
 *     region: "europe",
 *     tax_authority: "hmrc"
 *   }
 * });
 *
 * @example
 * // Create a city tax rate
 * const cityTax = await TaxRate("nyc-tax", {
 *   displayName: "NYC Local Tax",
 *   percentage: 4.5,
 *   inclusive: false,
 *   active: true,
 *   country: "US",
 *   state: "NY",
 *   jurisdiction: "New York City",
 *   description: "New York City local sales tax",
 *   taxType: "sales_tax",
 *   metadata: {
 *     locality: "nyc",
 *     tax_level: "city"
 *   }
 * });
 */
export const TaxRate = Resource("stripe::TaxRate", async function (_id, props) {
    const adopt = props.adopt ?? this.scope.adopt;
    const stripe = await createStripeClient({ apiKey: props.apiKey });
    if (this.phase === "delete") {
        try {
            if (this.output?.id) {
                await stripe.taxRates.update(this.output.id, { active: false });
            }
        }
        catch (error) {
            handleStripeDeleteError(error, "TaxRate", this.output?.id);
        }
        return this.destroy();
    }
    try {
        let taxRate;
        if (this.phase === "update" && this.output?.id) {
            taxRate = await stripe.taxRates.update(this.output.id, {
                active: props.active,
                country: props.country,
                description: props.description,
                display_name: props.displayName,
                jurisdiction: props.jurisdiction,
                metadata: props.metadata,
                state: props.state,
                tax_type: props.taxType,
            });
        }
        else {
            try {
                taxRate = await stripe.taxRates.create({
                    display_name: props.displayName,
                    percentage: props.percentage,
                    inclusive: props.inclusive,
                    active: props.active,
                    country: props.country,
                    description: props.description,
                    jurisdiction: props.jurisdiction,
                    metadata: props.metadata,
                    state: props.state,
                    tax_type: props.taxType,
                });
            }
            catch (error) {
                if (isStripeConflictError(error) && adopt) {
                    throw new Error("TaxRate adoption is not supported - tax rates cannot be uniquely identified for adoption");
                }
                else {
                    throw error;
                }
            }
        }
        return {
            id: taxRate.id,
            object: taxRate.object,
            displayName: taxRate.display_name,
            percentage: taxRate.percentage,
            inclusive: taxRate.inclusive,
            active: taxRate.active,
            country: taxRate.country || undefined,
            description: taxRate.description || undefined,
            jurisdiction: taxRate.jurisdiction || undefined,
            metadata: taxRate.metadata || undefined,
            state: taxRate.state || undefined,
            taxType: taxRate.tax_type || undefined,
            created: taxRate.created,
            livemode: taxRate.livemode,
        };
    }
    catch (error) {
        logger.error("Error creating/updating tax rate:", error);
        throw error;
    }
});
//# sourceMappingURL=tax-rate.js.map