import { config } from '$lib/stores/config.js';

/**
 * Get the API URL from runtime config
 * @returns {string}
 */
function getApiUrl() {
  const runtimeUrl = config.getApiUrl();
  return runtimeUrl || import.meta.env.VITE_API_URL || '';
}

/**
 * Generate lab report URL for an order
 * @param {string|number} orderId - Order ID
 * @returns {string} Report URL
 */
export function getReportUrl(orderId) {
  return `${getApiUrl()}/api/reports/${orderId}`;
}

/**
 * Open report in new window
 * @param {string|number} orderId - Order ID
 * @param {string} [target='_blank'] - Window target
 * @returns {Window|null} Window reference
 */
export function openReport(orderId, target = '_blank') {
  const url = getReportUrl(orderId);
  return window.open(url, target);
}

/**
 * Print report for an order
 * @param {string|number} orderId - Order ID
 */
export function printReport(orderId) {
  const url = getReportUrl(orderId);
  const printWindow = window.open(url, '_blank');
  if (printWindow) {
    printWindow.onload = () => {
      printWindow.print();
    };
  }
}
