////
/// @group path
////

@use "sass:map";
@use "sass:string";
@use "utils";

/// Aliases 
/// @type Map
/// - Important: alias could be used for directory or file so omit trailing slash for directories
/// - All aliases must start with "#"

$aliases: (
  "#Images" : "/images",
) !default;

/// Set aliases or change defaults
/// @param {Map} $changes Map of changes

@mixin set-aliases($changes) {
  $aliases: map.merge($aliases, $changes) !global;
}

/// Get an aliases path
/// @param {String} Name Name of alias

@function get-alias($name) {
  @return utils.require-map-get($aliases, $name, '[path alias]');
}

/// Resolves a Path 
/// - Checks if path is an alias if so rewrite's it
/// - Future could include rewrites for easier updating or rearranging of legacy things
/// - Aliases work by starting with "#", Since it's an illegal uri character, needs to be escaped if used as literal, we felt that it was the safest way to mix aliases in with real URLs (so that a dev of a module using path.resolve can always pass paths through (user input unknown if alias)
/// @example scss
///   .img {
///     background-image: url(ulu.path-resolve("#Images/branding/logo"));
///   }
///

@function resolve($path) {
  $is-alias: string.index($path, "#") == 1;
  $dir-index: string.index($path, "/");
  $resolved-path: $path;
  @if $is-alias {
    // Extract alias
    @if $dir-index {
      $alias: string.slice($path, 1, $dir-index - 1);
      @return "#{ get-alias($alias) }#{ string.slice($path, $dir-index) }";
    // Must just be only an alias
    } @else {
      @return "#{ get-alias($path) }";
    }
  } @else {
    @return $path;
  }
}