{
  "version": 3,
  "sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store.ts", "../src/store/store-with-backup.ts"],
  "sourcesContent": ["import { packageTracer } from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import { createLogger } from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @typeParam T - The type of the default value.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n  /**\n   * The name of the store.\n   */\n  name: string;\n\n  /**\n   * The default value of the store.\n   */\n  defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @typeParam T - The type of the data.\n *\n * @param config - The configuration object for the store.\n * @returns - The initialized store instance.\n *\n * @remarks\n *\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see {@link https://alpinejs.dev/globals/alpine-store}\n *\n * @example\n * ```ts\n * const store = alpineStoreGenerator({\n *   name: 'user',\n *   defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *```\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n  logger.logMethodArgs?.('alpineStoreGenerator', { config });\n\n  alpine.store(config.name, config.defaultValue);\n\n  // Get store Proxy\n  const store = alpine.store(config.name) as T;\n  return store;\n}\n", "import { type AlwatrLogger, createLogger } from '@alwatr/logger';\n\nimport { alpineStoreGenerator } from './store-generator.js';\n\n/**\n * AlpineStore Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreOptions<T extends DictionaryReq> = {\n  /**\n   * The name of the store.\n   */\n  name: string;\n\n  /**\n   * The default value of the store.\n   */\n  defaultValue: T;\n};\n\n/**\n * Provides a Alpine.js pure store implementation with logger.\n */\nexport class AlpineStore<T extends DictionaryReq> {\n  /**\n   * The store's data.\n   */\n  store: T;\n\n  protected logger_: AlwatrLogger;\n\n  /**\n   * Provides a Alpine.js pure store implementation with logger.\n   *\n   * @param config - Configuration object.\n   *\n   * @example\n   * ```ts\n   * import {AlpineStore} from '@nexim/alpine';\n   *\n   * const {store} = new AlpineStore({\n   *   name: 'myStore',\n   *   defaultValue: {data: 'root'},\n   * });\n   * console.log(store.data); // Output: { data: 'root' }\n   *\n   * store.data = 'user';\n   * console.log(store.data); // Output: { data: 'user' }\n   * ```\n   */\n  constructor(config: AlpineStoreOptions<T>) {\n    this.logger_ = createLogger(`${__package_name__}:${config.name}`);\n    this.logger_.logMethodArgs?.('constructor', config);\n\n    this.store = alpineStoreGenerator(config);\n  }\n}\n", "import { localJsonStorage } from '@alwatr/local-storage';\nimport { type Duration, parseDuration } from '@alwatr/parse-duration';\n\nimport { AlpineStore } from './store.js';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n  data: DictionaryReq | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n  /**\n   * The name of the store.\n   */\n  name: string;\n\n  /**\n   * The version of the store.\n   */\n  version: number;\n\n  /**\n   * The default value of the store.\n   */\n  defaultValue: T;\n\n  /**\n   * Optional. The duration after which the store expires.\n   */\n  expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {\n  /**\n   * Keys for storing data and expire time in local storage with version.\n   */\n  private localStorageKey__ = {\n    data: `[${__package_name__}:data:sv${schemaVersion.toString()}]:${this.config__.name}`,\n    expireTime: `[${__package_name__}:expire-time:sv${schemaVersion.toString()}]:${this.config__.name}`,\n  };\n\n  /**\n   * Provides a Alpine.js store implementation with backup and expiration.\n   *\n   * @param config__ - Configuration object.\n   *\n   * @example\n   * ```ts\n   * import {AlpineStoreWithBackup} from '@nexim/alpine';\n   *\n   * const storeWithBackup = new AlpineStoreWithBackup({\n   *   name: 'myStoreWithBackup',\n   *   version: 1,\n   *   defaultValue: {data: 'root'},\n   *   expireDuration: '1d',\n   * });\n   *\n   * storeWithBackup.store.data = 'user';\n   *\n   * storeWithBackup.save();\n   * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n   *\n   * storeWithBackup.clear();\n   * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n   * ```\n   */\n  constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n    super(config__);\n\n    if (this.config__.expireDuration != null) {\n      this.handleDataExpiration__();\n    }\n\n    this.load__();\n  }\n\n  /**\n   * Saves the current data to local storage. If the data is null, it clears the stored data.\n   *\n   * Also updates the expiration time.\n   */\n  save(): void {\n    this.logger_.logMethodArgs?.('save', { data: this.store.data });\n\n    if (this.store.data === null) {\n      this.clear();\n      return;\n    }\n\n    localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n    this.updateExpireTime__();\n  }\n\n  /**\n   * Clears the stored data.\n   */\n  clear(): void {\n    this.logger_.logMethod?.('clear');\n\n    localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n    localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n    this.store = this.config__.defaultValue;\n  }\n\n  /**\n   * Handles the expiration duration by checking if the stored data has expired.\n   * If expired, it clears the stored data.\n   */\n  private handleDataExpiration__(): void {\n    this.logger_.logMethod?.('handleDataExpiration__');\n\n    // FIXME: use null if not set, after local storage new version.\n    const expireDuration = localJsonStorage.getItem<{ time: number }>(\n      this.localStorageKey__.expireTime,\n      { time: -1 },\n      this.config__.version,\n    ).time;\n\n    if (expireDuration !== -1 && expireDuration < Date.now()) {\n      this.clear();\n    }\n  }\n\n  /**\n   * Loads data from local storage and updates the store's data.\n   *\n   * When data is not found or invalid in local storage, it uses the default value.\n   *\n   * FIXME: remove `NonNullable` from `<T['data']>`, after local storage new version.\n   */\n  private load__(): void {\n    this.logger_.logMethod?.('load__');\n\n    const newData = localJsonStorage.getItem<NonNullable<T['data']>>(\n      this.localStorageKey__.data,\n      this.config__.defaultValue.data as NonNullable<T['data']>,\n      this.config__.version,\n    ) as T['data'];\n    this.store.data = newData;\n  }\n\n  /**\n   * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n   */\n  private updateExpireTime__(): void {\n    if (this.config__.expireDuration == null) return;\n    this.logger_.logMethod?.('updateExpireTime__');\n\n    const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n    localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version);\n\n    this.logger_.logOther?.('updated_expire_time', { newExpireTime });\n  }\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA8B;;;ACA9B,oBAA6B;AAC7B,sBAAmB;AAEnB,IAAM,aAAS,4BAAa,eAAgB;AA8CrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,EAAE,OAAO,CAAC;AAEzD,kBAAAA,QAAO,MAAM,OAAO,MAAM,OAAO,YAAY;AAG7C,QAAM,QAAQ,gBAAAA,QAAO,MAAM,OAAO,IAAI;AACtC,SAAO;AACT;;;ACzDA,IAAAC,iBAAgD;AAwBzC,IAAM,cAAN,MAA2C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2BhD,YAAY,QAA+B;AACzC,SAAK,cAAU,6BAAa,GAAG,eAAgB,IAAI,OAAO,IAAI,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