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AAe;AACb,IAAA,OAAO,CAAA,KAAA,EAAQ,IAAA,CAAK,CAAA,MAAA,CAAQ,SAAS,CAAA,CAAA;AACvC,EAAA;AAAA;AAAA;AAAA;AAAA;AAMqB,EAAA;AACkB,IAAA;AAEF,IAAA;AACjC,MAAA;AACA,MAAA;AACY,MAAA;AACb,IAAA;AAEsB,IAAA;AACzB,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWqC,EAAA;AAC/B,IAAA;AACkC,MAAA;AACP,QAAA;AAC5B,MAAA;AAE+B,MAAA;AACzB,MAAA;AACO,IAAA;AACmB,MAAA;AACd,MAAA;AAET,MAAA;AACuB,wBAAA;AACnB,QAAA;AACd,MAAA;AACF,IAAA;AACF,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBc,EAAA;AACuB,IAAA;AACA,IAAA;AAEf,IAAA;AACX,MAAA;AACT,IAAA;AAEqB,IAAA;AACY,IAAA;AACnC,EAAA;AACF;AAEe;ADlC2B;AACA;AACA;AACA","file":"/Users/JOHNSON028877/Documents/new-juf.js/dist/chunk-UD6WYISC.cjs","sourcesContent":[null,"/**\n * @namespace Service\\Authentication\n */\n\nimport { CachingSystem } from '../core/cache.js';\nimport { envConfig } from '../../config/index.js';\nimport { getApiUrl } from '../core/apiUrl.js';\nimport Requester from '../core/requester.js';\nimport { logger as rootLogger } from '../core/logger.js';\nimport { AuthenticationError } from '../core/errors.js';\nimport { OAUTH_CONTENT_TYPE, OAUTH_GRANT_TYPE, OAUTH_TOKEN_PATH } from '../core/constants.js';\n\n/**\n * @class Authentication\n * @classdesc Provides methods to handle authentication and token management with the Sonatel API Platform.\n * Utilizes caching to store tokens and optimizes API requests by reducing unnecessary network calls.\n *\n * Supports dependency injection for testability — pass dependencies via constructor,\n * or use the {@link Authentication.init} factory for default behavior.\n *\n * @example\n * // Default usage (backward compatible)\n * const authentication = Authentication.init();\n * const { access_token } = await authentication.debug();\n *\n * @example\n * // With dependency injection (for testing)\n * const authentication = new Authentication({ config, cache: mockCache, client: mockAxios, logger: mockLogger });\n *\n * @typedef {import('../core/cache.js').default} Cache\n */\nclass Authentication {\n  /** @private @type {Cache} */\n  #cache;\n\n  /**\n   * @private\n   * @type {{\n   *  onProd: boolean,\n   *  onPProd: boolean,\n   *  url: { production: string, sandbox: string, preprod: string },\n   *  client_id: string,\n   *  client_secret: string,\n   *  decode_qr_sp_authorization: string\n   * }}\n   */\n  #config;\n\n  /** @private @type {import('axios').AxiosInstance} */\n  #client;\n\n  /** @private */\n  #logger;\n\n  /**\n   * Creates an Authentication instance with injectable dependencies.\n   *\n   * @param {object} deps - Dependencies for the authentication service.\n   * @param {object} deps.config - Apigee configuration (client_id, client_secret, urls, etc.).\n   * @param {Cache} deps.cache - Cache instance for token storage.\n   * @param {import('axios').AxiosInstance} deps.client - HTTP client instance.\n   * @param {object} deps.logger - Logger instance with error/warn/info/debug methods.\n   */\n  constructor({ config, cache, client, logger }) {\n    this.#config = config;\n    this.#cache = cache;\n    this.#client = client;\n    this.#logger = logger;\n  }\n\n  /**\n   * Factory method to initialize Authentication with default dependencies.\n   * @memberof Service\\Authentication\n   * @method init\n   * @returns {Authentication} An initialized instance of Authentication.\n   */\n  static init() {\n    const apigeeConfig = envConfig.get('apigee');\n\n    return new Authentication({\n      config: apigeeConfig,\n      cache: CachingSystem,\n      client: Requester.bootstrap({ baseURL: getApiUrl() }),\n      logger: rootLogger.child('auth'),\n    });\n  }\n\n  /**\n   * @private\n   * @returns {string} The generated cache key.\n   */\n  #getCacheKey() {\n    return `auth_${this.#config.client_id}`;\n  }\n\n  /**\n   * @private\n   * @returns {string} URL-encoded payload for the authentication request.\n   */\n  #createAuthPayload() {\n    const { client_id, client_secret } = this.#config;\n\n    const params = new URLSearchParams({\n      client_id,\n      client_secret,\n      grant_type: OAUTH_GRANT_TYPE,\n    });\n\n    return params.toString();\n  }\n\n  /**\n   * Makes the API call to fetch the authentication token and handles caching.\n   *\n   * @private\n   * @param {string} payload - The URL-encoded payload for the authentication request.\n   * @param {string} cacheKey - The cache key to store the token.\n   * @returns {Promise<object>} The authentication response.\n   * @throws {AuthenticationError} When the API request fails.\n   */\n  async #fetchToken(payload, cacheKey) {\n    try {\n      const { data } = await this.#client.post(OAUTH_TOKEN_PATH, payload, {\n        headers: { 'Content-Type': OAUTH_CONTENT_TYPE },\n      });\n\n      this.#cache.store(cacheKey, data);\n      return data;\n    } catch (error) {\n      const upstream = error.response?.data;\n      this.#logger.error('Authentication request failed', { status: error.response?.status });\n\n      throw new AuthenticationError(\n        upstream?.error_description || 'Authentication request failed. Check your configuration.',\n        upstream || null,\n      );\n    }\n  }\n\n  /**\n   * Authenticates against the Sonatel API Platform using the client credentials flow.\n   * Caches the access token to reduce redundant authentication calls.\n   *\n   * @async\n   * @method debug\n   * @memberof Service\\Authentication\n   * @returns {Promise<{ access_token: string, expires_in: number, refresh_expires_in: number, token_type: string, notbefore_policy: number, session_state: string }>}\n   * @throws {AuthenticationError} When authentication fails.\n   *\n   * @example\n   * authentication.debug()\n   *   .then(console.log)\n   *   .catch(({ message }) => console.log(`Failed: ${message}`))\n   */\n  async debug() {\n    const cacheKey = this.#getCacheKey();\n    const cachedResponse = this.#cache.retrieve(cacheKey);\n\n    if (cachedResponse) {\n      return cachedResponse;\n    }\n\n    const payload = this.#createAuthPayload();\n    return this.#fetchToken(payload, cacheKey);\n  }\n}\n\nexport default Authentication;\n"]}