export type ThemeFontStyle = "normal" | "italic";

export type ThemeFontDisplay =
  | "auto"
  | "block"
  | "swap"
  | "fallback"
  | "optional";

export interface ThemeFontSource {
  filename: string;
  weight: string;
  style: ThemeFontStyle;
}

export interface ThemeFontDefinition {
  name: string;
  family: string;
  cssVariable: string;
  fallbacks: string[];
  display?: ThemeFontDisplay;
  preload?: boolean;
  sources: readonly ThemeFontSource[];
}

export interface NextLocalFontOptions {
  variable: string;
  display?: ThemeFontDisplay;
  fallback: string[];
  preload?: boolean;
  src: ReadonlyArray<{
    path: string;
    weight: string;
    style: ThemeFontStyle;
  }>;
}

export function createLocalFontOptions(
  font: ThemeFontDefinition,
  metaUrl: string,
): NextLocalFontOptions {
  return {
    variable: font.cssVariable,
    display: font.display,
    fallback: font.fallbacks,
    preload: font.preload,
    src: font.sources.map(source => ({
      path: new URL(source.filename, metaUrl).pathname,
      weight: source.weight,
      style: source.style,
    })),
  };
}
