/* postcss-sorting: off */

/*
 * Space offset mixin
 *
 * @uses: (array)$space-units
 *
 * @para: (int)$top-multiplier
 * @para: (int)$bottom-multiplier
 * @para: (string)$top-multiplier
 * @para: (int)$correction-multiplier
 *
 * TODO optimize situation were 4 sides have the same value
 *
 */

@mixin space-offset(
	$top: false,
	$right:false,
	$bottom: false,
	$left: false,
	$type: margin
) {
	$space-side-map: (
		'top': $top,
		'right': $right,
		'bottom': $bottom,
		'left': $left
	);

	// If all sides have equal values. Use CSS shorthand. For example: margin: $value;
	@if ($top == $right and $right == $bottom and $bottom == $left) {
		$value: $top;
		$attribute: $type;

		@include property($attribute, $value);
	}@else { // If not all sides have equal values use separate classes. For example: margin-top: $value;
		@each $side, $side-value in $space-side-map {
			$attribute: $side;

			@if (space-prefix-type($type)) {
				$attribute: #{$type}-#{$side};
			}

			@if $side-value != false {
				$value: $side-value; // strip units because of register-su-var

				@include property($attribute, $value);
			}
		}
	}
}

/**
 * SPACE HELPERS
 * @private mixins
 */

@mixin space-offset-sides($top, $right, $bottom, $left, $type) {
	// If only $top value is given than make $right equal to top
	@if ($right == default) {
		$right: $top;
	}

	// If $left value is default than make $left equal to $right
	@if ($left == default) {
		$left: $right;
	}

	// If $right has a value and $bottom has no value make $bottom equal to $right
	@if ($bottom == default) {
		$bottom: $top;
	}

	@include space-offset($top, $right, $bottom, $left, $type);
}

@mixin space-offset-vertical($top, $bottom, $type) {
	@if ($bottom == default) {
		$bottom: $top;
	}

	@include space-offset($top, false, $bottom, false, $type);
}

@mixin space-offset-horizontal($left, $right, $type) {
	@if ($right == default) {
		$right: $left;
	}

	@include space-offset(false, $right, false, $left, $type);
}

/**
 * SPACE MARGIN MIXINS
 * @public mixins
 */

@mixin margin($top: false, $right: default, $bottom: default , $left: default) {
	$type: margin;

	@include space-offset-sides($top, $right, $bottom, $left, $type);
}

// Directions
@mixin margin-vertical($top: false, $bottom: default) {
	$type: margin;

	@include space-offset-vertical($top, $bottom, $type);
}

@mixin margin-horizontal($left: false, $right: default) {
	$type: margin;

	@include space-offset-horizontal($left, $right, $type);
}

// Sides
@mixin margin-top($top: false) {
	$type: margin;

	@include space-offset($top, false, false, false, $type);
}

@mixin margin-right($right: false) {
	$type: margin;

	@include space-offset(false, $right, false, false, $type);
}

@mixin margin-bottom($bottom: false) {
	$type: margin;

	@include space-offset(false, false, $bottom, false, $type);
}

@mixin margin-left($left: false) {
	$type: margin;

	@include space-offset(false, false, false, $left, $type);
}

/**
 * SPACE PADDING MIXINS
 * @public mixins
 */

@mixin padding($top: false, $right: default, $bottom: default, $left: default) {
	$type: padding;

	@include space-offset-sides($top, $right, $bottom, $left, $type);
}

// Directions
@mixin padding-vertical($top: false, $bottom: default) {
	$type: padding;

	@include space-offset-vertical($top, $bottom, $type);
}

@mixin padding-horizontal($left: false, $right: default) {
	$type: padding;

	@include space-offset-horizontal($left, $right, $type);
}

// Sides
@mixin padding-top($top: false) {
	$type: padding;

	@include space-offset($top, false, false, false, $type);
}

@mixin padding-right($right: false) {
	$type: padding;

	@include space-offset(false, $right, false, false, $type);
}

@mixin padding-bottom($bottom: false) {
	$type: padding;

	@include space-offset(false, false, $bottom, false, $type);
}

@mixin padding-left($left: false) {
	$type: padding;

	@include space-offset(false, false, false, $left, $type);
}

// SPACE POSITION MIXINS
@mixin top($top: false) {
	$type: position;

	@include space-offset($top, false, false, false, $type);
}

@mixin right($right: false) {
	$type: position;

	@include space-offset(false, $right, false, false, $type);
}

@mixin bottom($bottom: false) {
	$type: position;

	@include space-offset(false, false, $bottom, false, $type);
}

@mixin left($left: false) {
	$type: position;

	@include space-offset(false, false, false, $left, $type);
}

/* postcss-sorting: on */
