import debug from 'debug';
import { Readable } from 'stream';
import fetch from 'node-fetch';
import { FormData, File } from 'formdata-node';
import { FormDataEncoder } from 'form-data-encoder';
const error = debug('@modusjs/convert#html:error');
const info = debug('@modusjs/convert#html:info');
const trace = debug('@modusjs/convert#html:trace');
// From the Fixing Soil Health Tech Stack 2022 hackathon event:
// hosted HTML conversion for soil samples.  This POST returns
// a self-contained HTML report about the soil samples in the ModusResult.
export async function toHtml(mr, filename) {
    const form = new FormData();
    filename = filename || 'modus.json';
    filename = filename.replace('/', '_').replace('\\', '_'); // no path info allowed maybe
    form.set('files', new File([...JSON.stringify(mr)], filename));
    const encoder = new FormDataEncoder(form);
    const response = await fetch('https://soilapi.farmonapp.com/modus_json_to_html', {
        method: 'post',
        headers: encoder.headers,
        body: Readable.from(encoder.encode()),
    });
    if (response?.status && response.status >= 400) {
        error('ERROR: API request for HTML conversion failed.  Error was:', response);
        throw new Error('Network request failed for conversion to HTML');
    }
    const answer = await response.text();
    trace('Received response from HTML conversion api');
    return answer || '';
}
//# sourceMappingURL=html.js.map