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

export const NotIncludesAssertionKind = 'HttpAction#NotIncludesAssertion'

/**
 * An assertion that makes sure the input does not include the set value.
 */
export interface INotIncludesAssertion extends IHttpStep {
  kind: typeof NotIncludesAssertionKind

  /**
   * The value to search for.
   */
  value?: string
}

/**
 * An assertion that makes sure the input does not include the set value.
 */
export class NotIncludesAssertion extends HttpStep {
  override kind = NotIncludesAssertionKind

  /**
   * The value to search for.
   */
  value?: string

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

  override new(init: INotIncludesAssertion): void {
    super.new(init)
    this.kind = NotIncludesAssertionKind
    this.value = init.value
  }

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

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