const { withAndroidManifest, withDangerousMod } = require('expo/config-plugins'); const fs = require('node:fs/promises'); const path = require('node:path'); const { environment } = require('../config/environment.cjs'); // Production keeps exact-host cleartext restrictions. Development supports runtime // private-IP selection, enforced by the app before probing, saving or using it. module.exports = (config, options) => { const env = environment(); const hosts = [...new Set(options?.hosts || [])]; const expected = [...new Set([env.apiUrl, env.updateOrigin].filter(s => s.startsWith('http:')).map(s => new URL(s).hostname))]; if (JSON.stringify(hosts) !== JSON.stringify(expected)) throw Error('Native network hosts must match the environment config'); config = withAndroidManifest(config, cfg => { const app = cfg.modResults.manifest.application[0]; app.$['android:networkSecurityConfig'] = '@xml/cmvr_network_security'; app.$['android:usesCleartextTraffic'] = 'false'; return cfg; }); return withDangerousMod(config, ['android', async cfg => { const dir = path.join(cfg.modRequest.platformProjectRoot, 'app/src/main/res/xml'); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(path.join(dir, 'cmvr_network_security.xml'), `${hosts.length ? `${hosts.map(h => `${h}`).join('')}` : ''}`); return cfg; }]); };