{"version":3,"file":"uni-manager-locale.mjs","sources":["../../../projects/uni-manager/locale/manager.ts","../../../projects/uni-manager/locale/uni-manager-locale.ts"],"sourcesContent":["import { BehaviorSubject } from 'rxjs';\r\n\r\nexport class UniLocaleManager {\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* ----------------------------------- Config ------------------------------------ */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /** Codice lingua corrente (es. 'it-IT', 'en-US') usato per formattare date e numeri */\r\n  private static _locale = navigator.language ?? 'en-US';\r\n\r\n  /** Elenco dei codici lingua supportati dall'applicazione (es. ['it-IT', 'en-US']) */\r\n  private static _localesSupported: string[] = [];\r\n\r\n  /** Prefisso globale applicato a tutte le chiavi di traduzione (es. 'APP_') */\r\n  private static _prefix: string | undefined = undefined;\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* --------------------------------- Metodi: get --------------------------------- */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /** Restituisce il codice lingua corrente */\r\n  public static get locale(): string {\r\n    return this._locale;\r\n  }\r\n\r\n  /** Restituisce l'array contenente tutti i codici locale supportati.*/\r\n  public static get localesSupported(): string[] {\r\n    return this._localesSupported;\r\n  }\r\n\r\n  /** Restituisce solo la lingua (es. 'it') */\r\n  public static get language(): string {\r\n    return this._locale.split('-')[0];\r\n  }\r\n\r\n  /** Restituisce solo il paese (es. 'IT') */\r\n  public static get region(): string {\r\n    const parts = this._locale.split('-');\r\n    return parts.length > 1 ? parts[1] : '';\r\n  }\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* ------------------------------------ Store ------------------------------------ */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /** Store privato (Subject) */\r\n  public static store = new BehaviorSubject<Record<string, string>>({});\r\n\r\n  /** Store pubblico (Observable) */\r\n  public static store$ = this.store.asObservable();\r\n\r\n  /** Ottiene il dizionario attuale senza sottoscrizione */\r\n  public static get currentValue(): Record<string, string> {\r\n    return this.store.getValue();\r\n  }\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* -------------------------------- Metodi: setup -------------------------------- */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /**\r\n   * Inizializza la configurazione di rete del manager.\r\n   * Deve essere chiamato prima di effettuare qualsiasi richiesta HTTP.\r\n   */\r\n  public static setup(locale: string | null | undefined, prefix?: string): void {\r\n    this._locale = locale ?? 'en-US';\r\n    this._prefix = prefix;\r\n  }\r\n\r\n  /** Imposta l'elenco dei codici lingua supportati dall'applicazione */\r\n  public static setLocalesSupported(locales: string[] | undefined): void {\r\n    this._localesSupported = locales ?? [];\r\n  }\r\n\r\n  /**\r\n   * Aggiorna lo store locale con il dizionario delle traduzioni fornito.\r\n   * Se viene passato undefined, lo store viene inizializzato come oggetto vuoto.\r\n   */\r\n  public static setTranslations(translations: Record<string, string> | undefined): void {\r\n    this.store.next(translations ?? {});\r\n  }\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* ----------------------------- Metodi: traduzioni ------------------------------ */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /**\r\n   * Traduce una label in base al dizionario caricato.\r\n   * Gestisce la composizione della chiave, i parametri dinamici (interpolazione) e il fallback.\r\n   */\r\n  public static translate(\r\n    key: string,\r\n    prefix = 'lbl',\r\n    params?: Record<string, string | number | Date>,\r\n  ): string {\r\n    if (!key) return '-';\r\n\r\n    // Costruzione chiave: prefissoGlobale + prefissoLocale + LabelConInizialeMaiuscola\r\n    const keyParts = key.trim().split(/(?=[A-Z])/);\r\n    const rootKeyParts = keyParts.filter((p) => p.toLowerCase() !== prefix.toLowerCase());\r\n    const cleanKey = rootKeyParts.length > 0 ? rootKeyParts.join('') : undefined;\r\n    const capitalizedKey = cleanKey ? cleanKey.charAt(0).toUpperCase() + cleanKey.slice(1) : '';\r\n    const finalKey = `${this._prefix ?? ''}${prefix}${capitalizedKey}`;\r\n\r\n    // Cerca la chiave in minuscolo (standardizzazione)\r\n    let translation = this.currentValue?.[finalKey.toLowerCase()];\r\n\r\n    // Log e fallback se la traduzione manca\r\n    if (!translation) {\r\n      console.warn(`Translation missing for key: ${finalKey}`);\r\n      return `🔑 ${finalKey}`;\r\n    }\r\n\r\n    // Interpolazione variabili\r\n    if (params) {\r\n      for (const [key, value] of Object.entries(params)) {\r\n        const displayValue =\r\n          value instanceof Date ? value.toLocaleDateString(this._locale) : String(value);\r\n\r\n        translation = translation.replaceAll(`{{${key}}}`, displayValue);\r\n      }\r\n    }\r\n\r\n    return translation;\r\n  }\r\n\r\n  /**\r\n   * Traduce una stringa che contiene parametri separati da un carattere specifico.\r\n   * Supporta ora un numero arbitrario di parametri in formato \"label/param1/param2\"\r\n   * o semplicemente \"label\".\r\n   */\r\n  public static translateInlineParams(keyWithParams: string, splitChar: string): string {\r\n    if (!keyWithParams) return '-';\r\n\r\n    const [label, ...params] = keyWithParams.split(splitChar);\r\n\r\n    // Se non ci sono parametri, esegui una traduzione semplice\r\n    if (params.length === 0) {\r\n      return this.translate(label);\r\n    }\r\n\r\n    // Mappa i parametri in un oggetto di interpolazione: { inlineParam0: val, inlineParam1: val, ... }\r\n    const interpolationParameters: Record<string, string> = {};\r\n    for (const [index, val] of params.entries()) {\r\n      interpolationParameters[`param${index}`] = val;\r\n    }\r\n\r\n    return this.translate(label, 'lbl', interpolationParameters);\r\n  }\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* ------------------------------- Metodi: numeri -------------------------------- */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /**\r\n   * Converte un valore numerico in una stringa formattata secondo il locale impostato.\r\n   * Disabilita i separatori delle migliaia e forza un numero fisso di decimali.\r\n   */\r\n  public static toStringNumber(value: number, decimal: number): string {\r\n    return new Intl.NumberFormat(this._locale, {\r\n      useGrouping: true,\r\n      minimumFractionDigits: decimal,\r\n      maximumFractionDigits: decimal,\r\n      numberingSystem: 'latn',\r\n    }).format(value);\r\n  }\r\n\r\n  /* ------------------------------------------------------------------------------- */\r\n  /* -------------------------------- Metodi: date --------------------------------- */\r\n  /* ------------------------------------------------------------------------------- */\r\n  /**\r\n   * Formatta una data o una stringa in base al locale corrente.\r\n   * Gestisce tre modalità predefinite (date, time, full) e accetta opzioni personalizzate Intl.\r\n   */\r\n  public static toDate(\r\n    date: Date | string | number,\r\n    mode: 'date' | 'time' | 'full' = 'full',\r\n    force?: { oldLang: string; newLocale: string },\r\n  ): string {\r\n    const dt = new Date(date);\r\n\r\n    // Controllo: validità data\r\n    if (Number.isNaN(dt.getTime())) {\r\n      console.error(`[UniLocaleManager] Data non valida fornita a formatDateTime:`, date);\r\n      return '';\r\n    }\r\n\r\n    // Controllo: locale da forzare\r\n    const locale = force && this._locale.startsWith(force.oldLang) ? force.newLocale : this._locale;\r\n\r\n    switch (mode) {\r\n      case 'date': {\r\n        return dt.toLocaleDateString(locale);\r\n      }\r\n      case 'time': {\r\n        return dt.toLocaleTimeString(locale);\r\n      }\r\n      case 'full': {\r\n        return dt.toLocaleString(locale);\r\n      }\r\n    }\r\n  }\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;MAEa,gBAAgB,CAAA;;;;;AAKZ,IAAA,SAAA,IAAA,CAAA,OAAO,GAAG,SAAS,CAAC,QAAQ,IAAI,OAAO,CAAC;;aAGxC,IAAA,CAAA,iBAAiB,GAAa,EAAE,CAAC;;aAGjC,IAAA,CAAA,OAAO,GAAuB,SAAS,CAAC;;;;;AAMhD,IAAA,WAAW,MAAM,GAAA;QACtB,OAAO,IAAI,CAAC,OAAO;IACrB;;AAGO,IAAA,WAAW,gBAAgB,GAAA;QAChC,OAAO,IAAI,CAAC,iBAAiB;IAC/B;;AAGO,IAAA,WAAW,QAAQ,GAAA;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACnC;;AAGO,IAAA,WAAW,MAAM,GAAA;QACtB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC;AACrC,Q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