/**
 * Requires
 */
@use "sass:meta";
@use "sass:list";
@use "sass:map";
@use '../abstract/has-query' as *;
@use 'font-fluid-base' as *;

/**
 * Fluid font style setter
 * Adds the given font style attributes and fluid style in current scope
 * @param {string} $style - Name of font style
 * @param {Map} $styles - Map of font styles to use, default: $font-styles
 * @param {string} $base-query - Optional name for an @mixin media.query(name), default: null
 * @param {string} $fluid-attribute - Name of attribute to read fluid properties from, default: 'fluid'
 * @param {string} $no-max-attribute - Name of attribute to for no-max argument, default: 'no-max'
 * @output Adds the given font attributes and media queries in current scope
 */
@mixin font-fluid($style, $styles: null, $base-query: null, $fluid-attribute: 'fluid', $no-max-attribute: 'no-max', $error: 'font-fluid::') {

  // Styles must be a map
  @if meta.type-of($styles) != map {
    @error '#{$error}$styles[#{meta.type-of($styles)}] must be a map';
  }

  // Style must be defined in source
  @if map.has-key($styles, $style) != true {
    @error '#{$error}::$name[#{$style}] is not defined on $styles';
  }

  // Defined style must be a map
  $map: map.get($styles, $style);
  @if meta.type-of($map) != map {
    @error '#{$error}::$name[#{$style}] must be a map of attributes';
  }

  // Get base query if not set
  @if $base-query == null {
    $has-query: has-query($style);
    @if list.length($has-query) > 1 {
      $base-query: list.nth($has-query, 2);
    }
  }

  // Get no-max option from map
  $no-max: false;
  @if map.has-key($map, $no-max-attribute) {
    $no-max: map.get($map, $no-max-attribute);
  }

  // Cycle style attributes
  @each $attribute, $value in $map {

    // The fluid attribute defines the fluid style
    @if $attribute == $fluid-attribute {
      @include font-fluid-base(
        map.get($value, 'min-vw'),
        map.get($value, 'max-vw'),
        map.get($value, 'min-size'),
        map.get($value, 'max-size'),
        $base-query,
        $no-max,
        $error
      )
    }

    // Set base attributes
    @else if $attribute != $no-max-attribute {
      #{$attribute}: $value;
    }
  }
}
