// Copyright 2017-2023 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; import type { BN } from '@polkadot/util'; import type { DeriveApi } from '../types.js'; import type { Bag, BagExpanded } from './types.js'; import { map, switchMap } from 'rxjs'; import { objectSpread } from '@polkadot/util'; import { memo } from '../util/index.js'; export function expand (instanceId: string, api: DeriveApi): (bag: Bag) => Observable { return memo(instanceId, (bag: Bag): Observable => api.derive.bagsList.listNodes(bag.bag).pipe( map((nodes) => objectSpread({ nodes }, bag)) ) ); } export function getExpanded (instanceId: string, api: DeriveApi): (id: BN | number) => Observable { return memo(instanceId, (id: BN | number): Observable => api.derive.bagsList.get(id).pipe( switchMap((bag) => api.derive.bagsList.expand(bag) ) ) ); }