////////////////////////////////////////////////////////////////////////////////
// Headings
////////////////////////////////////////////////////////////////////////////////

@use 'sass:string';
@use 'sass:map';
@use 'sass:meta';
@use 'sass:math';
@use './helpers/font';
@use './modules/unit';

// Default Heading Values ------------------------------------------------------

$h1 : (
  'font-size'   : 3.0rem,
  'line-height' : 1.1
) !default;

$h2 : (
  'font-size'   : 2.5rem,
  'line-height' : 1.3
) !default;

$h3 : (
  'font-size'   : 2.2rem,
  'line-height' : 1.4
) !default;

$h4 : (
  'font-size'   : 2.0rem,
  'line-height' : 1.4
) !default;

$h5 : (
  'font-size'   : 1.8rem,
  'line-height' : 1.4
) !default;

$h6 : (
  'font-size'   : 1.6rem,
  'line-height' : 1.4
) !default;

$base : 62.5 !default;
$scaler : 1.5vw !default;
$enable-cropping : false !default;

// Fallback Line-heights should they not have been passed ----------------------

@if not map.has-key($h1, "line-height") { $h1 : map.set($h1, 'line-height', 1.1); }
@if not map.has-key($h2, "line-height") { $h2 : map.set($h2, 'line-height', 1.3); }
@if not map.has-key($h3, "line-height") { $h3 : map.set($h3, 'line-height', 1.4); }
@if not map.has-key($h4, "line-height") { $h4 : map.set($h4, 'line-height', 1.4); }
@if not map.has-key($h5, "line-height") { $h5 : map.set($h5, 'line-height', 1.4); }
@if not map.has-key($h6, "line-height") { $h6 : map.set($h6, 'line-height', 1.4); }

// Define the variables so they are still accesible ----------------------------

$h1-font-size     : map-get($h1, 'font-size');
$h1-line-height   : map-get($h1, 'line-height');
$h1-font-size-min : if(map.has-key($h1, "font-size-min"), map-get($h1, "font-size-min"), false);
$h1-scaler        : if(map.has-key($h1, "scaler"), map-get($h1, "scaler"), $scaler);

$h2-font-size     : map-get($h2, 'font-size');
$h2-line-height   : map-get($h2, 'line-height');
$h2-font-size-min : if(map.has-key($h2, "font-size-min"), map-get($h2, "font-size-min"), false);
$h2-scaler        : if(map.has-key($h2, "scaler"), map-get($h2, "scaler"), $scaler);

$h3-font-size     : map-get($h3, 'font-size');
$h3-line-height   : map-get($h3, 'line-height');
$h3-font-size-min : if(map.has-key($h3, "font-size-min"), map-get($h3, "font-size-min"), false);
$h3-scaler        : if(map.has-key($h3, "scaler"), map-get($h3, "scaler"), $scaler);

$h4-font-size     : map-get($h4, 'font-size');
$h4-line-height   : map-get($h4, 'line-height');
$h4-font-size-min : if(map.has-key($h4, "font-size-min"), map-get($h4, "font-size-min"), false);
$h4-scaler        : if(map.has-key($h4, "scaler"), map-get($h4, "scaler"), $scaler);

$h5-font-size     : map-get($h5, 'font-size');
$h5-line-height   : map-get($h5, 'line-height');
$h5-font-size-min : if(map.has-key($h5, "font-size-min"), map-get($h5, "font-size-min"), false);
$h5-scaler        : if(map.has-key($h5, "scaler"), map-get($h5, "scaler"), $scaler);

$h6-font-size     : map-get($h6, 'font-size');
$h6-line-height   : map-get($h6, 'line-height');
$h6-font-size-min : if(map.has-key($h6, "font-size-min"), map-get($h6, "font-size-min"), false);
$h6-scaler        : if(map.has-key($h6, "scaler"), map-get($h6, "scaler"), $scaler);

// Handles Crops should they not have been passed ------------------------------

$_h1-crop-top    : if(map.has-key($h1, "crop-top"), map-get($h1, "crop-top"), false);
$_h2-crop-top    : if(map.has-key($h2, "crop-top"), map-get($h2, "crop-top"), false);
$_h3-crop-top    : if(map.has-key($h3, "crop-top"), map-get($h3, "crop-top"), false);
$_h4-crop-top    : if(map.has-key($h4, "crop-top"), map-get($h4, "crop-top"), false);
$_h5-crop-top    : if(map.has-key($h5, "crop-top"), map-get($h5, "crop-top"), false);
$_h6-crop-top    : if(map.has-key($h6, "crop-top"), map-get($h6, "crop-top"), false);

$_h1-crop-bottom : if(map.has-key($h1, "crop-bottom"), map-get($h1, "crop-bottom"), false);
$_h2-crop-bottom : if(map.has-key($h2, "crop-bottom"), map-get($h2, "crop-bottom"), false);
$_h3-crop-bottom : if(map.has-key($h3, "crop-bottom"), map-get($h3, "crop-bottom"), false);
$_h4-crop-bottom : if(map.has-key($h4, "crop-bottom"), map-get($h4, "crop-bottom"), false);
$_h5-crop-bottom : if(map.has-key($h5, "crop-bottom"), map-get($h5, "crop-bottom"), false);
$_h6-crop-bottom : if(map.has-key($h6, "crop-bottom"), map-get($h6, "crop-bottom"), false);


// Mixins for each heading -----------------------------------------------------

@mixin _set-crop($font-size, $line-height, $crop) {

  @if (meta.type-of($crop) == 'map') {

    $font-size   : unit.strip($font-size) * 10;
    $line-height : unit.strip($line-height);
    $top         : if(map.has-key($crop, "top"), unit.strip(map.get($crop, "top")), 0);
    $bottom      : if(map.has-key($crop, "bottom"), unit.strip(map.get($crop, "bottom")), 0);

    @include font.crop($font-size, $line-height, $top, $bottom);

  }

}

@mixin _heading($font-size-only, $crop, $options) {

  $font-size        : map-get($options, 'font-size');
  $font-size-min    : map-get($options, 'font-size-min');
  $line-height      : map-get($options, 'line-height');
  $crop-top         : map-get($options, 'crop-top');
  $crop-bottom      : map-get($options, 'crop-bottom');
  $scaler : map-get($options, 'scaler');

  font-size : font.size(map-get($options, 'font-size'));

  @if ( $font-size-min and $font-size-min < $font-size ) {
    font-size : clamp(#{font.size($font-size-min)}, #{$scaler}, #{font.size($font-size)});
  }

  @if not $font-size-only { 

    @if ( $crop ) { 
      @include font.crop($font-size, $line-height, $crop-top, $crop-bottom ); 
    } @else {
      line-height : $line-height; 
    }

  }
}

@mixin h1($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h1-font-size,
    'font-size-min' : $h1-font-size-min,
    'line-height'   : $h1-line-height,
    'scaler'        : $h1-scaler,
    'crop-top'      : $_h1-crop-top,
    'crop-bottom'   : $_h1-crop-bottom
  ));

}

@mixin h2($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h2-font-size,
    'font-size-min' : $h2-font-size-min,
    'line-height'   : $h2-line-height,
    'scaler'        : $h2-scaler,
    'crop-top'      : $_h2-crop-top,
    'crop-bottom'   : $_h2-crop-bottom
  ))

}

@mixin h3($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h3-font-size,
    'font-size-min' : $h3-font-size-min,
    'line-height'   : $h3-line-height,
    'scaler'        : $h3-scaler,
    'crop-top'      : $_h3-crop-top,
    'crop-bottom'   : $_h3-crop-bottom
  ))

}

@mixin h4($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h4-font-size,
    'font-size-min' : $h4-font-size-min,
    'line-height'   : $h4-line-height,
    'scaler'        : $h4-scaler,
    'crop-top'      : $_h4-crop-top,
    'crop-bottom'   : $_h4-crop-bottom
  ))

}

@mixin h5($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h5-font-size,
    'font-size-min' : $h5-font-size-min,
    'line-height'   : $h5-line-height,
    'scaler'        : $h5-scaler,
    'crop-top'      : $_h5-crop-top,
    'crop-bottom'   : $_h5-crop-bottom
  ))

}

@mixin h6($font-size-only : false, $crop : $enable-cropping) {

  @include _heading($font-size-only, $crop, (
    'font-size'     : $h6-font-size,
    'font-size-min' : $h6-font-size-min,
    'line-height'   : $h6-line-height,
    'scaler'        : $h6-scaler,
    'crop-top'      : $_h6-crop-top,
    'crop-bottom'   : $_h6-crop-bottom
  ))

}