@use 'sass:math';
@use 'sass:color';
@use 'sass:map';
@use 'sass:meta';

@function hex-to-rgb($hex) {
	@return rgb(
		color.channel($hex, 'red', $space: rgb),
		color.channel($hex, 'green', $space: rgb),
		color.channel($hex, 'blue', $space: rgb)
	);
}

@function rgb-values($color) {
	@return '#{color.channel($color, "red", $space: rgb)}, #{color.channel($color, "green", $space: rgb)}, #{color.channel($color, "blue", $space: rgb)}';
}

@function generate-color-scale($base-color) {
	$scale: (
		100: color.scale(hex-to-rgb($base-color), $lightness: 40%),
		200: color.scale(hex-to-rgb($base-color), $lightness: 30%),
		300: color.scale(hex-to-rgb($base-color), $lightness: 20%),
		400: color.scale(hex-to-rgb($base-color), $lightness: 10%),
		500: hex-to-rgb($base-color),
		600: color.scale(hex-to-rgb($base-color), $lightness: -10%),
		700: color.scale(hex-to-rgb($base-color), $lightness: -20%),
		800: color.scale(hex-to-rgb($base-color), $lightness: -30%),
		900: color.scale(hex-to-rgb($base-color), $lightness: -40%),
	);

	@return $scale;
}

@function generate-theme-scale($base-colors-map) {
	$theme: ();

	@each $key, $base-color in $base-colors-map {
		$theme: map.merge(
			$theme,
			(
				$key: generate-color-scale($base-color),
			)
		);
	}

	@return $theme;
}

@function calc-luminance($color) {
	$r: math.div(color.channel($color, 'red', $space: rgb), 255);
	$g: math.div(color.channel($color, 'green', $space: rgb), 255);
	$b: math.div(color.channel($color, 'blue', $space: rgb), 255);

	@return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b);
}

@function apply-text-color($background-color, $black, $white) {
	@if calc-luminance($background-color) > 0.5 {
		@return $black;
	} @else {
		@return $white;
	}
}

@function tint-shade($color, $amount, $mode: 'lighten') {
	@if $mode == 'lighten' {
		@return color.mix(white, $color, $amount);
	} @else if $mode == 'darken' {
		@return color.mix(black, $color, $amount);
	} @else {
		@error "Invalid mode '#{$mode}' for tint-shade(). Use 'lighten' or 'darken'.";
	}
}
