// Get mapped color
// @function color($key) {
//     @if ($dark-theme and map-has-key($colors-dark, $key)) {     // If dark theme is enabled use colors-dark If color is not available in colors-dark, use regular color palette
//         @return map-get($colors-dark, $key);
//     } @else {
//         @return map-get($colors, $key);
//     }
// }
@function color($key, $shade: null) {
    $palette: if($dark-theme, $colors-dark, $colors);
    
    @if ($shade != null) {
        // e.g. color('neutral', 90) → map-get(map-get($palette, 'neutral'), 90)
        @if (map-has-key($palette, $key)) {
            $scale: map-get($palette, $key);
            @if map-has-key($scale, $shade) {
                @return map-get($scale, $shade);
            }
        }
    }
  
    // Fallback: flat colors like primary, accent, etc.
    @if (map-has-key($palette, $key)) {
      @return map-get($palette, $key);
    }
  
    // Optional: fallback color
    @warn "Color `#{$key}`#{if($shade != null, " tone '#{$shade}'", "")} not found.";
    @return null;
}


// Get mapped custom color
@function custom-color($key, $custom-colors-light, $custom-colors-dark) {
    @if ($dark-theme and map-has-key($custom-colors-dark, $key)) {
        @return map-get($custom-colors-dark, $key);
    } @else {
        @return map-get($custom-colors-light, $key);
    }
}

// Get mapped custom color
@function custom-color($key, $custom-colors-light, $custom-colors-dark) {
    @if ($dark-theme and map-has-key($custom-colors-dark, $key)) {
        @return map-get($custom-colors-dark, $key);
    } @else {
        @return map-get($custom-colors-light, $key);
    }
}

// Get customer variable
@function customer($map, $key, $type: 'raw') {
    $variable: map-deep-get($variables-customer, $map, $key);
    @if ($type == 'raw') {
        @return $variable;
    }
    @return formatCustomerData($variable, $type);
}

// Build customer URL
@function customer-image-url($imageProperty, $organisationCode, $subDomain: null) {
    $imageName: get-image-name($imageProperty);

    // Build the URL
    $url: '/Themes/' + $organisationCode;

    // If a subdomain is present, insert it into the image URL
    @if ($subDomain) {
        $url: $url + '/' + $subDomain;
    }

    $url: $url + '/Files/' + $imageName;

    // Append the tick count as a version
    @if (customer(ThemeProperties, TickCount)) {
        $url: $url + '?v=' + customer(ThemeProperties, TickCount, 'string');
    }
    @return $url;
}

// Get the image name and set the extension
@function get-image-name($imageProperty) {

    $extension: '.jpg' !default;

    // Set the default extensions
    // This same logic is implied in the RefreshPrimaryTheme component
    @if ($imageProperty == OrganisationStyleBackgroundImage) {
        $extension: '.jpg';
    }
    @if ($imageProperty == OrganisationStyleLoaderImage) {
        $extension: '.gif';
    }
    @if ($imageProperty == OrganisationStyleLoginBackgroundImage) {
        $extension: '.jpg';
    }
    @if ($imageProperty == OrganisationStyleLogo) {
        $extension: '.png';
    }
    @if ($imageProperty == OrganisationStyleLogoDark) {
        $extension: '.png';
    }
    @if ($imageProperty == OrganisationStyleLoginLogo) {
        $extension: '.png';
    }
    @if ($imageProperty == OrganisationStyleSidebarBackgroundImage) {
        $extension: '.jpg';
    }

    // Overwrite the extenstion if one is found in the customer properties
    $themePropertyExtension: $imageProperty + Extension;
    @if (customer(ThemeProperties, $themePropertyExtension)) {
        $extension: customer(ThemeProperties, $themePropertyExtension, 'string')
    }

    // Build the full image name
    $imageName: quote($imageProperty) + $extension;

    @return $imageName;
}

@function map-deep-get($map, $keys...) {
    $scope: $map; $i: 1;
    @while (type-of($scope) == map) and ($i <= length($keys)) {
       $scope: map-get($scope, nth($keys, $i));
       $i: $i + 1;
    }
    @return $scope;
 }

 @function formatCustomerData($value, $type) {

    // Color fields
    @if ($type == 'color') {
        $value: color-from-dec(number($value));
    }

    @if ($type == 'hex') {
        //$value: unquote('#' + dec-to-hex(number($value)));
        $value: color-from-dec(number($value));
    }

    @if ($type == 'rgb') {
        $value: hex-to-rgb('#' + dec-to-hex(number($value)));
    }

    // Unquote
    @if ($type == 'string') {
        $value: unquote($value);
    }

    // Integer
    @if ($type == 'integer') {
        $value: number($value);
    }

    // px fields
    @if ($type == 'px') {
        $value: number($value) + 0px;
    }

    // Percentage
    @if ($type == '%') {
        $value: number($value) * 1%;
    }

    // Boolean fields
    @if ($type == 'boolean') {
        @if ($value == 'true' or $value == '1') {
            $value: true;
        } @else {
            $value: false;
        }
    }

    @return $value;
 }

// Get the HEX code of a decimal 
@function dec-to-hex($d) {
    $hexVals: "A" "B" "C" "D" "E" "F";
    $base: 16;
    $quotient: $d;
    $result: "";
    $zeros: "000000";
    @if $d == 0 {
        $result: "000000";
    }
    @while $quotient != 0 {
        $mod: $quotient % $base;
        $quotient: floor($quotient / $base);
        @if $mod > 9 {
            $mod: nth($hexVals, $mod - 9);
        }
        @if $d < $base {
            $result: "0" + $mod;
        } @else {
            $result: $mod + $result;
        }
    }
    $length: 6 - str-length($result);
    $prefix: str-slice($zeros, 1, $length);
    @if (str-length($result) != 6) {
        $result: $prefix + $result;
    }
    @return $result;
}

// Get the RGB code of a decimal 
@function color-from-dec($decimal) {
    $r: floor($decimal / 65536);
    $g: floor(($decimal % 65536) / 256);
    $b: $decimal % 256;
    @return rgb($r, $g, $b);
}

@function number($string) {
    // Matrices
    $strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
    $numbers: 0 1 2 3 4 5 6 7 8 9;
  
    // Result
    $result: 0;
  
    // Looping through all characters
    @for $i from 1 through str-length($string) {
        $character: str-slice($string, $i, $i);
        $index: index($strings, $character);
      
        @if not $index {
          @warn "Unknown character `#{$character}`.";
          @return false;
        }
      
        $number: nth($numbers, $index);
        $result: $result * 10 + $number;
      }
  
    @return $result;
  }

  @function hex-to-rgb($string) {
    $string-lower: to-lower-case($string);
    $r: "";  $g: ""; $b: "";
    $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
    $length: str-length($string);
    $max: if($length == 4, 1, 2);
  
    // Check for length accuracy
    @if $length != 4 and $length != 7 {
      @return $string;
    }
  
    // Loop from the second character (omitting #)
    @for $i from 2 through $length {
      $c: str-slice($string-lower, $i, $i);
  
      // If wrong character, return
      @if index($hex, $c) == null {
        @return $string;
      }
  
      @if str-length($r) < $max {
        $r: $r + $c;
      } @else if str-length($g) < $max {
        $g: $g + $c;
      } @else if str-length($b) < $max {
        $b: $b + $c;
      }
    }
  
    @if $length == 4 {
      $r: $r + $r;
      $g: $g + $g;
      $b: $b + $b;
    }
  
    @return _hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b);
  }

  @function _hex-to-dec($string) {
    $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
    $string: to-lower-case($string);
    $length: str-length($string);
  
    $dec: 0;
    @for $i from 1 through $length {
      $factor: 1 + (15 * ($length - $i));
      $index: index($hex, str-slice($string, $i, $i));
      $dec: $dec + $factor * ($index - 1);
    }
  
    @return $dec;
  }

// This function takes a SCSS color (e.g., #4c4c4c), converts it to a string,
// removes the leading '#' character, and then prefixes '%23' so it can be used 
// in inline SVG data URLs. This ensures the color is properly URL-encoded 
// for the 'fill' attribute within an SVG.
@function encode-color($color) {
    $color-string: inspect($color);
    $without-hash: str-slice($color-string, 2);
    @return "%23#{$without-hash}";
}

@function generate-material-tone($color, $shade) {
    $hue: hue($color);
    $saturation: saturation($color);

    $lightness: $shade * 1%;

    // Adjust lightness using a non-linear curve to maintain contrast
    $new-lightness: if($lightness > 50%, 100% - (100% - $lightness) * 0.8, $lightness * 0.9);

    @return hsl($hue, $saturation, $new-lightness);
}

@function get-primary-color-tone($tone) {
    @return map-get($color-primary-tones, $tone);
}

// Manual reverse list fallback (for older Sass versions)
@function reverse-list($list) {
	$reversed: ();
	@for $i from length($list) through 1 {
	  $reversed: append($reversed, nth($list, $i));
	}
	@return $reversed;
}

// Returns a valid SVG `fill` color value for inline data URIs.
// - If the color is a hex value (e.g. `#ff6600`), it converts it to a URL-encoded format (`%23ff6600`)
//   because `#` must be escaped in data URLs.
// - If the color is an RGB or RGBA string (e.g. `rgb(255, 0, 0)`), it returns the value unchanged.
// Useful when dynamically injecting colors into inline SVGs inside background-image URLs.
@function svg-fill-color($color) {
	$color-str: inspect($color);
	$fill: if(
	str-index($color-str, 'rgb') == null,
	'%23' + str-slice($color-str, 2),
	$color-str
	);

	@return $fill;
}