import { parseTypeId , ParsedTypeId } from '../utils/parse-type-id' ; export interface StorageLayout { layoutVersion ? : string ; storage : StorageItem [ ] ; types : Record < string , TypeItem > ; } export interface StorageItem < Type = string > { contract : string ; label : string ; type : Type ; src : string ; } export interface TypeItem < Type = string > { label : string ; members ? : TypeItemMembers < Type > ; } export type TypeItemMembers < Type = string > = StructMember < Type > [ ] | EnumMember [ ] ; export interface StructMember < Type = string > { label : string ; type : Type ; } export type EnumMember = string ; export interface ParsedTypeDetailed extends ParsedTypeId { item : TypeItem < ParsedTypeDetailed > ; args ? : ParsedTypeDetailed [ ] ; rets ? : ParsedTypeDetailed [ ] ; } type Replace < T , K extends string , V > = Omit < T , K > & Record < K , V > ; export function getDetailedLayout ( layout ) { const cache = { } ; return layout . storage . map ( parseWithDetails ) ; function parseWithDetails < I extends { type : string ; } > ( item ) { const parsed = parseTypeId ( item . type ) ; const withDetails = addDetailsToParsedType ( parsed ) ; return { ... item , type : withDetails } ; } function addDetailsToParsedType ( parsed ) { if ( parsed . id in cache ) { return cache [ parsed . id ] ; } const item = layout . types [ parsed . id ] ; const detailed = { ... parsed , args : undefined , rets : undefined , item : { ... item , members : undefined , } , } ; cache [ parsed . id ] = detailed ; detailed . args = parsed . args ?. map ( addDetailsToParsedType ) ; detailed . rets = parsed . args ?. map ( addDetailsToParsedType ) ; detailed . item . members = item ?. members && ( isStructMembers ( item ?. members ) ? item . members . map ( parseWithDetails ) : item ?. members ) ; return detailed ; } } export function isEnumMembers < T > ( members ) { return members . length === 0 || typeof members [ 0 ] === 'string' ; } export function isStructMembers < T > ( members ) { return members . length === 0 || typeof members [ 0 ] === 'object' ; }