// Copyright 2017-2023 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; import type { Option } from '@polkadot/types'; import type { H256 } from '@polkadot/types/interfaces'; import type { FrameSupportPreimagesBounded, PalletDemocracyVoteThreshold } from '@polkadot/types/lookup'; import type { ITuple } from '@polkadot/types/types'; import type { DeriveApi, DeriveProposalExternal } from '../types.js'; import { map, of, switchMap } from 'rxjs'; import { memo } from '../util/index.js'; import { getImageHashBounded } from './util.js'; function withImage (api: DeriveApi, nextOpt: Option>): Observable { if (nextOpt.isNone) { return of(null); } const [hash, threshold] = nextOpt.unwrap(); return api.derive.democracy.preimage(hash).pipe( map((image): DeriveProposalExternal => ({ image, imageHash: getImageHashBounded(hash), threshold })) ); } export function nextExternal (instanceId: string, api: DeriveApi): () => Observable { return memo(instanceId, (): Observable => api.query.democracy?.nextExternal ? api.query.democracy.nextExternal().pipe( switchMap((nextOpt) => withImage(api, nextOpt)) ) : of(null) ); }