@use "sass:list";
@use "sass:map";
@use "sass:math";
@use "../../functions/general/smart-quote" as *;
@use "../../functions/general/error-not-token" as *;
@use "../../functions/general/unpack" as *;
@use "../../tokens/font/line-height" as *;
@use "../../tokens/font/base-cap-height" as *;
@use "../../variables/project-cap-heights" as *;

@function lh($props...) {
  $props: unpack($props);

  @if not(list.length($props) == 2) {
    @error 'lh() needs both a valid face and line height token '
      + 'in the format `lh(FACE, HEIGHT)`.';
  }

  $family: smart-quote(list.nth($props, 1));
  $scale: smart-quote(list.nth($props, 2));

  @if not map.has-key($project-cap-heights, $family) {
    @return error-not-token($family, "font family", $project-cap-heights);
  }

  @if not map.get($system-line-height, $scale) {
    @return error-not-token($scale, "line-height", $system-line-height);
  }

  @if not map.get($project-cap-heights, $family) {
    @return false;
  }

  $this-cap: map.get($project-cap-heights, $family);
  $this-line-height: map.get($system-line-height, $scale);
  $normalized-line-height: math.div(
    $this-line-height,
    (math.div($system-base-cap-height, $this-cap))
  );
  $normalized-line-height: math.div(
    math.round($normalized-line-height * 10),
    10
  );

  @return $normalized-line-height;
}

@function line-height($props...) {
  @return lh($props...);
}

// @debug lh("sans", 2);
// @return 1.1;
// @debug lh("serif", 2);
// @return 1.2;
// @debug lh("heading", 2);
// @return 1.2;
// @debug #{lh("not-sans", 2)};
// @return  'not-sans' is not a valid USWDS font family token.
