import { PropertyPath } from '../../../utils/property-path-utils'
import * as z from '../../../z'
import * as json from '../../common/json-schema'

const { zuiKey } = z

export const zodTupleToJsonTuple = (
  zodTuple: z.ZodTuple,
  toSchema: (x: z.ZodType, path: PropertyPath) => json.Schema,
  path: PropertyPath
): json.TupleSchema => {
  const schema: json.TupleSchema = {
    type: 'array',
    description: zodTuple.description,
    items: zodTuple._def.items.map((item, index) => toSchema(item, path.withIndexType('number', index))),
  }

  if (zodTuple._def[zuiKey]) {
    schema[zuiKey] = zodTuple._def[zuiKey]
  }

  if (zodTuple._def.rest) {
    schema.additionalItems = toSchema(zodTuple._def.rest, path.withIndexType('number'))
  }

  return schema
}
