@use 'sass:list';
@use 'sass:map';
@use 'sass:meta';
@use 'utilities';
@use 'variables';

@function font-family($key) {
  @return utilities.fetch(map.get(variables.$typography, 'fonts'), $key);
}

@function color($key, $opacity: null) {
  $value: utilities.fetch(map.get(variables.$colors, 'all'), $key);
  @if $opacity {
    @return rgba($value, $opacity);
  } @else {
    @return $value;
  }
}

// `dangerously-get-` functions are named this way to discourage their use.
// Users of this library should always be utilizing codified text treatments via the `text` mixin.
@function dangerously-get-letter-spacing($index) {
  @return utilities.access(map.get(variables.$typography, 'letter-spacing'), $index);
}

@function dangerously-get-line-height($index) {
  @return utilities.access(map.get(variables.$typography, 'line-heights'), $index);
}

@function dangerously-get-font-size($index) {
  @return utilities.access(map.get(variables.$typography, 'font-scale'), $index);
}

@function space($indexOrKey) {
  @if meta.type-of($indexOrKey) != number {
    @return utilities.fetch(map.get(variables.$space, 'map'), $indexOrKey);
  }

  @return utilities.access(map.get(variables.$space, 'scale'), $indexOrKey);
}

@function spacing($indices...) {
  $spacing: ();

  @each $index in $indices {
    $spacing: list.append($spacing, space($index));
  }

  @return $spacing;
}

@function text-treatment($key) {
  @return utilities.fetch(map.get(variables.$typography, 'text-treatments'), $key);
}

/// @param {prop (string)} $prop
///   The 0-based index to access
/// @param {list | bool} $list
///   Either list of properties
/// @return {string | null} declares !important or not
@function important($important: false, $prop: null) {
  @if meta.type-of($important) == bool {
    @return if($important, !important, null);
  }

  @if meta.type-of($important) == list {
    @return if(list.index($important, $prop), !important, null);
  }

  @return null;
}
