// Copyright (c) Alaska Air. All right reserved. Licensed under the Apache-2.0 license
// See LICENSE in the project root for license information.

// ---------------------------------------------------------------------
// Variables are defined by baseline Design Tokens

@use "sass:list";
@use "sass:map";
@use '../utilityVariables/spacing-options';
@use '../utilityFunctions/capitalize';
@use '../utilityFunctions/map-deep-get';
@use '../utilityFunctions/contains';
@use '../utilityVariables/important';
@use '../libSupport/manageScope';
// The spacing scale map (`$tokens`) is provided by the design-tokens package.
// Under the module system it must be loaded explicitly rather than relying on a
// consumer-injected global.
@use "@aurodesignsystem/design-tokens/dist/legacy/auro-classic/SCSSVariableMap" as tokens;

$ds-spacing-types: inline, stack !default;
$ds-spacing-options: spacing-options.$ds-spacing-options !default;
$ds-spacing-properties: padding, padding, margin, margin !default;

/// This mixin is designed to return a series of pre-defined selectors based on the stack or inline spacing design specs
///
/// DO NOT execute mixin, this is already included with the import of the file dependency
///
/// **Dependency:** `$npm i @aurodesignsystem/design-tokens`
/// @group utility-layout
/// @param {string} $ds-spacing-types [inline, stack] inline - L/R, stack - top/bottom
/// @param {string} $ds-spacing-options [none, 25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] defines value applied.
/// @example css - Example output css
/// .util_inlinePadding25--left {
///   padding-left: 0.125rem;
/// }
/// .util_inlinePadding25--right {
///   padding-right: 0.125rem;
/// }
/// .util_inlineMargin25--left {
///   margin-left: 0.125rem;
/// }
/// .util_inlineMargin25--right {
///   margin-right: 0.125rem;
/// }
/// @example scss - use dependencies
///   @use "@aurodesignsystem/webcorestylesheets/dist/utilityMixins/spacingUtility";
/// @example scss - use with custom over-rides
///   @use "@aurodesignsystem/webcorestylesheets/dist/utilityMixins/spacingUtility" with (
///     $ds-spacing-types: inline,
///     $ds-spacing-options: 200
///   );

@mixin auro_spacing($types: $ds-spacing-types, $options: $ds-spacing-options) {
  @each $type in $types {
    @each $value in $options {
      $map: map.keys(tokens.$tokens);
      $var: map-deep-get.auro_map-deep-get(tokens.$tokens, #{$map}, #{$value});
      $directions: null;
      $properties: null;

      @if $type == stack {
        $directions: top, bottom, top, bottom;
      } @else if $type == inline {
        $directions: left, right, left, right;
      }

      @if $type == stack or $type == inline {
        $properties: $ds-spacing-properties;
      }

      @if not list.index($types, $type) {
        @error 'INVALID, \'#{$type}\' is not known, please choose from options, [#{$types}]';
      }

      @if contains.auro_contains($options, $value) {
        @if not list.index($options, $value) {
          @error 'INVALID, \'#{$value}\' is not known, please choose from options, [#{$ds-spacing-options}]';
        }

        @if $value == 'none' {
          @for $i from 1 through list.length($properties) {
            $direction: list.nth($directions, $i);
            $property: list.nth($properties, $i);
            #{manageScope.$scope}.#{manageScope.$prefix}util_#{$type}#{capitalize.auro_capitalize($property)}#{capitalize.auro_capitalize($value)}--#{$direction} {
              #{$property}-#{$direction}: 0 important.$important;
            }
          }
        } @else {
          @for $i from 1 through list.length($properties) {
            $direction: list.nth($directions, $i);
            $property: list.nth($properties, $i);
            #{manageScope.$scope}.#{manageScope.$prefix}util_#{$type}#{capitalize.auro_capitalize($property)}#{capitalize.auro_capitalize($value)}--#{$direction} {
              #{$property}-#{$direction}: $var important.$important;
            }
          }
        }
      }
    }
  }
}

@include auro_spacing()
