////
/// (c) hidoo | MIT License
/// @group plugin/spritesheet
////

@use "sass:map";
@use "sass:meta";
@use "../../../../lib/mixin";
@use "../../../../settings";
@use "./set-properties" as _mixin;

/// Define responsive item
/// @access private
/// @param {String} $name name of spritesheet item
/// @param {Map} $items [()] spritesheet items
/// @param {Map} $options [()] options
///
@mixin define-responsive-item($name, $items: (), $options: ()) {
  $breakpoints: ();

  // Filter breakpoints
  @each $bp, $query in settings.$breakpoints {
    @if meta.type-of($query) == "string" {
      $breakpoints: map.set($breakpoints, $bp, $query);
    }
  }

  @if map.has-key($items, $name) {
    $use2x: map.get($options, "use2x");
    $on-args: (
      "capturing-selectors": map.get($options, "capturing-selectors")
    );

    &-#{$name} {
      $up: map.get($items, $name);

      @content;

      @include _mixin.set-properties(
        $values: $up,
        $options: map.merge(
            $options,
            (
              "use2x": $use2x == true
            )
          )
      );

      @each $bp, $query in $breakpoints {
        @if map.has-key($items, "#{$bp}_#{$name}") {
          $up-in-bp: map.get($items, "#{$bp}_#{$name}");

          @include mixin.media($query: $query) {
            @include _mixin.set-properties(
              $values: $up-in-bp,
              $options: map.merge(
                  $options,
                  (
                    "use2x": $use2x == $bp or $use2x == true,
                    "with-property": false
                  )
                )
            );
          }
        }
      }

      @if map.has-key($items, "#{$name}--focus") {
        $focus: map.get($items, "#{$name}--focus");

        @include mixin.on-focus($on-args...) {
          @include _mixin.set-properties(
            $values: (
              "offset-x": map.get($focus, "offset-x"),
              "offset-y": map.get($focus, "offset-y")
            ),
            $options: map.merge(
                $options,
                (
                  "use2x": $use2x == true,
                  "with-property": false
                )
              )
          );
        }

        @each $bp, $query in settings.$breakpoints {
          @if map.has-key($items, "#{$bp}_#{$name}--focus") {
            @include mixin.media($query: $query) {
              $focus-in-bp: map.get($items, "#{$bp}_#{$name}--focus");

              @include mixin.on-focus($on-args...) {
                @include _mixin.set-properties(
                  $values: (
                    "offset-x": map.get($focus-in-bp, "offset-x"),
                    "offset-y": map.get($focus-in-bp, "offset-y")
                  ),
                  $options: map.merge(
                      $options,
                      (
                        "use2x": $use2x == $bp or $use2x == true,
                        "with-property": false
                      )
                    )
                );
              }
            }
          }
        }
      }

      @if map.has-key($items, "#{$name}--current") {
        $current: map.get($items, "#{$name}--current");

        @include mixin.on-current($on-args...) {
          @include _mixin.set-properties(
            $values: (
              "offset-x": map.get($current, "offset-x"),
              "offset-y": map.get($current, "offset-y")
            ),
            $options: map.merge(
                $options,
                (
                  "use2x": $use2x == true,
                  "with-property": false
                )
              )
          );
        }

        @each $bp, $query in settings.$breakpoints {
          @if map.has-key($items, "#{$bp}_#{$name}--current") {
            @include mixin.media($query: $query) {
              $current-in-bp: map.get($items, "#{$bp}_#{$name}--current");

              @include mixin.on-current($on-args...) {
                @include _mixin.set-properties(
                  $values: (
                    "offset-x": map.get($current-in-bp, "offset-x"),
                    "offset-y": map.get($current-in-bp, "offset-y")
                  ),
                  $options: map.merge(
                      $options,
                      (
                        "use2x": $use2x == $bp or $use2x == true,
                        "with-property": false
                      )
                    )
                );
              }
            }
          }
        }
      }

      @if map.has-key($items, "#{$name}--disabled") {
        $disabled: map.get($items, "#{$name}--disabled");

        @include mixin.on-disabled($on-args...) {
          @include _mixin.set-properties(
            $values: (
              "offset-x": map.get($disabled, "offset-x"),
              "offset-y": map.get($disabled, "offset-y")
            ),
            $options: map.merge(
                $options,
                (
                  "use2x": $use2x == true,
                  "with-property": false
                )
              )
          );
        }

        @each $bp, $query in settings.$breakpoints {
          @if map.has-key($items, "#{$bp}_#{$name}--disabled") {
            @include mixin.media($query: $query) {
              $disabled-in-bp: map.get($items, "#{$bp}_#{$name}--disabled");

              @include mixin.on-disabled($on-args...) {
                @include _mixin.set-properties(
                  $values: (
                    "offset-x": map.get($disabled-in-bp, "offset-x"),
                    "offset-y": map.get($disabled-in-bp, "offset-y")
                  ),
                  $options: map.merge(
                      $options,
                      (
                        "use2x": $use2x == $bp or $use2x == true,
                        "with-property": false
                      )
                    )
                );
              }
            }
          }
        }
      }
    }
  } @else {
    @warn "@mixin define-responsive-item: Spritesheet '#{$name}' was not generate, because $name: '#{$name}' is not found.";
  }
}
