{"version":3,"sources":["../../src/react/ternSecureProvider.tsx","../../src/react/ternsecureCtx.ts","../../src/react/hooks/useTernSecure.ts","../../src/react/ternSecureAuthProvider.tsx"],"sourcesContent":["'use client'\n\nimport type { \n  TernSecureAuthProvider,\n  TernSecureInstanceTree,\n  TernSecureState,\n} from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\nexport type TernSecureAuthContextType = {\n  authProvider: TernSecureAuthProvider | null | undefined;\n  authState: TernSecureState;\n}\n\n\n// Create TernSecure instance context\nconst [TernSecureInstanceContext, useTernSecureInstanceContext] = \n  createContextAndHook<TernSecureInstanceTree>('TernSecureInstanceContext');\n\nconst [TernSecureAuthContext, useTernSecureAuthContext] =\n  createContextAndHook<TernSecureAuthContextType>('TernSecureAuthContext');\n\nconst [SessionContext, useSessionContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['session']>('SessionContext');\n\nconst [UserContext, useUserContext] = \ncreateContextAndHook<TernSecureInstanceTree['auth']['user']>('UserContext');\n\n// Assert helper\nfunction useAssertWrappedByTernSecureProvider(displayNameOrFn: string | (() => void)): void {\n  //const ctx = useTernSecureInstanceContext();\n  const ctx = useContext(TernSecureInstanceContext);\n  \n  if (!ctx) {\n    if (typeof displayNameOrFn === 'function') {\n      displayNameOrFn();\n      return;\n    }\n\n    throw new Error(\n      `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n      \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`\n    );\n  }\n}\n\n\nexport {\n  TernSecureInstanceContext,\n  TernSecureAuthContext,\n  SessionContext,\n  UserContext,\n  useTernSecureAuthContext,\n  useSessionContext,\n  useUserContext,\n  useTernSecureInstanceContext,\n  useAssertWrappedByTernSecureProvider\n};","'use client'\n\nimport React from 'react';\n\n/**\n * Assert that the context value exists, otherwise throw an error.\n *\n * @internal\n */\nexport function assertContextExists(contextVal: unknown, msgOrCtx: string | React.Context<any>): asserts contextVal {\n  if (!contextVal) {\n    throw typeof msgOrCtx === 'string' ? new Error(msgOrCtx) : new Error(`${msgOrCtx.displayName} not found`);\n  }\n}\ntype Options = { assertCtxFn?: (v: unknown, msg: string) => void };\ntype ContextAndHook<T> = React.Context<{ value: T } | undefined>;\ntype UseCtxFn<T> = () => T;\n\n/**\n * Create and return a Context and two hooks that return the context value.\n * The Context type is derived from the type passed in by the user.\n *\n * The first hook returned guarantees that the context exists so the returned value is always `CtxValue`\n * The second hook makes no guarantees, so the returned value can be `CtxValue | undefined`\n *\n * @internal\n */\n\nexport const createContextAndHook = <CtxValue>(\n  displayName: string,\n  options?: Options,\n): [ContextAndHook<CtxValue>, UseCtxFn<CtxValue>, UseCtxFn<CtxValue | Partial<CtxValue>>] => {\n  const { assertCtxFn = assertContextExists } = options || {};\n  const Ctx = React.createContext<{ value: CtxValue } | undefined >(undefined);\n  Ctx.displayName = displayName;\n\n  const useCtx = () => {\n    const ctx = React.useContext(Ctx);\n    assertCtxFn(ctx, `${displayName} not found`);\n    return (ctx as any).value as CtxValue;\n  };\n\n  const useCtxWithoutGuarantee = () => {\n    const ctx = React.useContext(Ctx);\n    return ctx ? ctx.value : {}\n  };\n\n    /**\n     * Assert that the context value exists, otherwise throw an error.\n    if (ctx === undefined) {\n      throw new Error(`use${name} must be used within a ${name}Provider`);\n    }\n    return ctx.value;\n    */\n\n  return [Ctx, useCtx, useCtxWithoutGuarantee];\n}","import type { \n    TernSecureInstanceTree\n} from \"@tern-secure/types\";\n\nimport { \n    useAssertWrappedByTernSecureProvider, \n    useTernSecureInstanceContext \n} from \"../ternSecureProvider\";\n\nexport const useTernSecure = (): TernSecureInstanceTree => {\n    /**\n     * if no assertion is needed, you can use the following:\n     *  const instance  = useTernSecureInstanceContext();\n     *  if (!instance) {\n     *      throw new Error('useTernSecure must be used within a TernSecureProvider');\n     *  }\n     *  return instance;\n     */\n    \n    useAssertWrappedByTernSecureProvider('useTernSecure');\n    return useTernSecureInstanceContext();\n}","'use client'\n\nimport type { \n  TernSecureAuth,\n} from '@tern-secure/types';\nimport { useContext } from 'react';\n\nimport { createContextAndHook } from './ternsecureCtx';\n\n\nconst [TernSecureAuthCtx, useTernSecureAuthCtx] =\n  createContextAndHook<TernSecureAuth>('TernSecureAuthCtx');\n\nfunction useAssertWrappedByTernSecureAuthProvider(displayNameOrFn: string | (() => void)): void {\n  //const ctx = useTernSecureInstanceContext();\n  const ctx = useContext(TernSecureAuthCtx);\n  \n  if (!ctx) {\n    if (typeof displayNameOrFn === 'function') {\n      displayNameOrFn();\n      return;\n    }\n    \n    throw new Error(\n      `${displayNameOrFn} can only be used within the <TernSecureProvider /> component.\n      \nPossible fixes:\n1. Ensure that the <TernSecureProvider /> is correctly wrapping your application\n2. Check for multiple versions of @tern-secure packages in your project`\n    );\n  }\n}\n\n\n\nexport {\n  TernSecureAuthCtx,\n  useTernSecureAuthCtx,\n  useAssertWrappedByTernSecureAuthProvider,\n};"],"mappings":";AAOA,SAAS,kBAAkB;;;ACL3B,OAAO,WAAW;AAOX,SAAS,oBAAoB,YAAqB,UAA2D;AAClH,MAAI,CAAC,YAAY;AACf,UAAM,OAAO,aAAa,WAAW,IAAI,MAAM,QAAQ,IAAI,IAAI,MAAM,GAAG,SAAS,WAAW,YAAY;AAAA,EAC1G;AACF;AAeO,IAAM,uBAAuB,CAClC,aACA,YAC2F;AAC3F,QAAM,EAAE,cAAc,oBAAoB,IAAI,WAAW,CAAC;AAC1D,QAAM,MAAM,MAAM,cAAgD,MAAS;AAC3E,MAAI,cAAc;AAElB,QAAM,SAAS,MAAM;AACnB,UAAM,MAAM,MAAM,WAAW,GAAG;AAChC,gBAAY,KAAK,GAAG,WAAW,YAAY;AAC3C,WAAQ,IAAY;AAAA,EACtB;AAEA,QAAM,yBAAyB,MAAM;AACnC,UAAM,MAAM,MAAM,WAAW,GAAG;AAChC,WAAO,MAAM,IAAI,QAAQ,CAAC;AAAA,EAC5B;AAUA,SAAO,CAAC,K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