/**
 * Template Helpers
 *
 * Re-exports template helper utilities from @frontmcp/ui.
 * Also provides individual helper functions for backwards compatibility.
 *
 * @see {@link https://docs.agentfront.dev/docs/servers/tools#tool-ui | Tool UI Documentation}
 */

// Import escapeHtml from @frontmcp/uipack/utils - single source of truth (no React needed)
import { escapeHtml } from '@frontmcp/uipack/utils';

// Re-export createTemplateHelpers from @frontmcp/uipack shell module
export { createTemplateHelpers } from '@frontmcp/uipack/shell';

// ============================================
// Individual Helper Functions (Backwards Compatibility)
// ============================================
// These are exported individually for SDK consumers who import them directly.
// For new code, prefer using createTemplateHelpers() instead.

// Re-export escapeHtml for backwards compatibility
export { escapeHtml };

let idCounter = 0;

/**
 * Format a date for display.
 * @param date - Date object or ISO string
 * @param format - Optional format: 'iso', 'time', 'datetime', or default (localized date)
 */
export function formatDate(date: Date | string, format?: string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  if (isNaN(d.getTime())) {
    return String(date);
  }

  switch (format) {
    case 'iso':
      return d.toISOString();
    case 'time':
      return d.toLocaleTimeString();
    case 'datetime':
      return d.toLocaleString();
    default:
      return d.toLocaleDateString();
  }
}

/**
 * Format a number as currency.
 * @param amount - The numeric amount
 * @param currency - ISO 4217 currency code (default: 'USD')
 */
export function formatCurrency(amount: number, currency = 'USD'): string {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency,
  }).format(amount);
}

/**
 * Generate a unique ID for DOM elements.
 * @param prefix - Optional prefix for the ID
 */
export function uniqueId(prefix = 'mcp'): string {
  return `${prefix}-${++idCounter}-${Date.now().toString(36)}`;
}

/**
 * Safely embed JSON data in HTML.
 * Escapes characters that could break out of script tags or HTML.
 */
export function jsonEmbed(data: unknown): string {
  return JSON.stringify(data)
    .replace(/</g, '\\u003c')
    .replace(/>/g, '\\u003e')
    .replace(/&/g, '\\u0026')
    .replace(/'/g, '\\u0027');
}

/**
 * Reset the ID counter (useful for testing).
 * @internal
 */
export function resetIdCounter(): void {
  idCounter = 0;
}
