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

export const RoundStepKind = 'HttpAction#Round'

/**
 * Formats the input as a number using fixed-point notation.
 */
export interface IRoundStep extends IHttpStep {
  kind: typeof RoundStepKind
  /**
   * The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive.
   * If this argument is omitted, it is treated as 0.
   * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed.
   */
  precision?: number
}

/**
 * Formats the input as a number using fixed-point notation.
 */
export class RoundStep extends HttpStep {
  override kind = RoundStepKind

  /**
   * The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive.
   * If this argument is omitted, it is treated as 0.
   * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed.
   */
  precision?: number

  /**
   * Creates the step from values.
   * @param precision The name of the variable to set
   */
  constructor(precision: number)

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

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

  constructor(precisionOrInit?: number | IRoundStep) {
    super()
    if (typeof precisionOrInit === 'object') {
      const typed = precisionOrInit as IRoundStep
      this.new(typed)
    } else if (typeof precisionOrInit === 'number') {
      this.new({
        kind: RoundStepKind,
        precision: precisionOrInit,
        enabled: true,
      })
    }
  }

  override new(init: IRoundStep): void {
    super.new(init)
    this.kind = RoundStepKind
    if (typeof init.precision === 'number') {
      this.precision = init.precision
    } else {
      this.precision = undefined
    }
  }

  checkValidity(): boolean {
    return true
  }

  override toJSON(): IRoundStep {
    const result: IRoundStep = {
      ...super.toJSON(),
      kind: RoundStepKind,
    }
    if (typeof this.precision === 'number') {
      result.precision = this.precision
    }
    return result
  }
}
