/**
 * Browser / edge entry point for @formepdf/core.
 *
 * Import as `@formepdf/core/browser` — no Node APIs, works in any
 * modern browser, edge runtime, or worker with WebAssembly support.
 *
 * Backed by the wasm-pack `--target bundler` build of pkg/, so the
 * WASM module is wired up implicitly by the consuming bundler at
 * module-load time (via `import * as wasm from './forme_bg.wasm'`
 * inside pkg/forme.js). Vite, esbuild, Webpack, Turbopack, and
 * Wrangler all handle this — there is no explicit init step.
 */
import { certify_pdf as wasmCertifyPdf, find_text_regions as wasmFindTextRegions, merge_pdfs as wasmMergePdfs, redact_pdf as wasmRedactPdf, redact_text as wasmRedactText, render_pdf as wasmRenderPdf, render_pdf_with_layout as wasmRenderPdfWithLayout, render_template_pdf as wasmRenderTemplatePdf, render_template_pdf_with_layout as wasmRenderTemplatePdfWithLayout, } from '../pkg/forme.js';
import { resolveFonts, resolveImages, extractDataFromPdf, } from './shared/browserHelpers.js';
// ── WASM initialization ────────────────────────────────────────────
//
// Kept as a no-op for backward compatibility. The bundler-target build
// instantiates the WASM at module-load time, so by the time anyone can
// invoke any of the exports below, the engine is already live.
//
// If you need an explicit `init(module)` driven by a `WebAssembly.Module`
// you imported yourself (e.g. Cloudflare Workers), import from
// `@formepdf/core/worker` instead — that entry is backed by the web
// target which supports manual instantiation.
/** @deprecated No-op under the bundler-target build. For explicit
 *  instantiation in Workers/edge, import from `@formepdf/core/worker`. */
export async function init(_module) {
    return;
}
// ── Render functions ───────────────────────────────────────────────
export async function renderPdf(json) {
    return wasmRenderPdf(json);
}
export async function renderPdfWithLayout(json) {
    const result = wasmRenderPdfWithLayout(json);
    return result;
}
export async function renderDocument(element, options) {
    const { serialize } = await import('@formepdf/react');
    const doc = serialize(element);
    if (options?.embedData !== undefined) {
        doc.embeddedData = JSON.stringify(options.embedData);
    }
    if (options?.flattenForms) {
        doc.flattenForms = true;
    }
    await Promise.all([resolveFonts(doc), resolveImages(doc)]);
    return renderPdf(JSON.stringify(doc));
}
export async function renderDocumentWithLayout(element, options) {
    const { serialize } = await import('@formepdf/react');
    const doc = serialize(element);
    if (options?.embedData !== undefined) {
        doc.embeddedData = JSON.stringify(options.embedData);
    }
    if (options?.flattenForms) {
        doc.flattenForms = true;
    }
    await Promise.all([resolveFonts(doc), resolveImages(doc)]);
    return renderPdfWithLayout(JSON.stringify(doc));
}
// ── Serialized document rendering ────────────────────────────────────
/**
 * Render a pre-serialized document object (from `serialize()`) to PDF,
 * resolving any HTTP image/font URLs to data URIs first.
 *
 * Use this when you have a serialized doc (e.g. from a web worker that
 * calls `serialize()` directly) and need image resolution without going
 * through the React element–based `renderDocument()`.
 */
export async function renderSerializedDoc(doc, options) {
    if (options?.embedData !== undefined) {
        doc.embeddedData = JSON.stringify(options.embedData);
    }
    if (options?.flattenForms) {
        doc.flattenForms = true;
    }
    await Promise.all([resolveFonts(doc), resolveImages(doc)]);
    return renderPdf(JSON.stringify(doc));
}
/**
 * Like `renderSerializedDoc` but also returns layout info for overlays.
 */
export async function renderSerializedDocWithLayout(doc, options) {
    if (options?.embedData !== undefined) {
        doc.embeddedData = JSON.stringify(options.embedData);
    }
    if (options?.flattenForms) {
        doc.flattenForms = true;
    }
    await Promise.all([resolveFonts(doc), resolveImages(doc)]);
    return renderPdfWithLayout(JSON.stringify(doc));
}
// ── Template rendering ──────────────────────────────────────────────
export async function renderTemplate(templateJson, dataJson) {
    return wasmRenderTemplatePdf(templateJson, dataJson);
}
export async function renderTemplateWithLayout(templateJson, dataJson) {
    const result = wasmRenderTemplatePdfWithLayout(templateJson, dataJson);
    return result;
}
// ── PDF certification ────────────────────────────────────────────────
export async function certifyPdf(pdfBytes, config) {
    return wasmCertifyPdf(pdfBytes, JSON.stringify(config));
}
/** @deprecated Use certifyPdf */
export const signPdf = certifyPdf;
// ── PDF redaction ────────────────────────────────────────────────────
export async function redactPdf(pdfBytes, regions) {
    return wasmRedactPdf(pdfBytes, JSON.stringify(regions));
}
// ── Text-search redaction ─────────────────────────────────────────────
/**
 * Find text regions matching patterns in a PDF.
 *
 * Searches PDF content streams for literal or regex patterns and returns
 * redaction regions (in web top-origin coordinates) for each match.
 */
export async function findTextRegions(pdfBytes, patterns) {
    const json = wasmFindTextRegions(pdfBytes, JSON.stringify(patterns));
    return JSON.parse(json);
}
/**
 * Redact text matching patterns from a PDF.
 *
 * Convenience wrapper: finds all text matching the patterns, then
 * applies coordinate-based redaction to each match.
 */
export async function redactText(pdfBytes, patterns) {
    return wasmRedactText(pdfBytes, JSON.stringify(patterns));
}
// ── PDF merging ──────────────────────────────────────────────────────
/**
 * Merge multiple PDF documents into a single PDF.
 *
 * @param pdfs - Array of PDF byte arrays to merge in order.
 * @returns The merged PDF as a Uint8Array.
 */
export async function mergePdfs(pdfs) {
    const base64Pdfs = pdfs.map((pdf) => btoa(Array.from(pdf, (b) => String.fromCharCode(b)).join('')));
    return wasmMergePdfs(JSON.stringify(base64Pdfs));
}
// ── Data extraction ─────────────────────────────────────────────────
/** Extract embedded JSON data from a Forme-generated PDF. */
export async function extractData(pdfBytes) {
    return extractDataFromPdf(pdfBytes);
}
