import { ObjectSchema , SchemaType , CodeModel } from "@autorest/codemodel" ; import { getLanguageMetadata } from "./languageHelpers" ; /**
 * This function sorts objects in a topographical order
 * this is to make sure that mappers of parents are generated
 * before children to avoid issues trying to reference a mapper
 * before being defined.
 */ export function sortObjectSchemasHierarchically ( codeModel ) { const stack = ( codeModel . schemas . objects || [ ] ) . filter ( o => ! o . parents || o . parents . all . length === 0 || ! ( o . parents . all . length && o . parents . all . some ( p => p . type === SchemaType . Object ) ) ) ; const result = [ ] ; const processed = new Set < string > ( ) ; while ( stack . length ) { const current = stack . shift ( ) ; if ( ! current ) { continue ; } const { name } = getLanguageMetadata ( current . language ) ; if ( processed . has ( name ) ) { continue ; } result . push ( current ) ; processed . add ( name ) ; const currentChildren = ! current . children ? [ ] : ( current . children . immediate . filter ( c => c . type === SchemaType . Object ) as ObjectSchema [ ] ) ; stack . push ( ... currentChildren ) ; } codeModel . schemas . objects = result ; }