@use 'sass:meta';
@use 'sass:string';

@use '@lucca-front/scss/src/commons/config';
@use '@lucca-front/icons/src/commons/core' as transform;

$contents: '100\\%', 'fit-content';
$boxModel: 'margin', 'padding', 'border', 'inset';
$boxDirection: '', 'top', 'bottom', 'left', 'right', 'inline', 'block', 'block-start', 'block-end', 'inline-start', 'inline-end'; // top, bottom, left and right are deprecated
$corners: '', 'top-left-', 'top-right-', 'bottom-left-', 'bottom-right-', 'start-start-', 'start-end-', 'end-start-', 'end-end-'; // top-left, top-right, bottom-left and bottom-right are deprecated
$gaps: 'column-gap', 'row-gap', 'gap';
$displays: 'block', 'flex', 'grid', 'inline', 'inline-flex', 'inline-block', 'inline-grid', 'contents', 'none';
$direction: 'column', 'column-reverse', 'row', 'row-reverse';
$wrap: 'wrap', 'nowrap', 'wrap-reverse';
$justify: 'flex-start', 'flex-end', 'space-around', 'space-between', 'space-evenly', 'center';
$align: 'flex-start', 'flex-end', 'baseline', 'stretch', 'center';
$flex: '0', '1';
$basis: '0', 'auto';
$order: '-1', '1';
$textAlign: 'left', 'center', 'right', 'start', 'end'; // left and right are deprecated
$visibility: 'visible', 'hidden', 'collapse';
$fontWeight: '400', '600', '700', '900', 'normal', 'bold';
$fontStyle: 'normal', 'italic';
$pointerEvents: 'none', 'auto';
$scrollBehavior: 'auto', 'smooth';
$whiteSpace: 'normal', 'nowrap', 'pre-line';
$float: 'left', 'right', 'inline-start', 'inline-end'; // left and right are deprecated
$verticalAlign: 'baseline', 'sub', 'super', 'text-top', 'text-bottom', 'middle', 'top', 'bottom';
$position: 'absolute', 'relative', 'static', 'fixed', 'sticky';
$decoration: 'underline', 'line-through', 'none';
$overflow: 'hidden', 'auto', 'visible', 'scroll';
$cursor: 'pointer', 'auto', 'default', 'text';

// Tokens

@mixin spacing($boxModel, $boxDirection, $key, $value, $suffix: '!important') {
	$boxDirectionString: '-';

	@if $boxDirection == '' {
		$boxDirectionString: '';
	}

	@if $boxModel != 'inset' or ($boxDirection != 'top' and $boxDirection != 'bottom' and $boxDirection != 'left' and $boxDirection != 'right') {
		.pr-u-#{transform.camelize($boxModel)}#{transform.capitalize(transform.camelize($boxDirection))}#{transform.capitalize($key)} {
			#{$boxModel}#{$boxDirectionString}#{$boxDirection}: var(--pr-t-spacings-#{$key}) #{$suffix};
		}
	}
}

@mixin spacings {
	@each $boxModel in $boxModel {
		@each $boxDirection in $boxDirection {
			@each $key, $value in config.$spacings {
				@if $boxModel != 'border' or $value == 0 {
					@include spacing($boxModel, $boxDirection, $key, $value);
				}
			}

			@if $boxModel == 'margin' {
				@include spacing($boxModel, $boxDirection, $key: 'auto', $value: auto);
			}
		}
	}

	@each $gap in $gaps {
		@each $key, $value in config.$spacings {
			@include spacing($gap, $boxDirection: '', $key: $key, $value: $value);
		}
	}
}

@mixin borderRadiusTokens($suffix: '!important') {
	@each $key, $value in config.$borderRadiusTokens {
		.pr-u-borderRadius#{transform.capitalize($key)} {
			border-radius: var(--pr-t-border-radius-#{$key}) #{$suffix};
		}
	}
}

// Variables

@mixin cssvars($name, $properties, $after: '') {
	@each $key, $value in $properties {
		$keyString: '-';

		@if $key == '' {
			$keyString: '';
		}

		--#{$name}#{$keyString}#{$key}#{$after}: #{$value};
	}
}

@mixin classes($name, $properties, $suffix: '!important', $prefix: '') {
	@each $property in $properties {
		$minusString: '';

		@if string.slice($property, 1, 1) == '-' {
			$minusString: 'Minus';
		}

		@if $prefix == '' {
			.pr-u-#{transform.camelize($name)}#{$minusString}#{transform.capitalize(transform.camelize($property))} {
				#{$name}: #{transform.replace($property, '\\', '')} #{$suffix};
			}

			// Deprecated .u- utilities
			@if config.$deprecatedUtilityPrefix {
				.u-#{transform.camelize($name)}#{$minusString}#{transform.capitalize(transform.camelize($property))} {
					#{$name}: #{transform.replace($property, '\\', '')} #{$suffix};
				}
			}
		} @else {
			.pr-u-#{$prefix}#{transform.capitalize(transform.camelize($name))}#{$minusString}#{transform.capitalize(transform.camelize($property))} {
				#{$name}: #{transform.replace($property, '\\', '')} #{$suffix};
			}

			// Deprecated .u- utilities
			@if config.$deprecatedUtilityPrefix {
				.u-#{$prefix}#{transform.capitalize(transform.camelize($name))}#{$minusString}#{transform.capitalize(transform.camelize($property))} {
					#{$name}: #{transform.replace($property, '\\', '')} #{$suffix};
				}
			}
		}
	}
}

@mixin palettes($suffix: '!important') {
	// .pr-u-text is deprecated
	@each $palette in config.$palettesAll {
		.pr-u-text#{transform.capitalize($palette)}, .pr-u-colorText#{transform.capitalize($palette)} {
			color: var(--palettes-#{$palette}-700) #{$suffix};
		}

		// Deprecated .u- utilities
		@if config.$deprecatedUtilityPrefix {
			.u-text#{transform.capitalize($palette)} {
				color: var(--palettes-#{$palette}-700) #{$suffix};
			}
		}
	}
}

@mixin rosetta($before, $after, $iterations) {
	@if meta.type-of($iterations) == map {
		@each $iterationBefore, $iterationAfter in $iterations {
			@if meta.type-of($iterationAfter) == string {
				#{$before}-#{$iterationBefore}: var(#{$after}-#{$iterationAfter});
			} @else {
				#{$before}-#{$iterationBefore}: #{$iterationAfter};
			}
		}
	} @else {
		@each $iteration in $iterations {
			#{$before}-#{$iteration}: var(#{$after}-#{$iteration});
		}
	}
}

@mixin fontFace($family, $weights, $styles, $path: '//cdn.lucca.fr/transverse/fonts') {
	@each $keyword, $number in $weights {
		@each $style in $styles {
			@if $style == 'normal' {
				@font-face {
					font-family: '#{$family}';
					src:
						url('#{$path}/#{$family}/#{$keyword}.woff2') format('woff2'),
						url('#{$path}/#{$family}/#{$keyword}.woff') format('woff');
					font-weight: #{$number};
					font-style: #{$style};
					font-display: swap;
				}
			} @else {
				@font-face {
					font-family: '#{$family}';
					src:
						url('#{$path}/#{$family}/#{$keyword}-#{$style}.woff2') format('woff2'),
						url('#{$path}/#{$family}/#{$keyword}-#{$style}.woff') format('woff');
					font-weight: #{$number};
					font-style: #{$style};
					font-display: swap;
				}
			}
		}
	}
}

// DEPRECATED

@mixin sizes($suffix: '!important') {
	@each $key, $value in config.$sizes {
		.pr-u-text#{transform.capitalize($key)} {
			font-size: var(--sizes-#{$key}-fontSize) #{$suffix};
			line-height: var(--sizes-#{$key}-lineHeight) #{$suffix};
		}

		// Deprecated .u- utilities
		@if config.$deprecatedUtilityPrefix {
			.u-text#{transform.capitalize($key)} {
				font-size: var(--sizes-#{$key}-fontSize) #{$suffix};
				line-height: var(--sizes-#{$key}-lineHeight) #{$suffix};
			}
		}
	}
}

@mixin borderRadius($suffix: '!important') {
	@each $corner in $corners {
		@each $key, $value in config.$borderRadius {
			.pr-u-border#{transform.capitalize(transform.camelize($corner))}Radius#{transform.capitalize($key)} {
				border-#{$corner}radius: var(--commons-borderRadius-#{$key}) #{$suffix};
			}

			// Deprecated .u- utilities
			@if config.$deprecatedUtilityPrefix {
				.u-border#{transform.capitalize(transform.camelize($corner))}Radius#{transform.capitalize($key)} {
					border-#{$corner}radius: var(--commons-borderRadius-#{$key}) #{$suffix};
				}
			}
		}
	}
}
