@sixbell-telco/sdk
Version:
A collection of reusable components designed for use in Sixbell Telco Angular projects
219 lines (218 loc) • 8.89 kB
TypeScript
import { ResolvedThemeScheme, RuntimeThemeConfig, ThemeAsset, ThemeAssets } from '../models/theme';
import * as i0 from "@angular/core";
export declare class ThemeAssetsService {
/**
* Signal holding current runtime configuration reference
* Used to look up asset definitions for the active theme
*/
private config;
private themeConfigs;
/**
* Layer 1: In-memory cache of resolved assets
* Maps "themeName:assetKey" to CachedAsset for fast retrieval
* Cleared when setConfig() is called to force re-resolution from new config
*/
private assetCacheByTheme;
/**
* Layer 2: Last working config storage (memory-based)
* Stores the most recent RuntimeThemeConfig that successfully resolved assets
* Persists across config updates as a complete fallback mechanism
* Similar to ThemeService's multi-layer config persistence
*
* When current config fails to provide an asset:
* - Falls back to the last complete working config
* - Handles asset removal, name changes, path changes
* - Automatically restores working state on config resets
* - No individual URL tracking - entire config stored once per successful load
*/
private lastWorkingConfigLayer;
private logger;
constructor();
/**
* Initialize service with runtime configuration
* Called by ThemeService after loading configuration
*
* If new config successfully resolves assets, it becomes the new fallback layer.
* Otherwise, previous working config persists.
*
* Only updates Layer 2 if:
* - Config has no PARTIAL/BROKEN assets (missing variants)
* - Themes can be removed entirely (that's intentional)
* - But existing assets must be COMPLETE (both light and dark)
*
* This ensures that accidental removals of variants trigger fallback
* while intentional complete removals are allowed.
*
* @param config - Runtime configuration containing asset definitions
*/
setConfig(config: RuntimeThemeConfig): void;
setThemeConfig(themeName: string, config: RuntimeThemeConfig): void;
/**
* Internal: Check if config has any PARTIAL/BROKEN assets
* Returns true if ANY asset is incomplete (missing light or dark variant)
*
* A partial asset is invalid:
* - { name: "logo", light: "..." } ← missing dark
* - { name: "logo", dark: "..." } ← missing light
* - { name: "logo" } ← missing both
*
* Complete removals are OK:
* - [] ← no assets at all (intentional)
* - theme: {} ← theme removed (intentional)
*
* @private
* @param config - Config to check
* @returns True if config has partial/broken assets, false if all complete or intentionally removed
*/
private hasPartialAssets;
/**
* Internal: Check if a theme asset list has ANY problematic assets
* Problematic assets are:
* - Partial assets: missing light or dark variant
* - Empty assets: no variants at all
*
* NOT problematic:
* - Completely empty list: [] (all assets intentionally removed - OK to accept)
*
* @private
* @param obj - Theme's asset object to check
* @returns True if object has problematic assets
*/
private themeHasProblematicAssets;
/**
* Internal: Check if object is a PARTIAL asset (incomplete)
* Partial means it LOOKS LIKE an asset (has some scheme keys) but is missing variants
*
* @private
* @param obj - Object to check
* @returns True if object is a partial/incomplete asset
*/
private isPartialAsset;
/**
* Get asset URL for the specified scheme
* Retrieves a specific asset variant (light or dark) with config-based fallback
*
* Resolution Strategy (in order):
* 1. Try to get the requested scheme variant from current config
* 2. If not available, use last working config from memory layer
* 3. If no last working config, return null
*
* This ensures:
* - Graceful degradation when assets are removed from config
* - Support for asset name changes, path changes, config resets
* - Automatic restoration when config is fixed
* - No automatic scheme switching (stays on requested scheme)
* - Similar to ThemeService's config persistence pattern
*
* @param themeName - Name of the theme (e.g., 'sixbell_telco', 'wom')
* @param assetKey - Asset identifier path (e.g., 'logo', 'banners.hero', 'illustrations.empty')
* @param scheme - Color scheme ('light' or 'dark')
* @returns Asset URL for the specified scheme, or from last working config, or null if not found
*
* @example
* // Current config has dark - returns requested variant
* getAssetUrl('wom', 'logo', 'dark')
* // Returns: '/assets/logos/logo_wom_dark.svg'
*
* // Current config missing dark - returns from last working config
* getAssetUrl('wom', 'logo', 'dark')
* // Returns: '/assets/logos/logo_wom_dark.svg' (from last working config layer)
*
* // Never existed - no working config
* getAssetUrl('wom', 'nonexistent', 'dark')
* // Returns: null
*/
getAssetUrl(themeName: string, assetKey: string, scheme: ResolvedThemeScheme): string | null;
/**
* Internal: Resolve asset URL from a specific config
* Helper method to resolve URL from current or last working config
*
* Returns null if:
* - Asset not found
* - Asset is incomplete (missing either light or dark variant)
* - Requested scheme variant not available
*
* @private
* @param themeName - Theme name
* @param assetKey - Asset key path
* @param scheme - Scheme variant
* @param configToUse - Config to resolve from (current or lastWorking)
* @returns URL if found and complete, null otherwise
*/
private resolveAssetUrl;
/**
* Get both light and dark variants of an asset
* Returns an object with both scheme variants for easy access
*
* @param themeName - Name of the theme
* @param assetKey - Asset identifier path
* @returns Object with light and dark URLs, or null if asset not found
*
* @example
* getAsset('wom', 'logo')
* // Returns: { light: '/assets/logos/logo_wom_light.svg', dark: '/assets/logos/logo_wom_dark.svg' }
*/
getAsset(themeName: string, assetKey: string): ThemeAsset | null;
/**
* Check if an asset exists in the specified theme
* Useful for conditional rendering or fallback handling
*
* @param themeName - Name of the theme
* @param assetKey - Asset identifier path
* @returns True if the asset exists, false otherwise
*
* @example
* hasAsset('wom', 'logo') // true
* hasAsset('wom', 'nonexistent') // false
*/
hasAsset(themeName: string, assetKey: string): boolean;
/**
* Get all assets for a specific theme
* Returns the complete asset set for further processing
*
* @param themeName - Name of the theme
* @returns All assets for the theme, or null if theme has no assets
*/
getThemeAssets(themeName: string): ThemeAssets | null;
/**
* Manually clear the asset cache
* Typically called automatically by setConfig(), but can be used for:
* - Testing and debugging
* - Force cache refresh in special scenarios
*
* Note: The last-working-config layer is NOT cleared - only the config cache
* This allows the fallback mechanism to persist across manual cache clears
*
* @example
* // In tests
* assetsService.clearCache();
*
* @example
* // Force refresh (if config doesn't change but images do)
* assetsService.clearCache();
* assetsService.setConfig(currentConfig);
*/
clearCache(themeName?: string): void;
prefetchAssets(themeName: string): Promise<void>;
/**
* Internal: Type guard to validate ThemeAsset structure
* Requires BOTH light and dark variants to be present
* Partial assets (missing either variant) are considered incomplete/broken
*
* Why both required:
* - Assets are designed to work with both light and dark themes
* - Missing variant indicates incomplete config (likely accidental removal)
* - Triggers fallback to last complete working config
* - Prevents partially broken configs from overwriting good state
*
* @private
* @param obj - Object to validate
* @returns True if object is a valid ThemeAsset (has BOTH light and dark)
*/
private isThemeAsset;
private collectAssetUrls;
private findAsset;
private cacheAsset;
static ɵfac: i0.ɵɵFactoryDeclaration<ThemeAssetsService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ThemeAssetsService>;
}