@import '../functions/power';

$sizes: (1 -  $font-size-offset-default-size: 0);
$font-sizes: (default: null);
@each $name, $scale in $scales {
  $map: (1 -  $font-size-offset-default-size: 0);
  $font-sizes: map_merge($font-sizes, ($name: $map));
}

@mixin font-sizes($ratio, $base, $set-vars: true) {
  @for $i from 1 through $font-size-num {
    $n: $i -  $font-size-offset-default-size;

    $size: $base * power($ratio, $n);
    @if $size < $font-size-min {
      $size: $font-size-min;
    }

    $sizes: map_merge($sizes, ($n: $size)) !global;

    @if $set-vars {
      @if $n < 0 {
        --font-size-m#{abs($n)}: #{$size};
      }
      @else if $n == 0 {
        --font-size-#{$n}: #{$size};
      }
      @else {
        --font-size-p#{$n}: #{$size};
      }
    }
  }
}

@mixin define-font-variables($set-vars: true) {
  @each $name, $scale in $scales {
    $base: map-get($scale, font-base);
    $ratio: map-get($scale, font-ratio);
    $bp: map-get($scale, bp);

    @if $bp == 0 {
      :root {
        @include font-sizes($ratio, $base, $set-vars);
      }
    }
    @else {
      :root {
        @media screen and (min-width: $bp) {
          @include font-sizes($ratio, $base, $set-vars);
        }
      }
    }

    .view--#{$name} {
      @include font-sizes($ratio, $base, $set-vars);
    }

    $font-sizes: map_merge($font-sizes, ($name: $sizes)) !global;
  }

}

@function line-height($line, $include-calc: true, $times: null, $fontsize-relative: null) {
  $line-height: null;

  @if $times != null {
    $times: " * #{$times}"
  }
  @else {
    $times: "";
  }

  @if $fontsize-relative != null {
    //check if $fontsize-relative is a map
    @if type-of($fontsize-relative) != 'map' {
      @error "$fontsize-relative parameter for line-height() must be a map.";
    }
    //check if $fontsize-relative map has the required key
    @else if map-has-key($fontsize-relative, "current") and map-has-key($fontsize-relative, "target") {
      $fontsize-relative: " + #{math.div($line-height-scaleable-part, 1em)} * (#{font-size(map-get($fontsize-relative, "target"))} - #{font-size(map-get($fontsize-relative, "current"))})#{$times}";
    }
    //trough an error if map does not have the required keys
    @else {
      @error "$fontsize-relative parameter for line-height() must have keys \"current\" and \"target\".";
    }
  }
  @else {
    $fontsize-relative: "";
  }

  @if $line == "single" {
    $line-height: "#{$line-height-scaleable-part}#{$times}#{$fontsize-relative} + #{$line-height-single-fixed-part}#{$times}";
  }
  @else if $line == "multiple" {
    $line-height: "#{$line-height-scaleable-part}#{$times}#{$fontsize-relative} + #{$line-height-multiple-fixed-part}#{$times}";
  }
  @else {
    @error "line-height(): Line-height parameter must be either single or multiple";
  }

  @if $include-calc {
    @return calc(#{$line-height});
  }
  @else {
    @return #{$line-height};
  }
}

@function font-size($size) {
  @if $size < 0 {
    @return var(--font-size-m#{abs($size)});
  }
  @else if $size == 0 {
    @return var(--font-size-#{$size});
  }
  @else {
    @return var(--font-size-p#{$size});
  }
}


//sets max-width to either the default max-width of text with default font-size or the max-width for a given font-size
@mixin max-line-width($max-width){
  $max-size: $font-size-num - $font-size-offset-default-size;
  $min-size: ($font-size-offset-default-size - 1) * -1;

  @if $max-width != null {
    @if $max-width != "default" {
      @if $max-width < $min-size or $max-width > $max-size {
        @error "set-font(): Requested max-width is out of range. Must be between " + $min-size + " and " + $max-size;
      }
      @else {
        max-width: calc(#{math.div($line-width-max, 1em)} * #{font-size($max-width)});
      }
    }
    @else if $max-width == "default" {
      max-width: calc(#{math.div($line-width-max, 1em)} * var(--font-size-0));
    }
  }
}


@mixin set-font($size, $line-height, $weight: "normal", $max-width: null, $transform: null) {

  //check if requested font-size is within range
  $max-size: $font-size-num - $font-size-offset-default-size;
  $min-size: ($font-size-offset-default-size - 1) * -1;
  @if $size < $min-size or $size > $max-size {
    @error "set-font(): Requested font-size is out of range. Must be between " + $min-size + " and " + $max-size;
  }

  //font-size
  font-size: font-size($size);

  //line-height
  line-height: line-height($line-height);

  //font-weight
  @if $weight == "light" {
    font-weight: $font-weight-light;
  }
  @else if $weight == "bold" {
    font-weight: $font-weight-bold;
  }
  @else {
    font-weight: $font-weight-normal;
  }

  //text-transform
  @if $transform == "uppercase" {
    text-transform: uppercase;
  }
  @else if $transform == "none" {
    text-transform: none;
  }

  //max-width
  @include max-line-width($max-width);
}
