@use "sass:map";
@use "sass:meta";
@use "@theholocron/config/src" as config;
@use "@theholocron/scss-mixins/src/at-rules/breakpoints" as *;
@use "@theholocron/scss-mixins/src/box-model/" as *;

/// The margin property
/// @since 0.1.0
/// @link https://developer.mozilla.org/en-US/docs/Web/CSS/margin
/// @param {String} $className ["margin"] - a CSS class name that will prefix all helpers
/// @param {Map} $breakpoints [config.$breakpoints] - a map of grid breakpoints
/// @param {Map} $spacing [config.$spacing] - a map of spacing
/// @example scss
///   @include margin($className: m, $breakpoints: ("sm": 480px), $spacing: ("sm": 480px));
@mixin margin($className: "margin", $breakpoints: config.$breakpoints, $spacing: config.$spacing) {
	.#{$className} {
		@each $key, $value in map.merge((auto: auto), $spacing) {
			@each $abbrev, $direction in config.$box-directions {
				@if $abbrev == "all" {
					&-#{$key} {
						@include box-model($property: "margin", $value: $value, $direction: $abbrev);
					}

					@each $breakpoint in map.keys($breakpoints) {
						@include media-breakpoint-up($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$key}-#{$breakpoint},
							&-#{$key}-#{$breakpoint}-up {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-minus-#{$key}-#{$breakpoint},
								&-minus-#{$key}-#{$breakpoint}-up {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}

						@include media-breakpoint-down($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$key}-#{$breakpoint}-down {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-minus-#{$key}-#{$breakpoint}-down {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}

						@include media-breakpoint-only($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$key}-#{$breakpoint}-only {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-minus-#{$key}-#{$breakpoint}-only {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}
					}
				} @else {
					&-#{$abbrev}-#{$key} {
						@include box-model($property: "margin", $value: $value, $direction: $abbrev);
					}

					@each $breakpoint in map.keys($breakpoints) {
						@include media-breakpoint-up($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$abbrev}-#{$key}-#{$breakpoint},
							&-#{$abbrev}-#{$key}-#{$breakpoint}-up {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-#{$abbrev}-minus-#{$key}-#{$breakpoint},
								&-#{$abbrev}-minus-#{$key}-#{$breakpoint}-up {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}

						@include media-breakpoint-down($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$abbrev}-#{$key}-#{$breakpoint}-down {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-#{$abbrev}-minus-#{$key}-#{$breakpoint}-down {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}

						@include media-breakpoint-only($name: $breakpoint, $breakpoints: $breakpoints) {
							&-#{$abbrev}-#{$key}-#{$breakpoint}-only {
								@include box-model($property: "margin", $value: $value, $direction: $abbrev);
							}

							@if meta.type-of($value) == "number" and $value > 0 {
								&-#{$abbrev}-minus-#{$key}-#{$breakpoint}-only {
									@include box-model($property: "margin", $value: - $value, $direction: $abbrev);
								}
							}
						}
					}
				}
			}
		}
	}
}
