/* Var
 -------------------------- */
$B: '';
$E: '';

$element-separator: '__';
$modifier-separator: '--';
$state-is-prefix: 'is-';
$state-has-prefix: 'has-';

/* Func
 -------------------------- */
@function selectorToString($selector) {
	$selector: inspect($selector);
	$selector: str-slice($selector, 2, -2);
	@return $selector;
}

@function containsModifier($selector) {
	$selector: selectorToString($selector);

	@if str-index($selector, $modifier-separator) {
		@return true;
	} @else {
		@return false;
	}
}

@function containWhenFlag($selector) {
	$selector: selectorToString($selector);

	@if str-index($selector, '.' + $state-is-prefix) or str-index($selector, '.' + $state-has-prefix) {
		@return true;
	} 
	@else {
		@return false;
	}
}

@function containPseudoClass($selector) {
	$selector: selectorToString($selector);

	@if str-index($selector, ':') {
		@return true;
	} 
	@else {
		@return false;
	}
}

@function hitAllSpecialNestRule($selector) {

	@return containsModifier($selector) or containWhenFlag($selector) or containPseudoClass($selector);
}

/* Mix
 -------------------------- */

@mixin block($block) {
	$B: $block !global;

	.#{$block} {
		@content;
	}
}

/**
 * @param $root: 默认置顶
 */
@mixin element($element, $root: true) {
	$E: $element !global;
	$selector: &;
	$currentSelector: "";
	// 支持数组
	@each $unit in $element {
		$currentSelector: #{$currentSelector + "." + $B + $element-separator + $unit + ","};
	}

	@if hitAllSpecialNestRule($selector) {
		@at-root {
			#{$selector} {
				#{$currentSelector} {
					@content;
				}
			}
		}
	} @else if $root {
		@at-root {
			#{$currentSelector} {
				@content;
			}
		}
	} @else {
		#{$currentSelector} {
			@content;
		}
	}
}

@mixin modifier($modifier, $root: true) {
	$selector: &;
	// 支持数组
	$currentSelector: "";
	@each $unit in $modifier {
		$currentSelector: #{$currentSelector + & + $modifier-separator + $unit + ","};
	}

	// TODO: 待验证第一个判断;
	@if hitAllSpecialNestRule($selector) {
		@at-root {
			#{$selector} {
				.#{$B + $currentSelector} {
					@content;
				}
			}
		}
	} @else if $root {
		@at-root {
			#{$currentSelector} {
				@content;
			}
		}
	} @else {
		#{$currentSelector} {
			@content;
		}
	}
}

@mixin configurable-m($modifier, $E-flag: false) {
	$selector: &;
	$interpolation: '';

	@if $E-flag {
		$interpolation: $element-separator + $E-flag;
	}

	@at-root {
		#{$selector} {
			.#{$B + $interpolation + $modifier-separator + $modifier} {
				@content;
			}
		}
	}
}

/**
 * 1. +, >, ~, ' '
 * 2. 同element
 * 3. 同modifier
 * 4. 同block
 */
@mixin spec-selector($specSelector: '', $element: $E, $modifier: false, $block: $B) {
	$modifierCombo: '';

	@if $modifier {
		$modifierCombo: $modifier-separator + $modifier;
	}

	@at-root {
		#{&}#{$specSelector}.#{$block + $element-separator + $element + $modifierCombo} {
			@content;
		}
	}
}

@mixin meb($modifier: false, $element: $E, $block: $B) {
	$selector: &;
	$modifierCombo: '';

	@if $modifier {
		$modifierCombo: $modifier-separator + $modifier;
	}

	@at-root {
		#{$selector} {
			.#{$block + $element-separator + $element + $modifierCombo} {
				@content;
			}
		}
	}
}

/**
 * -> [className].is_require 
 * 与xxx:hover类似
 */
@mixin when($state, $useHas: false) {
	$currentPrefix: $state-is-prefix;

	@if $useHas {
		$currentPrefix: $state-has-prefix;
	}

	@at-root {
		&.#{$currentPrefix + $state} {
			@content;
		}
	}
}

/**
 * 用 @at-root 来重置 & 读取次数的计数
 * 如 [clsssName] [className]:hover
 */
@mixin pseudo($pseudo) {
	@at-root #{&}#{':#{$pseudo}'} {
		@content;
	}
}

/**
 * 自定义规则
 */
@mixin extend-rule($name) {
	@extend #{'%shared-'+$name};
}

@mixin share-rule($name) {
	$rule-name: '%shared-'+$name;

	@at-root #{$rule-name} {
		@content;
	}
}
