export function getFlagBaseUrl(selector = '.crtoday-converter .crtoday-flag'): string {
  const img = document.querySelector<HTMLImageElement>(selector);
  if (img?.src) {
    return img.src.replace(/[^/]+$/, '');
  }
  return '';
}

export function createFlagWrap(
  flagBaseUrl: string,
  flagCode: string,
  sizeClass: string,
  size: number
): HTMLElement | null {
  if (!flagBaseUrl || !flagCode) {
    return null;
  }

  const flagWrap = document.createElement('span');
  flagWrap.className = `crtoday-flag-wrap ${sizeClass}`;

  const img = document.createElement('img');
  img.className = 'crtoday-flag';
  img.src = `${flagBaseUrl}${flagCode}.webp`;
  img.alt = '';
  img.width = size;
  img.height = size;
  img.loading = 'lazy';
  img.decoding = 'async';

  flagWrap.appendChild(img);
  return flagWrap;
}
