{"version":3,"file":"odx-angular-theming.mjs","sources":["../../../../libs/angular/theming/src/lib/helpers/prefers-dark-theme.ts","../../../../libs/angular/theming/src/lib/models/theme.ts","../../../../libs/angular/theming/src/lib/helpers/resolve-theme.ts","../../../../libs/angular/theming/src/lib/helpers/theme-storage.ts","../../../../libs/angular/theming/src/lib/theming.service.ts","../../../../libs/angular/theming/src/lib/theming.config.ts","../../../../libs/angular/theming/src/odx-angular-theming.ts"],"sourcesContent":["/**\n * Determines whether the user's system preferences indicate a preference for dark themes.\n * This is achieved by querying the '(prefers-color-scheme: dark)' media feature, which reflects\n * the user's preference for a light or dark color scheme in their operating system's UI.\n *\n * @param {Window} view - The window object, typically `window`, from which to access the matchMedia method.\n * @returns {boolean} `true` if the user prefers a dark theme, otherwise `false`.\n *\n * @example\n * Example usage in a web application to toggle themes:\n * ```ts\n * if (prefersDarkTheme(window)) {\n *   document.body.classList.add('dark-theme');\n * } else {\n *   document.body.classList.remove('dark-theme');\n * }\n * ```\n */\nexport function prefersDarkTheme(view: Window): boolean {\n  return !!view.matchMedia?.('(prefers-color-scheme: dark)')?.matches;\n}\n","export type Theme = (typeof Theme)[keyof typeof Theme];\nexport const Theme = {\n  DARK: 'dark',\n  LIGHT: 'light',\n} as const;\n\nexport const availableThemes = Object.values(Theme);\n","import { availableThemes, Theme } from '../models';\n\n/**\n * Resolves the specified theme by checking if it exists in the predefined list of available themes.\n * This function ensures that only valid themes are used in the application, providing a safeguard\n * against invalid theme names and facilitating the dynamic application of themes.\n *\n * @param {string | null} [theme] - The name of the theme to resolve. It's optional and can be `null`.\n * @returns {Theme | null} - Returns the resolved `Theme` object if the theme is found within the list of `availableThemes`;\n * otherwise, returns `null` to indicate the theme is not recognized or available.\n *\n * @example\n * ```ts\n * // Assuming 'dark' and 'light' are in the availableThemes array\n * console.log(resolveTheme('dark')); // Outputs the 'dark' Theme object\n * console.log(resolveTheme('unknown')); // Outputs null\n *\n * // Using resolveTheme to apply a user-selected theme\n * const userSelectedTheme = getUserSelectedTheme(); // This function gets the user's theme choice\n * const themeToApply = resolveTheme(userSelectedTheme);\n * if (themeToApply) {\n *   applyTheme(themeToApply); // This function applies the resolved theme to the application\n * } else {\n *   console.warn('Selected theme is not available.');\n * }\n * ```\n */\nexport function resolveTheme(theme?: string | null): Theme | null {\n  return availableThemes.find((t) => t === theme) ?? null;\n}\n","import { Theme } from '../models';\nimport { resolveTheme } from './resolve-theme';\n\n/**\n * The key used to store the selected theme in `Storage`.\n * This constant ensures consistent access to the theme setting across the application.\n */\nexport const THEME_STORAGE_NAME = '@odx/angular/theming:SelectedTheme';\n\n/**\n * Saves the specified theme to storage. This function abstracts the storage interaction,\n * providing a fail-safe mechanism to avoid exceptions if the storage is not available.\n *\n * @param {Storage | null | undefined} storage - The storage mechanism to use, typically `localStorage` or `sessionStorage`.\n * @param {Theme} theme - The theme to save. Should be a value that is recognized by the application's theming system.\n * @throws Will silently catch and ignore any exceptions caused by storage limitations or permissions.\n *\n * @example\n * ```ts\n * // Example usage with localStorage\n * saveTheme(localStorage, 'dark');\n *\n * // Using sessionStorage instead\n * saveTheme(sessionStorage, 'light');\n * ```\n */\nexport function saveTheme(storage: Storage | null | undefined, theme: Theme): void {\n  try {\n    storage?.setItem(THEME_STORAGE_NAME, theme);\n  } catch {\n    //\n  }\n}\n\n/**\n * Reads the theme from storage, attempting to resolve it to a valid application theme.\n * If the theme cannot be found or is invalid, `null` is returned. This function also provides\n * error handling for any storage access issues, ensuring the application can gracefully handle\n * scenarios where storage is not available or accessible.\n *\n * @param {Storage | null} [storage] - The storage mechanism to use, typically `localStorage` or `sessionStorage`.\n * @returns {Theme | null} - The resolved theme if found and valid; otherwise, `null`.\n * @throws Will silently catch and ignore any exceptions caused by storage access, returning `null`.\n *\n * @example\n * ```ts\n * // Reading theme from localStorage\n * const theme = readTheme(localStorage);\n * if (theme) {\n *   applyTheme(theme); // Assuming applyTheme is a function to apply the theme\n * } else {\n *   console.warn('No saved theme found or theme is invalid.');\n * }\n * ```\n */\nexport function readTheme(storage?: Storage | null): Theme | null {\n  try {\n    return resolveTheme(storage?.getItem(THEME_STORAGE_NAME)) ?? null;\n  } catch {\n    return null;\n  }\n}\n","\nimport { inject, Injectable, DOCUMENT } from '@angular/core';\nimport { WindowRef } from '@odx/angular';\nimport { BehaviorSubject, distinctUntilChanged, shareReplay } from 'rxjs';\nimport { prefersDarkTheme, readTheme, saveTheme } from './helpers';\nimport { Theme } from './models';\nimport { injectThemingConfig } from './theming.config';\n\nexport const THEME_ATTRIBUTE_NAME = 'odxTheme';\n\n/**\n * Manages theming for the application. It allows for dynamic theme selection, automatically saves theme preferences,\n * and applies the user's preferred theme or the default theme based on configuration settings.\n *\n * The service leverages the BehaviorSubject pattern to reactively handle theme changes across the application,\n * ensuring that subscribers are notified whenever the theme changes. It also interacts with the DOM directly\n * to apply the current theme and with the provided storage mechanism to persist theme preferences.\n */\n@Injectable({ providedIn: 'root' })\nexport class ThemingService {\n  private readonly config = injectThemingConfig();\n  private readonly document = inject(DOCUMENT);\n  private readonly windowRef = inject(WindowRef);\n  private readonly theme$$ = new BehaviorSubject<Theme>(this.getInitialTheme());\n\n  /**\n   * An observable stream of the current theme, emitting values whenever the theme changes.\n   * Subscribers to this observable can react to theme changes in real-time.\n   *\n   * @emits {Theme} The currently selected theme.\n   */\n  public readonly theme$ = this.theme$$.pipe(distinctUntilChanged(), shareReplay({ bufferSize: 1, refCount: true }));\n\n  constructor() {\n    this.theme$.subscribe((theme) => {\n      this.document.body.setAttribute(THEME_ATTRIBUTE_NAME, theme);\n      saveTheme(this.config.storage, theme);\n    });\n  }\n\n  /**\n   * Selects and applies a given theme by updating the BehaviorSubject and persisting the preference.\n   *\n   * @param {Theme} theme - The theme to apply.\n   */\n  public selectTheme(theme: Theme): void {\n    this.theme$$.next(theme);\n  }\n\n  /**\n   * Retrieves the currently selected theme.\n   *\n   * @returns {Theme} The currently selected theme.\n   */\n  public getSelectedTheme(): Theme {\n    return this.theme$$.getValue();\n  }\n\n  /**\n   * Toggles between dark and light themes, depending on the current theme.\n   */\n  public toggleDarkMode(): void {\n    const currentTheme = this.getSelectedTheme();\n    if (currentTheme === Theme.DARK) {\n      this.enableLightMode();\n    } else {\n      this.enableDarkMode();\n    }\n  }\n\n  /**\n   * Enables the dark mode theme.\n   */\n  public enableDarkMode(): void {\n    this.selectTheme(Theme.DARK);\n  }\n\n  /**\n   * Enables the light mode theme.\n   */\n  public enableLightMode(): void {\n    this.selectTheme(Theme.LIGHT);\n  }\n\n  private getInitialTheme(): Theme {\n    let selectedTheme = readTheme(this.config.storage);\n\n    if (!selectedTheme && this.config.autoDetect && prefersDarkTheme(this.windowRef.nativeWindow)) {\n      selectedTheme = Theme.DARK;\n    }\n\n    return selectedTheme ?? this.config.defaultTheme;\n  }\n}\n","import { ENVIRONMENT_INITIALIZER, EnvironmentProviders, inject, makeEnvironmentProviders } from '@angular/core';\nimport { ConfigDependencies, ConfigProvider, createConfigTokens } from '@odx/angular/utils';\nimport { Theme } from './models';\nimport { ThemingService } from './theming.service';\n\n/**\n * Defines the structure for theming configuration options in the application.\n */\nexport interface ThemingConfig {\n  /**\n   * Determines whether the theme should be auto-detected based on system preferences.\n   */\n  autoDetect: boolean;\n\n  /**\n   * Specifies the default theme to be used if auto-detection is not enabled or fails.\n   */\n  defaultTheme: Theme;\n\n  /**\n   * The storage mechanism (like `localStorage` or `sessionStorage`) to persist theme settings,\n   * or `null` to disable persistence.\n   */\n  storage?: Storage | null;\n}\n\n/**\n * Utility created by `createConfigTokens` for managing theming configuration tokens and functions.\n * Provides easy access to theming configuration, default values, and dependency injection utilities.\n */\nexport const { ThemingConfig, injectThemingConfig, provideThemingConfig } = createConfigTokens('Theming', '@odx/angular/theme', {\n  autoDetect: false,\n  defaultTheme: Theme.LIGHT,\n  storage: null,\n} as ThemingConfig);\n\n/**\n * Provides the theming configuration to the application and initializes the `ThemingService`\n * based on the provided configuration. This function simplifies the setup of a dynamic theming\n * system, integrating with Angular's environment initializers to ensure proper initialization.\n *\n * @template D - The dependencies required by the theming configuration, if any.\n * @param {ConfigProvider<Partial<ThemingConfig>, D>} config - The theming configuration or a configuration factory provider.\n * @returns {EnvironmentProviders} An array of providers for setting up theming in the application.\n *\n * @example\n * ```ts\n * // Providing a simple theming configuration\n * provideTheming({\n *   autoDetect: true,\n *   defaultTheme: Theme.DARK,\n *   storage: localStorage,\n * });\n *\n * // Providing a theming configuration with dependencies\n * provideTheming({\n *   useFactory: (deps: Dependencies) => ({\n *     autoDetect: deps.setting,\n *     defaultTheme: Theme.DARK,\n *     storage: localStorage,\n *   }),\n *   deps: [SomeDependency],\n * });\n * ```\n */\nexport function provideTheming<D extends ConfigDependencies = ConfigDependencies<ThemingConfig>>(\n  config: ConfigProvider<Partial<ThemingConfig>, D>,\n): EnvironmentProviders {\n  return makeEnvironmentProviders([\n    provideThemingConfig(config),\n    {\n      provide: ENVIRONMENT_INITIALIZER,\n      useFactory: () => () => inject(ThemingService),\n      deps: [ThemingConfig],\n      multi: true,\n    },\n  ]);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;;;AAiBG;AACG,SAAU,gBAAgB,CAAC,IAAY,EAAA;IAC3C,OAAO,CAAC,CAAC,IAAI,CAAC,UAAU,GAAG,8BAA8B,CAAC,EAAE,OAAO;AACrE;;ACnBO,MAAM,KAAK,GAAG;AACnB,IAAA,IAAI,EAAE,MAAM;AACZ,IAAA,KAAK,EAAE,OAAO;;AAGT,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK;;ACJlD;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;AACG,SAAU,YAAY,CAAC,KAAqB,EAAA;AAChD,IAAA,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,IAAI,IAAI;AACzD;;AC1BA;;;AAGG;AACI,MAAM,kBAAkB,GAAG;AAElC;;;;;;;;;;;;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