import { Resource } from "../resource.js";
import { logger } from "../util/logger.js";
import { createStripeClient, isStripeConflictError } from "./client.js";
/**
 * Create and manage Stripe entitlements features
 *
 * @example
 * // Create a basic feature for API access
 * const apiFeature = await EntitlementsFeature("api-access", {
 *   name: "API Access",
 *   lookupKey: "api_access_v1",
 *   metadata: {
 *     tier: "premium",
 *     category: "api"
 *   }
 * });
 *
 * @example
 * // Create a feature for advanced analytics
 * const analyticsFeature = await EntitlementsFeature("advanced-analytics", {
 *   name: "Advanced Analytics",
 *   lookupKey: "analytics_advanced",
 *   metadata: {
 *     tier: "enterprise",
 *     category: "analytics"
 *   }
 * });
 *
 * @example
 * // Create a feature for custom integrations
 * const integrationsFeature = await EntitlementsFeature("custom-integrations", {
 *   name: "Custom Integrations",
 *   lookupKey: "integrations_custom",
 *   metadata: {
 *     tier: "enterprise",
 *     category: "integrations",
 *     limit: "unlimited"
 *   }
 * });
 */
export const EntitlementsFeature = Resource("stripe::EntitlementsFeature", async function (_id, props) {
    const stripe = await createStripeClient({ apiKey: props.apiKey });
    if (this.phase === "delete") {
        return this.destroy();
    }
    try {
        let feature;
        if (this.phase === "update" && this.output?.id) {
            const updateParams = {
                name: props.name,
                metadata: props.metadata,
            };
            feature = await stripe.entitlements.features.update(this.output.id, updateParams);
        }
        else {
            const createParams = {
                name: props.name,
                metadata: props.metadata,
            };
            if (props.lookupKey) {
                createParams.lookup_key = props.lookupKey;
            }
            try {
                feature = await stripe.entitlements.features.create(createParams);
            }
            catch (error) {
                if (isStripeConflictError(error) &&
                    (props.adopt ?? this.scope.adopt)) {
                    const existingFeatures = await stripe.entitlements.features.list({
                        lookup_key: props.lookupKey,
                        limit: 1,
                    });
                    if (existingFeatures.data.length > 0) {
                        const existingFeature = existingFeatures.data[0];
                        const updateParams = {
                            name: props.name,
                            metadata: props.metadata,
                        };
                        feature = await stripe.entitlements.features.update(existingFeature.id, updateParams);
                    }
                    else {
                        throw error;
                    }
                }
                else {
                    throw error;
                }
            }
        }
        return {
            id: feature.id,
            object: feature.object,
            name: feature.name,
            lookupKey: feature.lookup_key || undefined,
            metadata: feature.metadata || undefined,
            livemode: feature.livemode,
        };
    }
    catch (error) {
        logger.error("Error creating/updating entitlements feature:", error);
        throw error;
    }
});
//# sourceMappingURL=entitlements-feature.js.map