@use "sass:list";
@use "sass:map";
@use "sass:meta";
@use "sass:string";
@use "../variables" as *;
@use "../vendor/rfs" as *;
@use "ltr-rtl" as *;

// Generate selectors for theme variants
@function theme-prefix($theme, $selector) {
  @return #{"[data" + $data-infix + "theme="$theme + "] ." + $theme + "\\:" + $selector + ", [data" + $data-infix + "theme="$theme + "] ." + $theme + "\\:" + $selector + ":not([class*='#{$theme}:'])"};
}

// Utility generator
// Used to generate utilities & print utilities
@mixin generate-utility($utility, $infix: "", $is-rfs-media-query: false) {
  $values: map.get($utility, values);

  // If the values are a list or string, convert it into a map
  @if meta.type-of($values) == "string" or meta.type-of(list.nth($values, 1)) != "list" {
    $values: list.zip($values, $values);
  }

  @each $key, $value in $values {
    $properties: map.get($utility, property);

    // Multiple properties are possible, for example with vertical or horizontal margins or paddings
    @if meta.type-of($properties) == "string" {
      $properties: list.append((), $properties);
    }

    // Use custom class if present
    // stylelint-disable-next-line scss/at-function-named-arguments, @stylistic/function-whitespace-after
    $property-class: if(sass(map.has-key($utility, class)): map.get($utility, class); else: list.nth($properties, 1));
    // stylelint-disable-next-line scss/at-function-named-arguments
    $property-class: if(sass($property-class == null): ""; else: $property-class);

    // Use custom CSS variable name if present, otherwise default to `class`
    // stylelint-disable-next-line scss/at-function-named-arguments, @stylistic/function-whitespace-after
    $css-variable-name: if(sass(map.has-key($utility, css-variable-name)): map.get($utility, css-variable-name); else: map.get($utility, class));

    // State params to generate pseudo-classes
    // stylelint-disable-next-line scss/at-function-named-arguments, @stylistic/function-whitespace-after
    $state: if(sass(map.has-key($utility, state)): map.get($utility, state); else: ());

    // stylelint-disable-next-line scss/at-function-named-arguments, @stylistic/function-whitespace-after
    $infix: if(sass($property-class == "" and string.slice($infix, 1, 1) == "-"): string.slice($infix, 2); else: $infix);

    // Don't prefix if value key is null (e.g. with shadow class)
    // stylelint-disable-next-line scss/at-function-named-arguments
    $property-class-modifier: if(sass($key): if(sass($property-class == "" and $infix == ""): ""; else: "-") + $key; else: "");

    @if map.get($utility, rfs) {
      // Inside the media query
      @if $is-rfs-media-query {
        $val: rfs-value($value);

        // Do not render anything if fluid and non fluid values are the same
        // stylelint-disable-next-line scss/at-function-named-arguments
        $value: if(sass($val == rfs-fluid-value($value)): null; else: $val);
      }
      @else {
        $value: rfs-fluid-value($value);
      }
    }

    $is-css-var: map.get($utility, css-var);
    $is-dark-mode: map.get($utility, dark-mode);
    $is-local-vars: map.get($utility, local-vars);
    $is-rtl: map.get($utility, rtl);

    @if $value != null {
      @if $is-rtl == false {
        /* rtl:begin:remove */
      }

      @if $is-css-var {
        @if $enable-dark-mode and $is-dark-mode {
          #{theme-prefix("dark", "#{$property-class + $infix + $property-class-modifier}")},
          .#{$property-class + $infix + $property-class-modifier} {
            --#{$prefix}#{$css-variable-name}: #{$value};
          }
        } @else {
          .#{$property-class + $infix + $property-class-modifier} {
            --#{$prefix}#{$css-variable-name}: #{$value};
          }
        }

        @each $pseudo in $state {
          .#{$property-class + $infix + $property-class-modifier}-#{$pseudo}:#{$pseudo} {
            --#{$prefix}#{$css-variable-name}: #{$value};
          }
        }
      } @else {
        @if $enable-dark-mode and $is-dark-mode {
          #{theme-prefix("dark", "#{$property-class + $infix + $property-class-modifier}")},
          .#{$property-class + $infix + $property-class-modifier} {
            @each $property in $properties {
              @if $is-local-vars {
                @each $local-var, $variable in $is-local-vars {
                  --#{$prefix}#{$local-var}: #{$variable};
                }
              }
              // stylelint-disable-next-line scss/at-function-named-arguments
              #{$property}: $value if(sass($enable-important-utilities): !important);
            }
          }
        } @else {
          .#{$property-class + $infix + $property-class-modifier} {
            @each $property in $properties {
              @if $is-local-vars {
                @each $local-var, $variable in $is-local-vars {
                  --#{$prefix}#{$local-var}: #{$variable};
                }
              }
              @if $is-rtl == true {
                @if (meta.type-of($value) == "map") {
                  @include ltr-rtl($property, map.get($value, "ltr"), null, map.get($value, "rtl"), if(sass($enable-important-utilities): !important));
                } @else {
                  @include ltr-rtl($property, $value, null, null, if(sass($enable-important-utilities): !important));
                }
              } @else {
                // stylelint-disable-next-line scss/at-function-named-arguments
                #{$property}: $value if(sass($enable-important-utilities): !important);
              }
            }
          }
        }

        @each $pseudo in $state {
          .#{$property-class + $infix + $property-class-modifier}-#{$pseudo}:#{$pseudo} {
            @each $property in $properties {
              @if $is-local-vars {
                @each $local-var, $variable in $is-local-vars {
                  --#{$prefix}#{$local-var}: #{$variable};
                }
              }
              // stylelint-disable-next-line scss/at-function-named-arguments
              #{$property}: $value if(sass($enable-important-utilities): !important);
            }
          }
        }
      }

      @if $is-rtl == false {
        /* rtl:end:remove */
      }
    }
  }
}
