import { css } from '@emotion/react';
import startCase from 'lodash/startCase';
import { FontWeight } from './weights.js';
import { jsx } from '@emotion/react/jsx-runtime';

function getCdnPath(fontFolder, fontFile) {
  if (!CONFIG.cdnURL) {
    throw new Error("\"cdnURL\" has not been set in the node-config files");
  }

  var fontPath = "".concat(CONFIG.cdnURL, "/fonts/ibmplex/v5.1.3/").concat(fontFolder);
  var cdnIndex = fontFile.split('').reduce(function (a, b) {
    return a + b.charCodeAt(0);
  }, 0) % 4;
  return "".concat(fontPath, "/").concat(fontFile).replace('cdn0', "cdn".concat(cdnIndex));
}

function buildPreloadLink(href) {
  return jsx("link", {
    rel: "preload",
    as: "font",
    crossOrigin: "anonymous",
    href: href
  }, href);
}

function buildFontFace(_ref) {
  var name = _ref.name,
      weight = _ref.weight,
      unicodeRange = _ref.unicodeRange,
      isItalic = _ref.isItalic,
      woff = _ref.woff,
      woff2 = _ref.woff2;
  var unicodeSettings = '';

  if (unicodeRange) {
    unicodeSettings = "unicode-range: ".concat(unicodeRange, ";");
  }

  var weightName = FontWeight[weight];
  return /*#__PURE__*/css("@font-face{font-family:'", startCase(name), "';src:local(\n          '", startCase(name), " ", "".concat(weightName).concat(isItalic ? ' Italic' : ''), "' ),local('", name, "-", "".concat(weightName).concat(isItalic ? 'Italic' : ''), "'),url('", woff2, "') format('woff2'),url('", woff, "') format('woff');font-display:swap;font-style:", isItalic ? 'italic' : 'normal', ";font-weight:", weight, ";", unicodeSettings, ";}" + (process.env.NODE_ENV === "production" ? "" : ";label:buildFontFace;"));
}

export { buildFontFace, buildPreloadLink, getCdnPath };
