/**
 * Requires
 */
@use 'sass:meta';
@use '../abstract/is-query' as *;
@use '../media';

/**
 * Generate custom properties with optional query wrapping
 * @public
 * @param {map} $props - Map of properties
 * @param {string} $prefix - Property prefix
 * @param {string} $query-marker - String marking the beginning of a query, default: '_at_'
 * @param {string} $error - Error message prefix
 * @output Adds given custom properties in current scope
 */
@mixin properties($props, $prefix: '', $query-marker: '_at_', $error: 'properties::') {

  // Require a map of properties
  @if meta.type-of($props) != map {
    @error '#{$error}props[#{meta.type-of($props)}] must be a map containing at least one name, value pair';
  }

  // Generate custom properties
  @each $name, $value in $props {

    // Name is a media query
    $query: is-query($name, $query-marker);
    @if $query {

      // Require a map of properties
      @if meta.type-of($value) != map {
        @error '#{$error}props.#{$name}[#{meta.type-of($value)}] must be a map containing at least one name, value pair';
      }

      // Nest properties inside a query
      @include media.query($query) {

        // Generate custom properties
        @each $query-name, $query-value in $value {
          --#{$prefix}#{$query-name}: #{$query-value};
        }
      }
    }

    // Output as property
    @else {
      --#{$prefix}#{$name}: #{$value};
    }
  }
}
