@import "functions";
@import "palette";

/**
 * Returns a HSL string with fallback.
 */
@function construct-hsl ($hsl-var-name, $hue-var-name, $saturation-var-name, $lightness-var-name, $hsl, $alpha: null) {
	$h: nth($hsl, 1);
	$s: nth($hsl, 2);
	$l: nth($hsl, 3);

	@if ($alpha and $alpha != 1) {
		@return #{'hsla(var(--#{$hsl-var-name}, var(--#{$hue-var-name}, #{$h}), var(--#{$saturation-var-name}, #{$s}), var(--#{$lightness-var-name}, #{$l})), #{$alpha})'};

	} @else {
		@return #{'hsl(var(--#{$hsl-var-name}, var(--#{$hue-var-name}, #{$h}), var(--#{$saturation-var-name}, #{$s}), var(--#{$lightness-var-name}, #{$l})))'};
	}
}

/**
 * Returns a color variable with fallback.
 */
@function hsl-fallback ($palette, $name, $hue: 500, $alpha: null) {
	@return construct-hsl(
			$hsl-var-name: "#{$name}-#{$hue}",
			$hue-var-name: "#{$name}-hue",
			$saturation-var-name: "#{$name}-saturation",
			$lightness-var-name: "#{$name}-lightness",
			$hsl: get-entry($palette, $hue),
			$alpha: $alpha
	);


}

/**
 * Returns a color contrast variable with fallback.
 */
@function hsl-contrast-fallback ($palette, $name, $hue: 500, $alpha: null) {
	@return construct-hsl(
			$hsl-var-name: "#{$name}-#{$hue}-contrast",
			$hue-var-name: "#{$name}-hue-contrast",
			$saturation-var-name: "#{$name}-saturation-contrast",
			$lightness-var-name: "#{$name}-lightness-contrast",
			$hsl: get-mapped-entry($palette, "contrast", $hue),
			$alpha: $alpha
	);
}

/**
 * Returns a color variable.
 */
@function color ($name, $hue: $default-hue, $alpha: null, $palette-map: $palette-map) {
	$palette: get-entry($palette-map, $name);
	@return hsl-fallback($palette, $name, $hue, $alpha);
}

/**
 * Returns a color contrast variable.
 */
@function color-contrast ($name, $hue: $default-hue, $alpha: null, $palette-map: $palette-map) {
	$palette: get-entry($palette-map, $name);
	@return hsl-contrast-fallback($palette, $name, $hue, $alpha);
}

$foreground: construct-hsl(
		$hsl-var-name: "foreground",
		$hue-var-name: "foreground-hue",
		$saturation-var-name: "foreground-saturation",
		$lightness-var-name: "foreground-lightness",
		$hsl: (230, 70%, 5%)
);

$background: construct-hsl(
		$hsl-var-name: "background",
		$hue-var-name: "background-hue",
		$saturation-var-name: "background-saturation",
		$lightness-var-name: "background-lightness",
		$hsl: (0, 0%, 100%)
);

$shadow: construct-hsl(
		$hsl-var-name: "shadow",
		$hue-var-name: "shadow-hue",
		$saturation-var-name: "shadow-saturation",
		$lightness-var-name: "shadow-lightness",
		$hsl: (230, 70%, 5%),
		$alpha: 0.15
);
