// Stores information about the assets that are available
// The structure of the map is that the keys indicate the module
// to which the assets belong. When the module is null, the assets
// belong to the application itself. Application assets take priority
// over module assets, that is, "foo/bar.png" in the application will
// take priority over "bar.png" in the "foo" module.
//
// The values of this map is another map of source urls (as is passed to
// asset-url()) to a map with two keys: filepath and uri. The filepath is the
// path on disk where the asset resides and the uri is the asset's preferred
// output URL which is namespaced to avoid conflicts with application urls.
$eg-registered-assets: () !default;

@function eyeglass-normalize-uri($uri, $type: web) {
  @if function-exists(-eyeglass-normalize-uri) {
    @return -eyeglass-normalize-uri($uri, $type);
  }

  @return $uri;
}
@function eyeglass-normalize-assets($assets) {
  @if function-exists(-eyeglass-normalize-assets) {
    @return -eyeglass-normalize-assets($assets);
  }

  @return $assets;
}
// Registers many assets for a module at once.
// @param $module string The name of the module to which these assets belong.
// @param $url the source url of the asset. This is what is passed to asset-url().
// @param $path the fully qualified filesystem path of the asset.
// @param $uri [Optional] the preferred output url of the asset. Defaults to the url
//   if not provided.
@function asset-register($module, $url, $path, $uri: null) {
  $mod-assets: map-get($eg-registered-assets, $module) or ();
  // normalize the various paths
  // ensure that the URI and URL are both web paths
  $url: eyeglass-normalize-uri($url, web);
  $uri: eyeglass-normalize-uri($uri or $url, web);
  // while the file path is a system path
  $path: eyeglass-normalize-uri($path, system);

  // then merge these back into the global registry
  $eg-registered-assets: map-merge($eg-registered-assets, (
    $module: map-merge($mod-assets, (
      $url: (
        filepath: quote($path),
        uri: quote($uri)
      )
    ))
  )) !global;
  @return null;
}

@mixin asset-register($module, $url, $path, $uri: null) {
  $discarded-value: asset-register($module, $url, $path, $uri);
}

// Registers many assets for a module at once.
// @param $module string The name of the module to which these assets belong.
// @param $assets map<string => map> maps source urls to the asset information
//   as described in the docs for `$eg-registered-assets` above.
@function asset-register-all($module, $assets) {
  $assets: eyeglass-normalize-assets($assets);
  $mod-assets: map-get($eg-registered-assets, $module);
  @if ($mod-assets) {
    $mod-assets: map-merge($mod-assets, $assets);
  }
  @else {
    $mod-assets: $assets;
  }
  $eg-registered-assets: map-merge($eg-registered-assets, ($module: $mod-assets)) !global;
  @return null;
}

@mixin asset-register-all($module, $assets) {
  $discarded-value: asset-register-all($module, $assets);
}

// Returns a URL to the asset wrapped.
@function asset-uri($relative-path) {
  $relative-path: eyeglass-uri-preserve($relative-path);
  @return quote(
    eyeglass-uri-restore(
      eyeglass-asset-uri($eg-registered-assets, unquote($relative-path))
    )
  );
}

// Returns a URL to the asset wrapped in the css url() function.
@function asset-url($relative-path) {
  @return url(asset-uri($relative-path));
}

// The full path to the file on disk specified by the relative path.
//@function asset-file($relative-path) {
//  // TODO - this isn't defined anywhere!!!
//  @return eyeglass-asset-file($eg-registered-assets, $relative-path);
//}

// Lists all the modules which have registered assets.
@function asset-modules() {
  @return map-keys($eg-registered-assets);
}

// Lists all the assets for a given module.
@function asset-list($module: null) {
  $asset-urls: ();
  $mod-assets: map-get($eg-registered-assets, $module) or ();
  @each $url, $path in $mod-assets {
    $url: if($module, $module + "/" + $url, $url);
    $asset-urls: append($asset-urls, quote($url), comma);
  }
  @return if(length($asset-urls) == 0, null, $asset-urls);
}
