{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-playback-source",
  "registryDependencies": [
    "https://limeplay.winoffrg.dev/r/use-asset.json",
    "https://limeplay.winoffrg.dev/r/use-player.json"
  ],
  "files": [
    {
      "path": "registry/default/hooks/use-playback-source.ts",
      "content": "\"use client\"\n\nimport { useEffect, useMemo, useRef } from \"react\"\n\nimport type {\n  GetAssetId,\n  PlayerSource,\n  TAsset,\n  UseAssetOptions,\n} from \"@/registry/default/hooks/use-asset\"\n\nimport {\n  AssetSourceOrigin,\n  AssetSourceType,\n  useAsset,\n} from \"@/registry/default/hooks/use-asset\"\nimport { usePlayerStore } from \"@/registry/default/hooks/use-player\"\n\nexport interface PlaybackSourceControllerProps<TItem extends TAsset> {\n  autoLoad?: boolean\n  initialIndex?: number\n  loading?: UseAssetOptions<TItem>\n  source?: PlayerSource<TItem>\n  sourceKey?: string\n}\n\nexport function PlaybackSourceController<TItem extends TAsset>(\n  props: PlaybackSourceControllerProps<TItem>\n) {\n  usePlaybackSource(props)\n\n  return null\n}\n\nfunction getSourceOrigin<TItem extends TAsset>(\n  source: PlayerSource<TItem> | undefined\n): Parameters<GetAssetId<TItem>>[1][\"origin\"] {\n  if (Array.isArray(source)) return AssetSourceOrigin.Playlist\n  if (typeof source === \"string\") return AssetSourceOrigin.MediaProps\n  return AssetSourceOrigin.Asset\n}\n\nfunction usePlaybackSource<TItem extends TAsset>(\n  options: PlaybackSourceControllerProps<TItem>\n): void {\n  const {\n    autoLoad = true,\n    initialIndex = 0,\n    loading,\n    source,\n    sourceKey,\n  } = options\n\n  const assets = useMemo(() => {\n    if (Array.isArray(source)) return [...source]\n    if (typeof source === \"string\") return [{ src: source } as TItem]\n    if (source) return [source]\n\n    return []\n  }, [source])\n  const player = usePlayerStore((state) => state.instance)\n  const { loadSource } = useAsset<TItem>()\n  const loadedSourceKeyRef = useRef<null | string>(null)\n\n  useEffect(() => {\n    loadedSourceKeyRef.current = null\n  }, [player])\n\n  const resolvedSourceKey = useMemo(() => {\n    if (sourceKey) return sourceKey\n\n    return assets\n      .map((item, index) => {\n        const id =\n          item.id ??\n          loading?.getAssetId?.(item, {\n            index,\n            origin: getSourceOrigin(source),\n          }) ??\n          item.src\n\n        return id ?? `item:${index}`\n      })\n      .join(\"|\")\n  }, [assets, loading, source, sourceKey])\n  const sourceType = useMemo<AssetSourceType>(() => {\n    return Array.isArray(source)\n      ? AssetSourceType.Playlist\n      : AssetSourceType.Asset\n  }, [source])\n  const requestKey = useMemo(\n    () => `${resolvedSourceKey}::${sourceType}::${initialIndex}`,\n    [initialIndex, resolvedSourceKey, sourceType]\n  )\n\n  useEffect(() => {\n    loadedSourceKeyRef.current = null\n  }, [requestKey])\n\n  useEffect(() => {\n    if (!autoLoad || !player || source === undefined || assets.length === 0)\n      return\n\n    if (loadedSourceKeyRef.current === requestKey) return\n    loadedSourceKeyRef.current = requestKey\n\n    loadSource(source, {\n      initialIndex,\n      loading,\n      sourceKey,\n      sourceType,\n    })\n  }, [\n    assets,\n    autoLoad,\n    initialIndex,\n    loadSource,\n    player,\n    requestKey,\n    resolvedSourceKey,\n    source,\n    sourceKey,\n    sourceType,\n    loading,\n  ])\n}\n",
      "type": "registry:hook",
      "target": "hooks/limeplay/use-playback-source.ts"
    }
  ],
  "type": "registry:hook"
}