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

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

import MorphologicalContourInterpolationNodeOptions from './morphological-contour-interpolation-node-options.js'
import MorphologicalContourInterpolationNodeResult from './morphological-contour-interpolation-node-result.js'

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

/**
 * Interpolates contours between slices.
 *
 * @param {Image} inputImage - The input image
 * @param {MorphologicalContourInterpolationNodeOptions} options - options object
 *
 * @returns {Promise<MorphologicalContourInterpolationNodeResult>} - result object
 */
async function morphologicalContourInterpolationNode(
  inputImage: Image,
  options: MorphologicalContourInterpolationNodeOptions = {}
) : Promise<MorphologicalContourInterpolationNodeResult> {

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

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

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

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

  // Options
  args.push('--memory-io')
  if (options.label) {
    args.push('--label', options.label.toString())

  }
  if (options.axis) {
    args.push('--axis', options.axis.toString())

  }
  if (options.noHeuristicAlignment) {
    options.noHeuristicAlignment && args.push('--no-heuristic-alignment')
  }
  if (options.noUseDistanceTransform) {
    options.noUseDistanceTransform && args.push('--no-use-distance-transform')
  }
  if (options.useCustomSlicePositions) {
    options.useCustomSlicePositions && args.push('--use-custom-slice-positions')
  }
  if (options.noUseExtrapolation) {
    options.noUseExtrapolation && args.push('--no-use-extrapolation')
  }
  if (options.useBallStructuringElement) {
    options.useBallStructuringElement && args.push('--use-ball-structuring-element')
  }
  if (options.labeledSliceIndicesAxis) {
    args.push('--labeled-slice-indices-axis', options.labeledSliceIndicesAxis.toString())

  }
  if (options.labeledSliceIndicesLabel) {
    args.push('--labeled-slice-indices-label', options.labeledSliceIndicesLabel.toString())

  }
  if (options.labeledSliceIndices) {
    if(options.labeledSliceIndices.length < 1) {
      throw new Error('"labeled-slice-indices" option must have a length > 1')
    }
    args.push('--labeled-slice-indices')

    options.labeledSliceIndices.forEach((value) => {
      args.push(value.toString())

    })
  }

  const pipelinePath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'pipelines', 'morphological-contour-interpolation')

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

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

export default morphologicalContourInterpolationNode
