import type { IZodNull, IZodType, ZodNullDef, ParseInput, ParseReturnType } from '../../typings'
import { ZodBaseTypeImpl, addIssueToContext } from '../basetype'

export class ZodNullImpl extends ZodBaseTypeImpl<null, ZodNullDef> implements IZodNull {
  _parse(input: ParseInput): ParseReturnType<this['_output']> {
    const parsedType = this._getType(input)
    if (parsedType !== 'null') {
      const ctx = this._getOrReturnCtx(input)
      addIssueToContext(ctx, {
        code: 'invalid_type',
        expected: 'null',
        received: ctx.parsedType,
      })
      return { status: 'aborted' }
    }
    return { status: 'valid', value: input.data }
  }
  isEqual(schema: IZodType): boolean {
    return schema instanceof ZodNullImpl
  }
}
