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

export const DeleteCookieStepKind = 'HttpAction#DeleteCookie'

/**
 * A step allowing to delete a cookie for the given configuration.
 * This step does not require an input.
 */
export interface IDeleteCookieStep extends IHttpStep {
  kind: typeof DeleteCookieStepKind
  /**
   * The URL associated with the cookie. When not set it uses the request URL or the request URL of the last redirect.
   */
  url?: string
  /**
   * Name of the cookie to remove.
   * When not set it removes all cookies for the URL.
   */
  name?: string
}

export class DeleteCookieStep extends HttpStep {
  override kind = DeleteCookieStepKind

  /**
   * The URL associated with the cookie. When not set it uses the request URL or the request URL of the last redirect.
   */
  url?: string

  /**
   * Name of the cookie to remove.
   * When not set it removes all cookies for the URL.
   */
  name?: string

  /**
   * @param name The name of the cookie
   * @param url Optionally, the URL of the cookie.
   */
  static byName(name: string, url?: string): DeleteCookieStep {
    return new DeleteCookieStep({
      kind: DeleteCookieStepKind,
      enabled: true,
      name,
      url,
    })
  }

  static byUrl(url: string): DeleteCookieStep {
    return new DeleteCookieStep({
      kind: DeleteCookieStepKind,
      enabled: true,
      url,
    })
  }

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

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

  constructor(init?: IDeleteCookieStep) {
    super()
    if (init) {
      this.new(init)
    }
  }

  override new(init: IDeleteCookieStep): void {
    super.new(init)
    this.kind = DeleteCookieStepKind
    if (init.url) {
      this.url = init.url
    } else {
      this.url = undefined
    }
    if (init.name) {
      this.name = init.name
    } else {
      this.name = undefined
    }
  }

  checkValidity(): boolean {
    return true
  }

  override toJSON(): IDeleteCookieStep {
    const result: IDeleteCookieStep = {
      ...super.toJSON(),
      kind: DeleteCookieStepKind,
    }
    if (this.url) {
      result.url = this.url
    }
    if (this.name) {
      result.name = this.name
    }
    return result
  }
}
