import { getTraceId } from '@defra/hapi-tracing'
import Joi from 'joi'

import { config } from '../../config/index.js'

/**
 * Returns a set of headers to use in an HTTP request, merging them with any existing headers in options.
 * @param {Record<string, string> | undefined} [existingHeaders] - Optional existing headers to merge with the tracing headers.
 * @param {string} [header] - The tracing header name to use.
 * @returns {Record<string, string> | undefined} The merged headers, or undefined if no tracing header is available.
 */
export function applyTraceHeaders(
  existingHeaders,
  header = config.get('tracing').header
) {
  if (!header) {
    return existingHeaders
  }

  const traceId = getTraceId()

  const headers = traceId ? { [header]: traceId } : undefined

  return existingHeaders ? Object.assign(existingHeaders, headers) : headers
}

/**
 * Validates if a string conforms to the uuid structure
 * @param {string} str
 * @returns
 */
export function isValidUUID(str) {
  const { error } = Joi.string().uuid().validate(str)
  return error === undefined
}
