import { CONFIG } from '@server/initializers/config.js'

function buildRtspUrl (endpoint: string | undefined, urlPattern: string | undefined, targetCamera: string) {
  const normalizedEndpoint = (endpoint || 'rtsp://localhost:8554').trim().replace(/\/+$/, '')
  const normalizedPattern = (urlPattern || '/{targetCamera}/live')
    .replace('{targetCamera}', encodeURIComponent(targetCamera))

  if (normalizedPattern.startsWith('/')) return normalizedEndpoint + normalizedPattern

  return normalizedEndpoint + '/' + normalizedPattern
}

export function buildRtspSourceUrlForTargetCamera (targetCamera: string) {
  return buildRtspUrl(CONFIG.LIVE.RTSP_SOURCE.ENDPOINT, CONFIG.LIVE.RTSP_SOURCE.URL_PATTERN, targetCamera)
}

export function buildRtspSinkUrlForTargetCamera (targetCamera: string) {
  return buildRtspUrl(CONFIG.LIVE.RTSP_SINK.ENDPOINT, CONFIG.LIVE.RTSP_SINK.URL_PATTERN, targetCamera)
}
