// Generated file. To retain edits, remove this comment.
import { InterfaceTypes, runPipeline } from 'itk-wasm';
import { getPipelinesBaseUrl } from './pipelines-base-url.js';
import { getPipelineWorkerUrl } from './pipeline-worker-url.js';
import { getDefaultWebWorker } from './default-web-worker.js';
/**
 * Write DICOM segmentation object for overlapping segments.
 *
 * @param {Image} segImage - dicom segmentation object as an image
 * @param {JsonCompatible} metaInfo - JSON file containing the meta-information that describesthe measurements to be encoded. See DCMQI documentation for details.
 * @param {string} outputDicomFile - File name of the DICOM SEG object that will store theresult of conversion.
 * @param {WriteOverlappingSegmentationOptions} options - options object
 *
 * @returns {Promise<WriteOverlappingSegmentationResult>} - result object
 */
async function writeOverlappingSegmentation(segImage, metaInfo, outputDicomFile, options = { refDicomSeries: [], }) {
    const desiredOutputs = [
        { type: InterfaceTypes.BinaryFile, data: { path: outputDicomFile, data: new Uint8Array() } },
    ];
    const inputs = [
        { type: InterfaceTypes.Image, data: segImage },
        { type: InterfaceTypes.JsonCompatible, data: metaInfo },
    ];
    const args = [];
    // Inputs
    const segImageName = '0';
    args.push(segImageName);
    const metaInfoName = '1';
    args.push(metaInfoName);
    // Outputs
    const outputDicomFileName = outputDicomFile;
    args.push(outputDicomFileName);
    // Options
    args.push('--memory-io');
    if (options.refDicomSeries) {
        if (options.refDicomSeries.length < 1) {
            throw new Error('"ref-dicom-series" option must have a length > 1');
        }
        args.push('--ref-dicom-series');
        await Promise.all(options.refDicomSeries.map(async (value) => {
            let valueFile = value;
            if (value instanceof File) {
                const valueBuffer = await value.arrayBuffer();
                valueFile = { path: value.name, data: new Uint8Array(valueBuffer) };
            }
            inputs.push({ type: InterfaceTypes.BinaryFile, data: valueFile });
            const name = value instanceof File ? value.name : valueFile.path;
            args.push(name);
        }));
    }
    if (options.skipEmptySlices) {
        options.skipEmptySlices && args.push('--skip-empty-slices');
    }
    if (options.useLabelidAsSegmentnumber) {
        options.useLabelidAsSegmentnumber && args.push('--use-labelid-as-segmentnumber');
    }
    const pipelinePath = 'write-overlapping-segmentation';
    let workerToUse = options?.webWorker;
    if (workerToUse === undefined) {
        workerToUse = await getDefaultWebWorker();
    }
    const { webWorker: usedWebWorker, returnValue, stderr, outputs } = await runPipeline(pipelinePath, args, desiredOutputs, inputs, { pipelineBaseUrl: getPipelinesBaseUrl(), pipelineWorkerUrl: getPipelineWorkerUrl(), webWorker: workerToUse, noCopy: options?.noCopy });
    if (returnValue !== 0 && stderr !== "") {
        throw new Error(stderr);
    }
    const result = {
        webWorker: usedWebWorker,
        outputDicomFile: outputs[0]?.data,
    };
    return result;
}
export default writeOverlappingSegmentation;
//# sourceMappingURL=write-overlapping-segmentation.js.map