import yaml from "js-yaml" ; import { BrickConf } from "@next-core/brick-types" ; import { SnippetNodeInstance } from "../interfaces" ; const jsonFieldsInBrick = [ "properties" , "events" , "lifeCycle" , "params" , "if" , "transform" , ] ; const yamlFieldsInBrick = [ "permissionsPreCheck" , "transformFrom" ] ; const ignoredFieldsInBrick = [ "brick" , "template" , "portal" , "slots" , "id" , "children" , "instanceId" , ] ; export interface ReverseNormalizeContext { isPortalCanvas : boolean ; nodeData : { type : "brick" | "template" | "provider" ; parent : string ; mountPoint : string ; sort : number ; } ; } export function reverseNormalize ( brickConf , ctx ) { return Object . fromEntries ( Object . entries ( brickConf ) . map < [ string , unknown ] > ( ( [ key , value ] ) => value === undefined || ignoredFieldsInBrick . includes ( key ) ? undefined : jsonFieldsInBrick . includes ( key ) ? [ key , JSON . stringify ( value ) ] : yamlFieldsInBrick . includes ( key ) ? [ key , yaml . safeDump ( value ) ] : [ key , value ] ) . filter ( Boolean ) . concat ( Object . entries ( ctx . nodeData ) , [ [ "brick" , ctx . nodeData . type === "template" ? brickConf . template : brickConf . brick , ] , [ "portal" , ctx . isPortalCanvas || brickConf . portal ] , ] ) ) as SnippetNodeInstance ; }