// --- Default options helpers ---------------------------------------------- //

// Merge default options for `A(define, ...)` mixin.
@function _anthology-define-options($options: ()) {
  @return A(map-merge,
    (
      'important': true,
      'responsive': true,
      'pseudos': null,
      'themes': null,
    ),
    $options
  );
}

// Merge default options for `A(emit, ...)` mixin.
@function _anthology-emit-options($options: ()) {
  @return A(map-merge,
    (
      'important': false,
      'silent': false,
      'flush-cache': true,
    ),
    $options
  );
}

// --- Variable helpers ----------------------------------------------------- //

@function _anthology-config-to-map()
{
  @return (
    'breakpoints': $_anthology-config-breakpoints,
    'separator': $_anthology-config-separator,
    'important-tag': $_anthology-config-important-tag,
    'theme-tag': $_anthology-config-theme-tag,
    'responsive-tag': $_anthology-config-responsive-tag,
    'namespace-tag': $_anthology-config-namespace-tag,
    'grid-prefix': $_anthology-config-grid-prefix,
    'grid-area-prefix': $_anthology-config-grid-area-prefix,
    'theme-attr': $_anthology-config-theme-attr,
  );
}

// Returns the cache with the specified key reset to its default value.
@function _anthology-flush-define-state($state-key)
{
  @return A(map-set,
    $_anthology-define-state,
    $state-key,
    A(map-get, $_anthology-define-state-defaults, $state-key),
  );
}

// --- Miscellaneous utils -------------------------------------------------- //


@function anthology-to-json($map) {
  $typeOfArg: type-of($map);

  @if $typeOfArg == 'list'
  {
    $list: $map;
    $result: '[';
    $listLength: length($list);

    @for $i from 1 through $listLength
    {
      $list-item: nth($list, $i);
      $result: '#{$result} #{A(to-json, $list-item)}';
      @if $i != $listLength { $result: '#{$result},'; }
    }

    $result: '#{$result} ]';
    @return $result;
  }
  @else if $typeOfArg != 'map'
  {
    @return '"#{$map}"';
  }

  $result: '{';
  $mapLength: length($map);

  @for $i from 1 through $mapLength
  {
    $entry: nth($map, $i);
    $key: nth($entry, 1);
    $value: nth($entry, 2);

    $typeOfValue: type-of($value);

    // Transform inner maps into JSON
    @if $typeOfValue == 'map' or $typeOfValue == 'list'
    {
      $result: '#{$result} "#{$key}": #{A(to-json, $value)}';
    }
    @else
    {
      $result: '#{$result} "#{$key}": "#{$value}"';
    }


    @if $i != $mapLength { $result: '#{$result},'; }
  }

  $result: '#{$result} }';
  @return $result;
}
