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

@use "sass:map";
@use "sass:math";
@use "sass:meta";

/// default options
/// @access private
/// @type Map
///
$_default-options: (
  "use2x": false,
  "as-mask": false,
  "with-property": true
) !default;

/// set properties
/// @access private
/// @param {Map} $values [()] values of item
/// @param {Map} $options [()] options
///
@mixin set-properties($values: (), $options: ()) {
  @if meta.type-of($values) == "map" {
    $width: map.get($values, "width");
    $height: map.get($values, "height");
    $offset-x: map.get($values, "offset-x");
    $offset-y: map.get($values, "offset-y");
    $total-width: map.get($values, "total-width");
    $total-height: map.get($values, "total-height");

    // normalize options
    $options: if(
      meta.type-of($options) == "map",
      map.merge($_default-options, $options),
      $_default-options
    );
    $use2x: map.get($options, "use2x");
    $as-mask: map.get($options, "as-mask");
    $with-property: map.get($options, "with-property");

    @if meta.type-of($width) == "number" {
      --width: #{if($use2x, math.div($width, 2), $width)};

      @if $with-property {
        width: var(--width);
      }
    }

    @if meta.type-of($height) == "number" {
      --height: #{if($use2x, math.div($height, 2), $height)};

      @if $with-property {
        height: var(--height);
      }
    }

    @if meta.type-of($offset-x) ==
      "number" and
      meta.type-of($offset-y) ==
      "number"
    {
      $offset-x: if($use2x, math.div($offset-x, 2), $offset-x);
      $offset-y: if($use2x, math.div($offset-y, 2), $offset-y);

      @if $as-mask {
        --mask-position: #{$offset-x} #{$offset-y};

        @if $with-property {
          mask-position: var(--mask-position);
        }
      } @else {
        --background-position: #{$offset-x} #{$offset-y};

        @if $with-property {
          background-position: var(--background-position);
        }
      }
    }

    @if meta.type-of($total-width) ==
      "number" and
      meta.type-of($total-height) ==
      "number"
    {
      $total-width: if($use2x, math.div($total-width, 2), $total-width);
      $total-height: if($use2x, math.div($total-height, 2), $total-height);

      @if $as-mask {
        --mask-size: #{$total-width} #{$total-height};

        @if $with-property {
          mask-size: var(--mask-size);
        }
      } @else {
        --background-size: #{$total-width} #{$total-height};

        @if $with-property {
          background-size: var(--background-size);
        }
      }
    }
  }
}
