@use 'sass:list';
@use 'sass:map';
@use 'sass:string';
@use '../tokens/variables';

////
/// @group global

/// Returns the safe area value with fallback for both env() and CSS variable
/// @param {String} $side - The side (top, right, bottom, left)
/// @return {String} - The max() expression with env() and CSS variable fallback
@function safe-area($side) {
	@return string.unquote('max(env(safe-area-inset-#{$side}), var(--safe-area-inset-#{$side}, 0px))');
}

/// Returns the safe area top value for specifically for Android devices.
/// @return {String} - The max() expression with current variable and new CSS variable
@function android-safe-area-top() {
	@return string.unquote('max(var(--status-bar-height, 0px), var(--safe-area-inset-top, 0px))');
}

/// Focus ring. $isInner only moves that ring inside the element.
/// @param {Boolean} $isInner [false] - Draw the ring inside the element when true.
@mixin a11y-focus-shadow($isInner: false) {
	@if $isInner {
		box-shadow: inset
			0
			0
			0
			calc(var(--osui-focus-inner-shadow-gap) + var(--osui-focus-shadow-width))
			var(--osui-focus-shadow-color);
	} @else {
		box-shadow:
			0 0 0 var(--osui-focus-shadow-gap) var(--color-background-surface),
			0 0 0 calc(var(--osui-focus-shadow-gap) + var(--osui-focus-shadow-width)) var(--osui-focus-shadow-color);
	}

	outline: none;
	outline-offset: variables.$token-border-size-0;
}

/// Focus outline. Used for special cases where the focus ring is not enough, such Carousel arrows, where the outline offset is not possible with the focus ring.
@mixin a11y-focus-outline() {
	box-shadow: none;
	outline: var(--osui-focus-shadow-width) solid var(--osui-focus-shadow-color);
	outline-offset: var(--osui-focus-shadow-gap);
}

/// outline style for high contrast mode
@mixin a11y-high-contrast-outline($isBorder: false, $size: variables.$token-border-size-075, $borderType: 'all') {
	@if $isBorder == false {
		outline: $size solid variables.$token-primitives-yellow-500;
	} @else {
		@if $borderType == 'all' {
			border: $size solid variables.$token-primitives-yellow-500;
		} @else {
			border-#{$borderType}: $size solid variables.$token-primitives-yellow-500;
		}
	}
}

/// AppLogo
@mixin app-logo {
	border-radius: variables.$token-border-radius-100;
	margin-inline-end: variables.$token-scale-200;
	max-height: var(--header-size);
	max-width: 120px;
}

///
@mixin absolute-center() {
	left: 50%;
	position: absolute;
	top: 50%;
	transform: translate(-50%, -50%);
}

///
@mixin absolute-left-top($left: 0, $top: 0) {
	inset-inline-start: $left;
	position: absolute;
	top: $top;
}

/* ============================================================================ */
/* Typography                                                                   */
/* ============================================================================ */

/// Applies all 7 properties of a typography role map in place.
/// Consumer may still override individual properties after the @include.
/// @param {Map} $role - A $token-*-* role map (e.g. $token-heading-h1-semi-bold)
@mixin apply-typography($role) {
	font-size: map.get($role, font-size);
	font-style: map.get($role, font-style);
	font-weight: map.get($role, font-weight);
	letter-spacing: map.get($role, letter-spacing);
	line-height: map.get($role, line-height);
	text-transform: map.get($role, text-transform);
	text-decoration: map.get($role, text-decoration);
}

/// Emits the desktop rule at the current selector, plus optional
/// `.tablet &` / `.phone &` overrides — but only when the tier role
/// actually differs from the one it would otherwise inherit.
/// @param {Map} $desktop - Required. Baseline role applied at the selector root.
/// @param {Map} $tablet  - Optional. If null or == $desktop, no `.tablet &` block emitted.
/// @param {Map} $phone   - Optional. If null or == (tablet or desktop), no `.phone &` block emitted.
@mixin apply-typography-responsive($desktop, $tablet: null, $phone: null) {
	@include apply-typography($desktop);

	@if $tablet != null and $tablet != $desktop {
		.tablet & {
			@include apply-typography($tablet);
		}
	}

	// The phone block is only emitted when it differs from what it would inherit,
	// which is the tablet role when there is one and the desktop role otherwise.
	// Written as @if rather than the if() function: that function is deprecated
	// (sass-lang.com/d/if-function) in favour of a newer if(sass(...)) syntax that
	// this repo's declared Sass range (^1.77.4) does not guarantee. @if has never
	// been deprecated and behaves identically here - assigning inside the block
	// updates the local declared just above it rather than shadowing it.
	$phone-parent: $desktop;
	@if $tablet != null {
		$phone-parent: $tablet;
	}
	@if $phone != null and $phone != $phone-parent {
		.phone & {
			@include apply-typography($phone);
		}
	}
}
