/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */

import { EntryPoints } from 'N/types'
import runtime = require('N/runtime')
import log = require('N/log')
import search = require('N/search')

export const getInputData: EntryPoints.MapReduce.getInputData = () => {
  try {
    const columns = ['internalid']
    return search.create({
      type: search.Type.LOCATION,
      columns,
    })
  } catch (error) {
    log.error({ title: 'getInputData error', details: error })
    return {}
  }
}

export const map: EntryPoints.MapReduce.map = (context) => {
  context.errors.iterator().each((key, error, executionNo) => {
    log.error({
      title: 'map error',
      details: error,
    })
    return true
  })
  log.debug({ title: 'map', details: 'map' })
  try {
  } catch (err) {
    log.error({ title: 'map error', details: err })
  }
}

export const reduce: EntryPoints.MapReduce.reduce = (context) => {
  log.debug({ title: 'reduce values', details: { values: context.values } })
  log.debug({ title: 'reduce key', details: { key: context.key } })
}

export const summarize: EntryPoints.MapReduce.summarize = (context) => {
  context.mapSummary.errors.iterator().each((key, error, executionNo) => {
    log.error({
      title: 'map (summarize stage) error',
      details: error,
    })
    return true
  })
}
