import { test } from 'vitest'
import * as z from '../index'
import * as assert from '../../assertions.utils.test'

test('generics', () => {
  async function stripOuter<TData extends z.ZodType>(schema: TData, data: unknown) {
    return z
      .object({
        nested: schema,
      })
      .transform((data) => {
        return data.nested!
      })
      .parse({ nested: data })
  }

  const result = stripOuter(z.object({ a: z.string() }), { a: 'asdf' })
  assert.assertEqual<typeof result, Promise<{ a: string }>>(true)
})

test('assignability', () => {
  const createSchemaAndParse = <K extends string, VS extends z.ZodString>(key: K, valueSchema: VS, data: unknown) => {
    const schema = z.object({
      [key]: valueSchema,
    })
    const parsed = schema.parse(data)
    const inferred: z.infer<z.ZodObject<{ [k in K]: VS }>> = parsed
    return inferred
  }
  createSchemaAndParse('foo', z.string(), { foo: '' })
})
