{
  "version": 3,
  "sources": ["../../src/index.ts"],
  "sourcesContent": ["import { ErrorProps } from './types'\n\n/**\n * A utility function for handling asynchronous operations with error handling.\n *\n * This function wraps an asynchronous operation (`fn`) in a try-catch block,\n * providing a consistent way to handle errors and capturing useful metadata\n * such as the execution time and timestamp.\n *\n * ### Features:\n * - Encapsulates `try-catch` logic for asynchronous operations.\n * - Returns a tuple `[data, error, metadata]` for flexible handling.\n * - Provides error details, including message, stack, function name, and location.\n * - Captures execution time and timestamp for monitoring and debugging.\n *\n * @template T The expected return type of the asynchronous operation.\n *\n * @param fn A function that returns a `Promise<T>`. This is the asynchronous operation to be executed.\n * @param initialValue A fallback value of type `T` to return in case of an error.\n * @param additionalContext (Optional) Additional context to include in the error details, as a key-value object.\n *\n * @returns A Promise resolving to a tuple:\n * - **[0]**: The result of the operation (`T`), or `initialValue` if an error occurs.\n * - **[1]**: An `ErrorProps` object containing error details, or `null` if no error occurs.\n * - **[2]** (Optional): Metadata about the operation, including:\n *   - `executionTimeMs`: The time taken to execute the operation (in milliseconds).\n *   - `timestamp`: A timestamp of when the operation started.\n *\n * ### Example Usage\n *\n * ```typescript\n * import tryState from '@ipcom/try-state';\n *\n * const fetchData = async () => {\n *   return new Promise((resolve, reject) => {\n *     setTimeout(() => {\n *       Math.random() > 0.5 ? resolve('Success') : reject(new Error('Random Error'));\n *     }, 500);\n *   });\n * };\n *\n * (async () => {\n *   const [data, error, meta] = await tryState(fetchData, 'Default Value');\n *\n *   if (error) {\n *     console.error('Error occurred:', error.message);\n *     console.error('Error location:', error.location);\n *     console.error('Execution time:', meta?.executionTimeMs, 'ms');\n *     console.error('Timestamp:', meta?.timestamp);\n *   } else {\n *     console.log('Success:', data);\n *     console.log('Execution time:', meta?.executionTimeMs, 'ms');\n *     console.log('Timestamp:', meta?.timestamp);\n *   }\n * })();\n * ```\n */\n\nexport const tryState = async <T>(\n    fn: () => Promise<T>,\n    initialValue: T,\n    additionalContext?: Record<string, unknown>\n): Promise<[T, ErrorProps | null, { executionTimeMs: number; timestamp: string }?]> => {\n    const start = Date.now(); // Captura o tempo de in\u00EDcio\n    const timestamp = new Date().toISOString(); // Timestamp do in\u00EDcio da execu\u00E7\u00E3o\n\n    try {\n        const result = await fn();\n        const executionTimeMs = Date.now() - start; // Calcula o tempo de execu\u00E7\u00E3o\n        return [\n            result,\n            null,\n            { executionTimeMs, timestamp },\n        ];\n    } catch (error) {\n        const executionTimeMs = Date.now() - start; // Calcula o tempo at\u00E9 o erro\n        const stack = (error as Error).stack || '';\n        const message = (error as Error).message || 'An unknown error occurred';\n\n        // Converte qualquer coisa em um objeto\n        const errorObject = typeof error === 'object' && error !== null ? error : { error };\n\n        const errorDetails: ErrorProps = {\n            message,\n            stack,\n            ...errorObject, // Espalha propriedades adicionais\n            ...additionalContext, // Adiciona contexto adicional, se houver\n        };\n\n        return [\n            initialValue,\n            errorDetails,\n            { executionTimeMs, timestamp },\n        ];\n    }\n};\n\nexport default tryState;\n"],
  "mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,aAAAC,IAAA,eAAAC,EAAAJ,GA0DO,IAAMG,EAAW,MACpBE,EACAC,EACAC,IACmF,CACnF,IAAMC,EAAQ,KAAK,IAAI,EACjBC,EAAY,IAAI,KAAK,EAAE,YAAY,EAEzC,GAAI,CACA,IAAMC,EAAS,MAAML,EAAG,EAClBM,EAAkB,KAAK,IAAI,EAAIH,EACrC,MAAO,CACHE,EACA,KACA,CAAE,gBAAAC,EAAiB,UAAAF,CAAU,CACjC,CACJ,OAASG,EAAO,CACZ,IAAMD,EAAkB,KAAK,IAAI,EAAIH,EAC/BK,EAASD,EAAgB,OAAS,GAMlCE,EAA2B,CAC7B,QANaF,EAAgB,SAAW,4BAOxC,MAAAC,EACA,GALgB,OAAOD,GAAU,UAAYA,IAAU,KAAOA,EAAQ,CAAE,MAAAA,CAAM,EAM9E,GAAGL,CACP,EAEA,MAAO,CACHD,EACAQ,EACA,CAAE,gBAAAH,EAAiB,UAAAF,CAAU,CACjC,CACJ,CACJ,EAEOP,EAAQC",
  "names": ["src_exports", "__export", "src_default", "tryState", "__toCommonJS", "fn", "initialValue", "additionalContext", "start", "timestamp", "result", "executionTimeMs", "error", "stack", "errorDetails"]
}
