////
/// (c) hidoo | MIT License
/// @group features
////

@use "sass:map";
@use "sass:meta";
@use "../../lib/function";
@use "../../settings";

/// Default options
/// @access private
/// @type Map
///
$_default-options: (
  "eval-query": true,
  "breakpoints": null
) !default;

/// The media query helper.
/// @param {String} $query - Breakpoint name or media query string
/// @param {Map} $options [()] - Options
/// @param {String} $options.eval-query [false] - Eval query as template whether or not
/// @param {Map} $options.breakpoints [null] - Breakpoints.
///
/// @example scss - scss inputs
///    $breakpoint: (
///      "sm": "(width <= 640px)",
///      ...
///    );
///
///   .selector {
///     @include mixin.media($query: "sm") {
///       font-size: 16px;
///     }
///   }
///
/// @example css - css outputs
///   @media (width <= 640px) {
///     .selector {
///       font-size: 16px;
///     }
///   }
///
@mixin media($query, $options: ()) {
  $opts: map.merge($_default-options, $options);
  $breakpoints: map.get($opts, "breakpoints");

  @if meta.type-of($breakpoints) != "map" {
    $breakpoints: settings.$breakpoints;
  }

  @if map.has-key($breakpoints, $query) {
    $query: map.get($breakpoints, $query);
  }

  @if map.get($opts, "eval-query") {
    @each $key, $value in $breakpoints {
      $query: function.string-replace($query, "$#{$key}", $value);
    }
  }

  @media #{$query} {
    @content;
  }
}
