import { HttpStep, IHttpStep } from '../../HttpStep.js'
import { ToBeTypes } from './ToBeAssertion.js'

export const NotToBeAssertionKind = 'HttpAction#NotToBeAssertion'

/**
 * An assertion that makes sure the input is not of a specific type (string, number, boolean, object, array).
 */
export interface INotToBeAssertion extends IHttpStep {
  kind: typeof NotToBeAssertionKind

  /**
   * The type to expect.
   */
  value?: ToBeTypes
}

/**
 * An assertion that makes sure the input is not of a specific type (string, number, boolean, object, array).
 */
export class NotToBeAssertion extends HttpStep {
  override kind = NotToBeAssertionKind

  /**
   * The type to expect.
   */
  value?: ToBeTypes

  constructor(valueOrInit?: INotToBeAssertion | ToBeTypes) {
    super()
    if (typeof valueOrInit === 'object') {
      const typed = valueOrInit as INotToBeAssertion
      this.new(typed)
    } else if (typeof valueOrInit === 'string') {
      this.new({
        kind: NotToBeAssertionKind,
        value: valueOrInit,
        enabled: true,
      })
    }
  }

  override new(init: INotToBeAssertion): void {
    super.new(init)
    this.kind = NotToBeAssertionKind
    this.value = init.value
  }

  checkValidity(): boolean {
    return !!this.value
  }

  override toJSON(): INotToBeAssertion {
    const result: INotToBeAssertion = {
      ...super.toJSON(),
      kind: NotToBeAssertionKind,
    }
    if (this.value) {
      result.value = this.value
    }
    return result
  }
}
