@use 'sass:map';
@use 'sass:string';
@use '../config' as *;

/// Generate CSS custom properties based on a map.
/// @param {map} $map - The map to generate the CSS custom properties from.
/// @param {list} $exclude - The list of keys (or a segment of it) to exclude.
/// @param {list} $include - The list of keys (or a segment of it) to include.
/// @return {string} - The generated CSS custom properties.
/// @throws {error} - If you use both $exclude and $include arguments.
@mixin generate-variables(
  $map,
  $exclude: null,
  $include: null
) {
  @if $exclude and $include {
    @error 'You can\'t use both $exclude and $include arguments.';
  }

  @if map.get($settings, 'css-custom-properties') {
    $exclude-map: $map;
    $include-map: ();

    @if $exclude {
      @each $key, $value in $map {
        @if $value {
          @each $fraction in $exclude {
            @if string.index($key, $fraction) {
              $exclude-map: map.remove($exclude-map, $key);
            }
          }
        }
      }
    }

    @if $include {
      @each $key, $value in $map {
        @if $value {
          @each $fraction in $include {
            @if string.index($key, $fraction) {
              $include-map: map.set($include-map, $key, $value);
            }
          }
        }
      }
    }

    @if $exclude {
      @each $key, $value in $exclude-map {
        @if $value {
          --#{$internal-prefix}#{$key}: #{$value};
        }
      }
    }

    @if $include {
      @each $key, $value in $include-map {
        @if $value {
          --#{$internal-prefix}#{$key}: #{$value};
        }
      }
    }

    @if not $exclude and not $include {
      @each $key, $value in $map {
        @if $value {
          --#{$internal-prefix}#{$key}: #{$value};
        }
      }
    }
  }
}
