@use "sass:map";
@use "sass:math";
@use "sass:meta";

$fontSizes: (
	bigger: var(--bigger-font-size),
	title1: var(--h1-font-size),
	title2: var(--h2-font-size),
	title3: var(--h3-font-size),
	normal: var(--normal-font-size),
	small: var(--small-font-size),
	smaller: var(--smaller-font-size),
	supersmall: var(--supersmall-font-size),
);

// Breakpoints
$breakpoints: (
	s: 0,
	fold: 280px,
	sm: 320px,
	lm: 360px,
	m: 410px,
	xm: 640px,
	ipad: 768px,
	lg: 1024px,
	xl: 1440px,
) !default;

$sizes: map.keys($breakpoints);

/// Devuelve true si el breakpoint es parte del core
/// @author Raúl Sanjuán
/// @param   {number} $bp - breakpoint
/// @example scss
///
@function isCoreBreakpoint($bp) {
	@if map.get($breakpoints, $bp) {
		@return true;
	} @else {
		@return false;
	}
}

/// Devuelve true si el breakpint es válido (em, rem, px)
/// @author  Raul Sanjuan
/// @param   {number} $bp - breakpoint a verificar.
@function isValidBreakpoint($bp) {
	@if meta.type-of($bp) ==
		number and
		(
			math.unit($bp) ==
				"px" or
				math.unit($bp) ==
				"em" or
				math.unit($bp) ==
				"rem"
		)
	{
		@return true;
	} @else {
		@return false;
	}
}

/// Obtiene un breakpoint del core
/// @author  Raul Sanjuan
/// @param   {number} $bp - breakpoint a obtener.
@function getBp($bp) {
	@return map.get($breakpoints, $bp);
}

/// Convierte un valor en em o rem a px
/// @author  Raul Sanjuan
/// @param   {number} $value - var a convertir a pixeles.

@function toPx($value) {
	@if meta.type-of($value) == number {
		@if unit($value) == "em" {
			@return calc(#{math.div($value, 1em)} * 16px);
		} @else if unit($value) == "rem" {
			@return calc(#{math.div($value, 1rem)} * 16px);
		} @else if unit($value) == "px" {
			@return $value;
		} @else {
			@error "La función toPx() solo acepta números en em, rem o px";
		}
	} @else {
		@error "La función toPx() solo acepta números en px, em o rem";
	}
}

/// Convierte un valor en em o px a rem
/// @author  Raul Sanjuan
/// @param   {number} $value - var a convertir a rem.

@function toRem($value) {
	@if meta.type-of($value) == number {
		@if math.unit($value) == "px" {
			@return math.div($value, 16px) * 1rem;
		} @else {
			@error "La funcion toRem() solo acepta números en px";
		}
	} @else {
		@error "La funcion toRem() solo acepta números en px";
	}
}

/// Convierte un valor en rem o px a em
/// @author  Raul Sanjuan
/// @param   {number} $value - var a convertir a em.

@function toEm($value, $context: 16px) {
	@if meta.type-of($value) == number {
		@if unit($value) == "px" {
			@return calc(#{math.div($value, $context)} * 1em);
		} @else {
			@error "La función toEm() solo acepta números en px";
		}
	} @else {
		@error 'La función toEm() requiere una unidad de pixeles como primer argumento (el segundo es opcional)';
	}
}

// replace substring with another string
// credits: https://css-tricks.com/snippets/sass/str-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;
}

@function lightness($color, $lightnessMultiplier) {
	$color: str-replace($color, "var(");
	$color: str-replace($color, ")");
	$color-h: var(#{$color + "-h"});
	$color-s: var(#{$color + "-s"});
	$color-l: var(#{$color + "-l"});
	@return hsl(
		$color-h,
		$color-s,
		calc(#{$color-l} * #{$lightnessMultiplier})
	);
}

@function hexToRGB($hex) {
	@return red($hex), green($hex), blue($hex);
}

// Get font size from map
@function fontSize($size) {
	@if map-has-key($fontSizes, $size) {
		@return map-get($fontSizes, $size);
	} @else {
		@error '$size is not valid, valid values are title1, title2, title3, normal, small, smaller, supersmall';
	}
}
