import * as React from 'react';
import { webFonts } from './remoteAssets/fonts';
import { coreTheme } from './themes';
import { getFonts } from './utils/fontUtils';

/*
 * Only preload woff2 fonts, since woff1 are only included as fallbacks.
 */
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
export const createFontLinks = fonts => {
  const currentFonts = fonts ?? webFonts.core;
  if (!Array.isArray(currentFonts)) {
    return [];
  }
  return currentFonts.filter(f => f?.extensions && Array.isArray(f.extensions) && f.extensions.includes('woff2') && f.filePath && f.filePath.trim() !== '').map(({
    filePath
  }) => /*#__PURE__*/_jsx("link", {
    as: "font",
    crossOrigin: "anonymous",
    href: `${filePath}.woff2`,
    rel: "preload",
    type: "font/woff2"
  }, filePath));
};
export const AssetProvider = ({
  theme
}) => {
  try {
    const defaultTheme = theme ?? coreTheme;
    const themeName = defaultTheme?.name || 'core';
    const fonts = getFonts(themeName);
    return /*#__PURE__*/_jsx(_Fragment, {
      children: createFontLinks(fonts)
    });
  } catch (error) {
    // Handle font loading errors gracefully
    // eslint-disable-next-line no-console
    console.warn('Font loading failed:', error);
    return null;
  }
};