{
  "version": 3,
  "sources": ["../../src/modules/payload-diviner/contexts/DivinedPayload/Context.ts", "../../src/modules/payload-diviner/contexts/DivinedPayload/Provider.tsx", "../../src/modules/payload-diviner/contexts/PayloadDiviner/Context.ts", "../../src/modules/payload-diviner/contexts/PayloadDiviner/Provider.tsx", "../../src/modules/payload-diviner/contexts/PayloadDiviner/use.ts", "../../src/modules/payload-diviner/contexts/DivinedPayload/use.ts"],
  "sourcesContent": ["import { createContextEx } from '@ariestools/sdk-react/shared'\n\nimport type { DivinedPayloadState } from './State.ts'\n\n/** React context for a divined payload and related error state. */\nexport const DivinedPayloadContext = createContextEx<DivinedPayloadState>()\n", "import { ErrorRender } from '@ariestools/sdk-react/error'\nimport { ModuleErrorSchema } from '@xyo-network/sdk'\nimport type { PropsWithChildren } from 'react'\nimport React, { useMemo } from 'react'\nimport { useParams, useSearchParams } from 'react-router'\n\nimport { useBuildHuri } from '#payload-huri'\n\nimport { useDivinePayload } from '../PayloadDiviner/index.ts'\nimport { DivinedPayloadContext } from './Context.ts'\nimport type { DivinedPayloadState } from './State.ts'\nimport { useDivinedPayload } from './use.ts'\n\n/** Props for {@link DivinedPayloadProvider}. */\nexport interface DivinedPayloadProviderProps extends PropsWithChildren {\n  hash?: string\n}\n\n/** Resolves a payload from a hash/HURI and provides it via context. */\nexport const DivinedPayloadProvider: React.FC<DivinedPayloadProviderProps> = ({ children, hash }) => {\n  const { hash: hashParam } = useParams()\n\n  const huriFromHashParam = useBuildHuri(hashParam)\n\n  const [params] = useSearchParams()\n  const huriSearchParameter = params.get('huri')\n  const decodedHuriParam = decodeURIComponent(huriSearchParameter ?? '')\n  const huriUri = hash ?? decodedHuriParam ?? huriFromHashParam\n\n  const [payload, setPayload, payloadError] = useDivinePayload(huriUri)\n\n  const value: DivinedPayloadState = useMemo(() => ({\n    payload, payloadError, provided: true, setPayload,\n  }), [payload, payloadError, setPayload])\n\n  return (\n    <DivinedPayloadContext value={value}>\n      {children}\n    </DivinedPayloadContext>\n  )\n}\n\n/** Renders children with error handling for a failed divined payload. */\nexport const DivinedPayloadWithHandleInner: React.FC<PropsWithChildren> = ({ children }) => {\n  const { payloadError } = useDivinedPayload()\n\n  return (\n    <ErrorRender\n      error={payloadError\n        ? {\n            message: payloadError.message, schema: ModuleErrorSchema, sources: [],\n          }\n        : undefined}\n      errorContext=\"Divined Payload Provider\"\n    >\n      {children}\n    </ErrorRender>\n  )\n}\n\n/** Divined payload provider that also surfaces resolution errors in the UI. */\nexport const DivinedPayloadWithHandleProvider: React.FC<DivinedPayloadProviderProps> = ({ children, ...props }) => {\n  return (\n    <DivinedPayloadProvider {...props}>\n      <DivinedPayloadWithHandleInner>{children}</DivinedPayloadWithHandleInner>\n    </DivinedPayloadProvider>\n  )\n}\n", "import { createContextEx } from '@ariestools/sdk-react/shared'\n\nimport type { PayloadDivinerState } from './State.ts'\n\n/** React context for the active payload diviner instance. */\nconst PayloadDivinerContext = createContextEx<PayloadDivinerState>()\n\nexport { PayloadDivinerContext }\n", "import { useResetState } from '@ariestools/sdk-react/hooks'\nimport type { ContextExProviderProps } from '@ariestools/sdk-react/shared'\nimport type { PayloadDiviner } from '@xyo-network/sdk/diviner-payload-abstract'\nimport React, { useMemo } from 'react'\n\nimport { PayloadDivinerContext } from './Context.ts'\nimport type { PayloadDivinerState } from './State.ts'\n\n/** Props for {@link PayloadDivinerProvider}. */\nexport type PayloadDivinerProviderProps = ContextExProviderProps<{\n  /** Required */\n  diviner?: PayloadDiviner\n}>\n\n/** Provides a payload diviner instance to descendants. */\nexport const PayloadDivinerProvider: React.FC<PayloadDivinerProviderProps> = ({\n  diviner: divinerProp, required = false, children,\n}) => {\n  const [diviner, setDiviner] = useResetState<PayloadDiviner | undefined>(divinerProp)\n\n  const value: PayloadDivinerState = useMemo(() => ({\n    diviner: diviner === divinerProp ? diviner : undefined, provided: true, setDiviner,\n  }), [diviner, divinerProp, setDiviner])\n\n  return (\n    <PayloadDivinerContext\n      value={value}\n    >\n      {diviner\n        ? children\n        : required\n          ? null\n          : children}\n    </PayloadDivinerContext>\n  )\n}\n", "import { exists } from '@ariestools/sdk'\nimport { useAsyncEffect } from '@ariestools/sdk-react/async-effect'\nimport { useContextEx } from '@ariestools/sdk-react/shared'\nimport type { HuriPayload } from '@xyo-network/diviner-huri'\nimport { HuriSchema } from '@xyo-network/diviner-huri'\nimport type { Payload } from '@xyo-network/sdk'\nimport type { Dispatch } from 'react'\nimport { useEffect, useState } from 'react'\n\nimport { PayloadDivinerContext } from './Context.ts'\n\n/** Consumes the payload diviner context. */\nexport const usePayloadDiviner = (required = false) => {\n  return useContextEx(PayloadDivinerContext, 'PayloadDiviner', required)\n}\n\n/** Divines a single payload for the given HURI using the context diviner. */\nexport const useDivinePayload = <T extends Payload = Payload>(\n  huri?: string,\n): [T | undefined | null, Dispatch<T | null | undefined>, Error | undefined] => {\n  const { diviner } = usePayloadDiviner()\n  const [payload, setPayload] = useState<T | null>()\n  const [error, setError] = useState<Error>()\n\n  useEffect(() => {\n    // we do this to clear the payload when the diviner changes\n    if (diviner) {\n      // eslint-disable-next-line react-hooks/set-state-in-effect, react-x/set-state-in-effect\n      setPayload(undefined)\n    }\n  }, [diviner])\n\n  useAsyncEffect(\n    async (mounted) => {\n      if (huri && diviner && payload === undefined) {\n        try {\n          const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n          const [payload] = (await diviner?.divine([huriPayload])) ?? []\n          if (mounted()) {\n            setPayload((payload as T) || null)\n          }\n        } catch (ex) {\n          if (mounted()) {\n            setError(ex as Error)\n          }\n        }\n      }\n    },\n    [diviner, huri, payload],\n  )\n\n  return [payload, setPayload, error]\n}\n\n/** Divines payloads for a list of HURIs using the context diviner. */\nexport const useDivinePayloads = <T extends Payload = Payload>(\n  huriList: string[],\n): [(T | null)[] | undefined, Dispatch<(T | null)[] | undefined>, Error[] | undefined] => {\n  const { diviner } = usePayloadDiviner()\n  const [payloads, setPayloads] = useState<(T | null)[]>()\n  const [errors, setErrors] = useState<Error[]>()\n\n  useEffect(() => {\n    if (diviner) {\n      // clear payloads when diviner changes\n      // eslint-disable-next-line react-hooks/set-state-in-effect, react-x/set-state-in-effect\n      setPayloads(undefined)\n    }\n  }, [diviner])\n\n  useAsyncEffect(\n    async (mounted) => {\n      console.log(`huriList: ${JSON.stringify(huriList, null, 2)}`)\n      const payloads = await Promise.allSettled(\n        huriList.map(async (huri) => {\n          const huriPayload: HuriPayload = { huri: [huri], schema: HuriSchema }\n          const [payload] = (await diviner?.divine([huriPayload])) ?? []\n          return payload\n        }),\n      )\n      if (mounted()) {\n        setPayloads([...payloads.values()].map(value => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n        setErrors(\n          (\n            [...payloads.values()].map(value => (value.status === 'rejected' ? new Error('divine failed', { cause: value.reason }) : undefined))\n          ).filter(exists),\n        )\n        if (mounted()) {\n          setPayloads([...payloads.values()].map(value => (value.status === 'rejected' ? null : value.value)) as (T | null)[])\n          setErrors(\n            (\n              [...payloads.values()].map(value => (value.status === 'rejected' ? new Error('divine failed', { cause: value.reason }) : undefined))\n            ).filter(exists),\n          )\n        }\n      }\n    },\n    [diviner, huriList, payloads],\n  )\n\n  return [payloads, setPayloads, errors]\n}\n", "import { useContextEx } from '@ariestools/sdk-react/shared'\n\nimport { DivinedPayloadContext } from './Context.ts'\n\n/** Consumes the divined payload context for the resolved payload and error. */\nexport const useDivinedPayload = () => useContextEx(DivinedPayloadContext, 'DivinedPayload', true)\n"],
  "mappings": ";AAAA,SAAS,uBAAuB;AAKzB,IAAM,wBAAwB,gBAAqC;;;ACL1E,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAElC,SAAgB,WAAAA,gBAAe;AAC/B,SAAS,WAAW,uBAAuB;AAE3C,SAAS,oBAAoB;;;ACN7B,SAAS,mBAAAC,wBAAuB;AAKhC,IAAM,wBAAwBA,iBAAqC;;;ACLnE,SAAS,qBAAqB;AAG9B,SAAgB,eAAe;AAsB3B;AAVG,IAAM,yBAAgE,CAAC;AAAA,EAC5E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