import { exportD1Database, } from "./d1-export.js";
import { importD1Database, } from "./d1-import.js";
/**
 * Clones a D1 database by exporting from a source database and importing to a target database.
 * Handles the full workflow:
 * 1. Export source database to get a signed URL
 * 2. Fetch SQL content from the signed URL
 * 3. Import SQL content into the target database
 *
 * @param api The CloudflareApi instance to use for requests
 * @param options Options including source and target database IDs
 * @returns An object containing results from both export and import operations
 * @throws Will throw an error if any part of the clone process fails
 */
export async function cloneD1Database(api, options) {
    const exportResult = await exportD1Database(api, {
        databaseId: options.sourceDatabaseId,
        ...options.exportOptions,
    });
    if (!exportResult.success) {
        throw new Error(`Failed to export source database: ${options.sourceDatabaseId}`);
    }
    // Step 2: Fetch SQL content from the signed URL
    const sqlResponse = await fetch(exportResult.signed_url);
    if (!sqlResponse.ok) {
        throw new Error(`Failed to fetch SQL content: ${sqlResponse.statusText}`);
    }
    const sqlData = await sqlResponse.text();
    // Step 3: Import SQL content into target database
    const importResult = await importD1Database(api, {
        databaseId: options.targetDatabaseId,
        sqlData,
        filename: exportResult.filename,
        ...options.importOptions,
    });
    if (!importResult.success) {
        throw new Error(`Failed to import to target database: ${options.targetDatabaseId}`);
    }
    return {
        exportResult,
        importResult,
        success: true,
    };
}
//# sourceMappingURL=d1-clone.js.map