@use 'sass:map';
@use 'sass:list';
@use 'variables';
@use 'functions';

// Table cell sizing

@mixin cell($font-size, $gap: variables.$margin) {
  font-size: #{$font-size}em;
  padding: ($gap / 2.5 / $font-size) ($gap / 2 / $font-size);
}

// Heading gap, border and font sizing

@mixin heading(
  $font-size,
  $gap: variables.$margin,
  $with-border: false,
  $top-space: true
) {
  font-size: #{$font-size}em;
  margin-bottom: $gap / $font-size;

  @if $top-space {
    margin-top: $gap * 2 / $font-size;
  } @else {
    margin-top: $gap / $font-size;
  }

  @if variables.$enable-heading-border and $with-border {
    border-bottom: 1px solid variables.$border-color;
    padding-bottom: $gap / 2 / $font-size;
  }
}

// Font family declaration

@mixin font-family($native-family, $custom-family, $variable-family) {
  font-family: functions.prepend($custom-family, $native-family);

  @if variables.$enable-variable-fonts and $variable-family {
    @supports (font-variation-settings: normal) {
      font-family: functions.prepend($variable-family, $native-family);
    }
  }
}

// Convenience mixin to set default font families

@mixin font($key) {
  @if map.has-key(variables.$families, $key) {
    $selected-families: map.get(variables.$families, $key);

    @if list.length($selected-families) >= 3 {
      $native-family: functions.native-family(list.nth($selected-families, 1));
      $custom-family: list.nth($selected-families, 2);
      $variable-family: list.nth($selected-families, 3);
      @include font-family($native-family, $custom-family, $variable-family);
    } @else {
      @error "Missing one or more font families";
    }
  } @else {
    @error "Couldn't find a matching font family";
  }
}

// Parent resolver

@mixin parent($selector: variables.$root) {
  @if $selector != 'body' {
    #{$selector} {
      @content;
    }
  } @else {
    @at-root {
      @content;
    }
  }
}
