import { readXMLAsync , XMLObject } from '../utils/XML' ; export type ResourceGroupXML = { $ : { name : string ; parent : string ; } ; item : ResourceItemXML [ ] ; } ; export type ResourceXML = { resources : { $ ? : { 'xmlns:tools' ? : string ; } ; color ? : ResourceItemXML [ ] ; string ? : ResourceItemXML [ ] ; style ? : ResourceGroupXML [ ] ; } ; } ; export type ResourceItemXML = { _ : string ; $ : { name : string ; 'tools:targetApi' ? : string ; translatable ? : string ; } ; } ; /**
 * Name of the resource folder.
 */ export type ResourceKind = 'values' | 'values-night' | 'values-v23' | 'values-night-v23' | 'drawable' ; const fallbackResourceString = `<?xml version="1.0" encoding="utf-8"?><resources></resources>` ; param options export async function readResourcesXMLAsync ( { path , fallback = fallbackResourceString , } ) { const xml = await readXMLAsync ( { path , fallback } ) ; if ( ! xml . resources ) { xml . resources = { } ; } return xml as ResourceXML ; } param xml export function ensureDefaultResourceXML ( xml ) { if ( ! xml ) { xml = { resources : { } } ; } if ( ! xml . resources ) { xml . resources = { } ; } return xml as ResourceXML ; } param props export function buildResourceItem ( { name , value , targetApi , translatable , } ) { const item = { $ : { name } , _ : value } ; if ( targetApi ) { item . $ [ 'tools:targetApi' ] = targetApi ; } if ( translatable !== undefined ) { item . $ [ 'translatable' ] = String ( translatable ) ; } return item ; } export function buildResourceGroup ( parent ) { return { $ : { name : parent . name , parent : parent . parent } , item : parent . items ?? [ ] } ; } export function findResourceGroup ( xml , group ) { const app = xml ?. filter ?. ( ( { $ : head } ) => { let matches = head . name === group . name ; if ( group . parent != null && matches ) { matches = head . parent === group . parent ; } return matches ; } ) ?. [ 0 ] ; return app ?? null ; } param xml returns export function getResourceItemsAsObject ( xml ) { return xml . reduce ( ( prev , curr ) => ( { ... prev , [ curr . $ . name ] : curr . _ } ) , { } ) ; } param xml returns export function getObjectAsResourceItems ( obj ) { return Object . entries ( obj ) . map ( ( [ name , value ] ) => ( { $ : { name } , _ : value } ) ) ; } export function getObjectAsResourceGroup ( group ) { return { $ : { name : group . name , parent : group . parent } , item : getObjectAsResourceItems ( group . item ) } ; }