/* eslint-disable @typescript-eslint/unified-signatures */
import { HttpStep, IHttpStep } from '../../HttpStep.js'

export const DeleteVariableStepKind = 'HttpAction#DeleteVariable'

/**
 * A step that deletes a variable.
 */
export interface IDeleteVariableStep extends IHttpStep {
  kind: typeof DeleteVariableStepKind
  /**
   * The name of the variable to delete
   */
  name: string
}

export class DeleteVariableStep extends HttpStep {
  override kind = DeleteVariableStepKind

  /**
   * The name of the variable to delete
   */
  name = ''

  /**
   * Creates the step from values.
   * @param name The name of the variable to delete
   */
  constructor(name: string)

  /**
   * Creates the step from serialized schema.
   * @param init The serialized step values.
   */
  constructor(init: IDeleteVariableStep)

  /**
   * Creates an empty step.
   */
  constructor()

  constructor(nameOrInit?: string | IDeleteVariableStep) {
    super()
    if (!nameOrInit) {
      return
    }
    if (typeof nameOrInit === 'object') {
      const typed = nameOrInit as IDeleteVariableStep
      this.new(typed)
    } else if (typeof nameOrInit === 'string') {
      this.new({
        kind: DeleteVariableStepKind,
        name: nameOrInit,
        enabled: true,
      })
    }
  }

  override new(init: IDeleteVariableStep): void {
    super.new(init)
    this.kind = DeleteVariableStepKind
    this.name = init.name
  }

  checkValidity(): boolean {
    const { name } = this
    return !!name
  }

  override toJSON(): IDeleteVariableStep {
    const result: IDeleteVariableStep = {
      ...super.toJSON(),
      kind: DeleteVariableStepKind,
      name: this.name,
    }
    return result
  }
}
