// Generated file. To retain edits, remove this comment.

import {
  Mesh,
  PolyData,
  InterfaceTypes,
  PipelineOutput,
  PipelineInput,
  runPipelineNode
} from 'itk-wasm'

import MeshToPolyDataNodeResult from './mesh-to-poly-data-node-result.js'

import path from 'path'
import { fileURLToPath } from 'url'

/**
 * Convert an itk::Mesh to an itk::PolyData
 *
 * @param {Mesh} mesh - Input mesh
 *
 * @returns {Promise<MeshToPolyDataNodeResult>} - result object
 */
async function meshToPolyDataNode(
  mesh: Mesh
) : Promise<MeshToPolyDataNodeResult> {

  const desiredOutputs: Array<PipelineOutput> = [
    { type: InterfaceTypes.PolyData },
  ]

  const inputs: Array<PipelineInput> = [
    { type: InterfaceTypes.Mesh, data: mesh },
  ]

  const args = []
  // Inputs
  const meshName = '0'
  args.push(meshName)

  // Outputs
  const polyDataName = '0'
  args.push(polyDataName)

  // Options
  args.push('--memory-io')

  const pipelinePath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'pipelines', 'mesh-to-poly-data')

  const {
    returnValue,
    stderr,
    outputs
  } = await runPipelineNode(pipelinePath, args, desiredOutputs, inputs)
  if (returnValue !== 0 && stderr !== "") {
    throw new Error(stderr)
  }

  const result = {
    polyData: outputs[0]?.data as PolyData,
  }
  return result
}

export default meshToPolyDataNode
