@use '../abstracts/variables';

$directions: (
  'a': (
    'top',
    'right',
    'bottom',
    'left'
  ),
  'x': (
    'right',
    'left'
  ),
  'y': (
    'top',
    'bottom'
  ),
  't': (
    'top'
  ),
  'b': (
    'bottom'
  ),
  'r': (
    'right'
  ),
  'l': (
    'left'
  )
);

@function selector($direction, $breakpoint, $value) {
  @return if(
    $breakpoint != '',
    #{$direction}-#{$breakpoint}-#{$value},
    #{$direction}-#{$value}
  );
}

@function pixels($number) {
    @return $number * 8px;
}

@mixin classesForBreakpoint($breakpoint) {
  @each $direction, $properties in $directions {
    $selector: selector($direction, $breakpoint, 'auto');
    
    .m#{$selector} {
      @each $property in $properties {
        margin-#{$property}: auto;
      }
    }
    
    .p#{$selector} {
      @each $property in $properties {
        padding-#{$property}: auto;
      }
    }
    
    @for $i from 0 through 10 {
      $selector: selector($direction, $breakpoint, $i);
      $amount: pixels($i);
    
      .m#{$selector} {
        @each $property in $properties {
          margin-#{$property}: $amount;
        }
      }
      
      .p#{$selector} {
        @each $property in $properties {
          padding-#{$property}: $amount;
        }
      }
    }
  }
}

:root {
  @for $i from 0 through 10 {
    $amount: pixels($i);

    --p-#{$i}: #{$amount};
    --m-#{$i}: #{$amount};
  }
}

@each $breakpoint, $value in variables.$breakpoints {
  @if $breakpoint == '' {
    @include classesForBreakpoint($breakpoint);
  }
  @else {
    @media (min-width: $value) {
      @include classesForBreakpoint($breakpoint);
    }
  }
}
