resolve method

ResolvedBundleUri resolve(
  1. String bundleUri
)

FR-BUNDLE-003

Implementation

ResolvedBundleUri resolve(String bundleUri) {
  final Uri uri;
  try {
    uri = Uri.parse(bundleUri);
  } catch (e) {
    throw BundleUriResolutionException(bundleUri, 'Malformed URI', cause: e);
  }

  if (uri.scheme != _scheme) {
    throw BundleUriResolutionException(
      bundleUri,
      'Not a bundle:// URI (scheme: ${uri.scheme})',
    );
  }

  final category = uri.host;
  final path = uri.pathSegments.join('/');
  final relPath = path.isEmpty ? category : '$category/$path';

  // Inline asset lookup via assets section.
  final assets = _assets;
  if (assets != null) {
    final asset = assets.getAsset(relPath) ?? assets.getAsset(path);
    if (asset != null) {
      if (asset.content != null) {
        final mime = asset.mimeType ?? _inferMediaType(asset.path);
        final encoding = asset.encoding;
        final dataUri = encoding == 'base64'
            ? Uri.parse('data:$mime;base64,${asset.content}')
            : Uri.dataFromString(
                asset.content!,
                mimeType: mime,
              );
        return ResolvedBundleUri(dataUri, mime);
      }
      if (asset.contentRef != null) {
        final refUri = Uri.parse(asset.contentRef!);
        if (refUri.hasScheme) {
          return ResolvedBundleUri(
            refUri,
            asset.mimeType ?? _inferMediaType(asset.path),
          );
        }
        if (_bundleRootPath != null) {
          return ResolvedBundleUri(
            Uri.file('$_bundleRootPath/${asset.contentRef}'),
            asset.mimeType ?? _inferMediaType(asset.path),
          );
        }
      }
    }
  }

  // Fallback: infer from bundle root path.
  if (_bundleRootPath != null) {
    return ResolvedBundleUri(
      Uri.file('$_bundleRootPath/$relPath'),
      _inferMediaType(relPath),
    );
  }

  throw BundleUriResolutionException(bundleUri, 'No mapping found');
}