/**
 * Automation2 Flow Editor - Utility functions
 */

import type React from 'react';
import { CATEGORY_ICONS, DEFAULT_CATEGORY_ICON } from './categoryIcons';

/** Resolve localized label from string or { de, en, fr } object */
export function getLabel(
  text: string | Record<string, string> | undefined,
  lang = 'de'
): string {
  if (!text) return '';
  if (typeof text === 'string') return text;
  const rec = text as Record<string, string>;
  return rec[lang] ?? rec.en ?? '';
}

/** Get icon for a category */
export function getCategoryIcon(categoryId: string): React.ReactNode {
  return CATEGORY_ICONS[categoryId] ?? DEFAULT_CATEGORY_ICON;
}

/** Function type for resolving localized labels */
export type GetLabelFn = (text: string | Record<string, string> | undefined, lang?: string) => string;

/** Build an HTML accept attribute from an upload node config's allowedTypes array. */
export function getAcceptStringFromConfig(
  config: Record<string, unknown>
): string {
  const types = config.allowedTypes;
  if (!Array.isArray(types) || types.length === 0) return '*';
  return types.join(',');
}
