//------------------------------------------------------------------------------------------------
// EVO TOOLKIT SETTINGS
//------------------------------------------------------------------------------------------------

/*
Various settings and helper functions for use by the framework.
*/


// HELPERS
//------------------------------------------------------------------------------------------------

// SASS string replace function
@function str-replace($string, $search, $replace: '') {
	$index: str-index($string, $search);
	@if $index {
		@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
	}
	@return $string;
}

// Framework helper function to sort maps by their PX values, smallest to largest
@function map-sort($map) {
	// Transform map to zipped list
	$keys: ();
	$values: ();
	@each $key, $val in $map {
		$keys: append($keys, $key);
		$values: append($values, $val);
	}
	$list: zip($keys, $values);
	$sortedMap: ();
	@while length($list) > 0 {
		// Find smallest pair
		$smallestPair: nth($list, 1);
		@each $pair in $list {
			$value: nth($pair, 2);
			$smallestValue: nth($smallestPair, 2);
			@if $value < $smallestValue {
				$smallestPair: $pair;
			}
		}
		// Add smallest pair to sorted map
		$key: nth($smallestPair, 1);
		$value: nth($smallestPair, 2);
		$sortedMap: map-merge($sortedMap, ($key: $value));
		// Remove from list smallest pair
		$smallestPairIndex: index($list, $smallestPair);
		$newList: ();
		@for $i from 1 through length($list) {
			@if $i != $smallestPairIndex {
				$newList: append($newList, nth($list, $i), 'space');
			}
		}
		$list: $newList;
	}
	@return $sortedMap;
}

// Function to amend maps, allowing new values to be added and existing ones overriden
@function amend-map($oldMap, $newMap) {
	@each $key, $value in $newMap {
		@if map-has-key($oldMap, $key) {
			@if not type-of($value) == 'list' {
				$oldMap: map-remove($oldMap, $key);
			}
			@else {
				$newMap: map-merge($newMap, (
					$key: $value
				));
			}
		}
	}
	@return map-merge($oldMap, $newMap);
}