import { getResourceXMLPathAsync } from './Paths' ; import { buildResourceGroup , buildResourceItem , ensureDefaultResourceXML , findResourceGroup , getResourceItemsAsObject , readResourcesXMLAsync , ResourceGroupXML , ResourceItemXML , ResourceKind , ResourceXML , } from './Resources' ; const fallbackResourceString = `<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"></resources>` ; export async function readStylesXMLAsync ( { path , fallback = fallbackResourceString , } ) { return readResourcesXMLAsync ( { path , fallback } ) ; } export async function getProjectStylesXMLPathAsync ( projectRoot , { kind } = { } ) { return getResourceXMLPathAsync ( projectRoot , { kind , name : 'styles' } ) ; } function ensureDefaultStyleResourceXML ( xml ) { xml = ensureDefaultResourceXML ( xml ) ; if ( ! Array . isArray ( xml ?. resources ?. style ) ) { xml . resources . style = [ ] ; } return xml ; } export function getStyleParent ( xml , group ) { return findResourceGroup ( xml . resources . style , group ) ; } export function getStylesItem ( { name , xml , parent , } ) { xml = ensureDefaultStyleResourceXML ( xml ) ; const appTheme = getStyleParent ( xml , parent ) ; if ( ! appTheme ) { return null ; } if ( appTheme . item ) { const existingItem = appTheme . item . filter ( ( { $ : head } ) => head . name === name ) [ 0 ] ; if ( existingItem ) { return existingItem ; } } return null ; } export function setStylesItem ( { item , xml , parent , } ) { xml = ensureDefaultStyleResourceXML ( xml ) ; let appTheme = getStyleParent ( xml , parent ) ; if ( ! appTheme ) { appTheme = buildResourceGroup ( parent ) ; xml . resources ! . style ! . push ( appTheme ) ; } if ( appTheme . item ) { const existingItem = appTheme . item . filter ( ( { $ : head } ) => head . name === item . $ . name ) [ 0 ] ; if ( existingItem ) { existingItem . _ = item . _ ; existingItem . $ = item . $ ; } else { appTheme . item . push ( item ) ; } } else { appTheme . item = [ item ] ; } return xml ; } export function removeStylesItem ( { name , xml , parent , } ) { xml = ensureDefaultStyleResourceXML ( xml ) ; const appTheme = getStyleParent ( xml , parent ) ; if ( appTheme ?. item ) { const index = appTheme . item . findIndex ( ( { $ : head } ) => head . name === name ) ; if ( index > - 1 ) { appTheme . item . splice ( index , 1 ) ; } } return xml ; } export function getAppThemeLightNoActionBarGroup ( ) { return { name : 'AppTheme' , parent : 'Theme.AppCompat.Light.NoActionBar' } ; } export function assignStylesValue ( xml , { add , value , targetApi , name , parent , } ) { if ( add ) { return setStylesItem ( { xml , parent , item : buildResourceItem ( { name , targetApi , value } ) } ) ; } return removeStylesItem ( { xml , parent , name } ) ; } param xml returns export function getStylesGroupAsObject ( xml , group ) { const xmlGroup = getStyleParent ( xml , group ) ; return xmlGroup ?. item ? getResourceItemsAsObject ( xmlGroup . item ) : null ; }