'use strict';

var photoFilters = require('./photoFilters.js');
var photoOverlays = require('./photoOverlays.js');

/**
 * photoEditorActions.ts — Pure helper functions for photo export, download,
 * clipboard copy, and native share in PhotoEditor.
 */
/**
 * Render the current editor state to a JPEG Blob for export.
 */
async function getExportBlob(params) {
    const { image, selectedFilter, filterParams, filterIntensity, adjustments, cropRect, rotation, flipH, flipV, overlays } = params;
    const exportCanvas = document.createElement("canvas");
    photoFilters.renderToCanvas(exportCanvas, image, selectedFilter, { ...filterParams, intensity: filterIntensity }, adjustments, cropRect, rotation, flipH, flipV);
    if (overlays.length > 0) {
        const ctx = exportCanvas.getContext("2d");
        if (ctx)
            photoOverlays.renderAllOverlays(ctx, overlays, exportCanvas.width, exportCanvas.height);
    }
    return new Promise(res => exportCanvas.toBlob(b => res(b), "image/jpeg", 0.92));
}
/**
 * Trigger a browser download of the given Blob as "photo-edited.jpg".
 */
function handleDownloadBlob(blob) {
    const url = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = "photo-edited.jpg";
    document.body.appendChild(a);
    a.click();
    setTimeout(() => { document.body.removeChild(a); URL.revokeObjectURL(url); }, 200);
}
/**
 * Copy the current editor output to clipboard as PNG.
 */
async function handleCopyImageToClipboard(params) {
    const { image, selectedFilter, filterParams, filterIntensity, adjustments, cropRect, rotation, flipH, flipV, overlays } = params;
    try {
        // Clipboard API requires image/png
        const pngCanvas = document.createElement("canvas");
        photoFilters.renderToCanvas(pngCanvas, image, selectedFilter, { ...filterParams, intensity: filterIntensity }, adjustments, cropRect, rotation, flipH, flipV);
        if (overlays.length > 0) {
            const ctx = pngCanvas.getContext("2d");
            if (ctx)
                photoOverlays.renderAllOverlays(ctx, overlays, pngCanvas.width, pngCanvas.height);
        }
        const pngBlob = await new Promise(res => pngCanvas.toBlob(b => res(b), "image/png"));
        if (pngBlob) {
            await navigator.clipboard.write([new ClipboardItem({ "image/png": pngBlob })]);
        }
    }
    catch ( /* clipboard write not supported — silent fail */_a) { /* clipboard write not supported — silent fail */ }
}
/**
 * Use the Web Share API to share the exported image.
 * Falls back to download if sharing is not supported.
 */
async function handleNativeShare(blob) {
    var _a;
    const file = new File([blob], "photo-edited.jpg", { type: "image/jpeg" });
    if (navigator.share && ((_a = navigator.canShare) === null || _a === void 0 ? void 0 : _a.call(navigator, { files: [file] }))) {
        try {
            await navigator.share({ files: [file], title: "AudioVerse Photo", text: "" });
        }
        catch ( /* user cancelled or not supported */_b) { /* user cancelled or not supported */ }
    }
    else {
        // Fallback — just download
        handleDownloadBlob(blob);
    }
}

exports.getExportBlob = getExportBlob;
exports.handleCopyImageToClipboard = handleCopyImageToClipboard;
exports.handleDownloadBlob = handleDownloadBlob;
exports.handleNativeShare = handleNativeShare;
//# sourceMappingURL=photoEditorActions.js.map
