////
/// @author David Khourshid
////

/// Replaces a batch of substrings (needles) in a string (haystack)
/// with a single replacement string.
/// @since 2.0.0
/// @param {String} $haystack - string to perform search and replacement on
/// @param {List | String} $needles - string or list of strings to replace globally
/// @param {String} $replacement ('') - replacement string to replace needles
/// @return {String} replaced string
@function str-replace-batch($haystack, $needles, $replacement: "") {
	$instances: false;

	@if not type-of($needles) == list {
		$needles: ($needles);
	}

	@while ($instances == false) or ($instances > 0) {
		$instances: 0;

		@each $needle in $needles {
			$needle-index: str-index($haystack, $needle);
			$instances: $instances + if($needle-index, 1, 0);

			@if $needle-index {
				$haystack: str-slice($haystack, 1, $needle-index - 1) + str-slice($haystack, $needle-index + str-length($needle), -1);
				$haystack: str-insert($haystack, $replacement, $needle-index);
			}
		}
	}

	@return $haystack;
}
