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

export const IncludesAssertionKind = 'HttpAction#IncludesAssertion'

/**
 * An assertion that makes sure the input includes the set value.
 */
export interface IIncludesAssertion extends IHttpStep {
  kind: typeof IncludesAssertionKind

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

/**
 * An assertion that makes sure the input includes the set value.
 */
export class IncludesAssertion extends HttpStep {
  override kind = IncludesAssertionKind

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

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

  override new(init: IIncludesAssertion): void {
    super.new(init)
    this.kind = IncludesAssertionKind
    this.value = init.value
  }

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

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