import type { JsonObject } from '../../../types/json.js'
export type { JsonObject }
export function pickDefinedBizParams(input: JsonObject): JsonObject {
  const output: JsonObject = {}

  for (const [key, value] of Object.entries(input)) {
    if (value === undefined || value === null) {
      continue
    }

    if (typeof value === 'string' && value.trim() === '') {
      continue
    }

    output[key] = value
  }

  return output
}

export function normalizeOpenApiLong(value: unknown): string | number {
  const text = String(value ?? '').trim()
  if (!text) {
    return ''
  }

  if (!/^\d+$/.test(text)) {
    return text
  }

  const parsed = Number(text)
  return Number.isSafeInteger(parsed) ? parsed : text
}

export function normalizeOpenApiInteger(value: unknown, fallback = 0): number {
  const parsed = Number(value)
  return Number.isInteger(parsed) ? parsed : fallback
}

export function normalizeOpenApiPositiveInteger(value: unknown, fallback = 1): number {
  const parsed = Number(value)
  return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback
}

export function normalizeStringList(value: unknown): string[] {
  if (!Array.isArray(value)) {
    return []
  }

  return value.map((item) => String(item || '').trim()).filter(Boolean)
}
