import * as T from "./types" ; import { assertUnreachable , flat } from "./utils" ; export function canHaveChildren ( layer ) { return layer . type === "container" || layer . type === "link" ; } export function layerTypeToName ( type ) { switch ( type ) { case "text" : return "text" ; case "link" : return "link" ; case "container" : return "container" ; case "image" : return "image" ; case "component" : return "component" ; } assertUnreachable ( type ) ; } export function findLayerById ( root , id ) { if ( root . id === id ) { return root ; } if ( canHaveChildren ( root ) ) { for ( const child of root . children ) { const layer = findLayerById ( child , id ) ; if ( layer ) { return layer ; } } } return ; } export function findLayerByIdWithParent ( root , id ) { if ( root . id === id ) { return { layer : root , parent : undefined , } ; } if ( canHaveChildren ( root ) ) { for ( const child of root . children ) { const result = findLayerByIdWithParent ( child , id ) ; if ( result . layer ) { if ( result . parent ) { return result ; } return { layer : result . layer , parent : root , } ; } } } return { layer : undefined , parent : undefined , } ; } export function isResizable ( layer ? ) { return layer && layer . type !== "component" ; } export function updateLayer ( rootLayer , newLayer ) { if ( ! rootLayer ) { return newLayer ; } if ( rootLayer . id === newLayer . id ) { return newLayer ; } if ( canHaveChildren ( rootLayer ) ) { return { ... rootLayer , children : rootLayer . children . map ( ( child ) => updateLayer ( child , newLayer ) ) , } ; } return rootLayer ; } export function layerTreeToArray ( root ) { if ( ! root ) { return [ ] ; } const result = [ root ] ; if ( root . type === "container" ) { return result . concat ( flat ( root . children . map ( layerTreeToArray ) ) ) ; } return result ; } export function isLayerUsingRef ( layer , refId , isUsingRef ) { return [ layer . style , ... layer . mediaQueries . map ( ( mq ) => mq . style ) , ] . some ( ( style ) => isUsingRef ( style , refId ) ) ; } export function getComponentOrThrow ( componentId , refs ) { const component = refs . components . get ( componentId ) ; if ( component == null ) { throw new Error ( `Component with id: "${ componentId }" not found` ) ; } return component ; } export function isComponentLayer ( layer ) { return layer . type === "component" ; }