import { Resource } from "../resource.js";
import { logger } from "../util/logger.js";
import { createStripeClient, isStripeConflictError } from "./client.js";
/**
 * Create and manage Stripe files for uploads
 *
 * @example
 * // Upload a dispute evidence file
 * const disputeEvidence = await File("dispute-evidence", {
 *   file: await fs.promises.readFile("./evidence.pdf"),
 *   purpose: "dispute_evidence"
 * });
 *
 * @example
 * // Upload an identity document
 * const identityDocument = await File("identity-doc", {
 *   file: await fs.promises.readFile("./passport.jpg"),
 *   purpose: "identity_document"
 * });
 *
 * @example
 * // Upload a business logo with file link
 * const businessLogo = await File("business-logo", {
 *   file: await fs.promises.readFile("./logo.png"),
 *   purpose: "business_logo",
 *   fileLink: {
 *     create: true,
 *     expiresAt: Math.floor(Date.now() / 1000) + 86400,
 *     metadata: {
 *       brand: "company_logo"
 *     }
 *   }
 * });
 */
export const File = Resource("stripe::File", async function (_id, props) {
    const adopt = props.adopt ?? this.scope.adopt;
    const stripe = await createStripeClient({ apiKey: props.apiKey });
    if (this.phase === "delete") {
        return this.destroy();
    }
    try {
        let file;
        if (this.phase === "update" && this.output?.id) {
            file = await stripe.files.retrieve(this.output.id);
        }
        else {
            try {
                file = await stripe.files.create({
                    file: props.file,
                    purpose: props.purpose,
                });
            }
            catch (error) {
                if (isStripeConflictError(error) && adopt) {
                    throw new Error("File adoption is not supported - files are immutable and cannot be adopted");
                }
                else {
                    throw error;
                }
            }
        }
        return {
            id: file.id,
            object: file.object,
            created: file.created,
            expiresAt: file.expires_at || undefined,
            filename: file.filename || undefined,
            links: file.links
                ? {
                    object: file.links.object,
                    data: file.links.data.map((link) => ({
                        id: link.id,
                        object: link.object,
                        created: link.created,
                        expired: link.expired,
                        expiresAt: link.expires_at || undefined,
                        file: typeof link.file === "string" ? link.file : link.file.id,
                        livemode: link.livemode,
                        metadata: link.metadata,
                        url: link.url || undefined,
                    })),
                    hasMore: file.links.has_more,
                    url: file.links.url,
                }
                : undefined,
            purpose: file.purpose,
            size: file.size,
            title: file.title || undefined,
            type: file.type || undefined,
            url: file.url || undefined,
            livemode: true,
        };
    }
    catch (error) {
        logger.error("Error creating/retrieving file:", error);
        throw error;
    }
});
//# sourceMappingURL=file.js.map