// @ts-check
/**
 * Expo config plugin for the MapmapNav local module (T1, iOS).
 *
 * WHY THIS EXISTS
 * ---------------
 * The MapmapNav pod is now fully self-contained: the MapMap iOS SDK
 * (MapMapKit / MapMapValhalla) and the valhalla-mobile router are vendored
 * into the pod as sources + binaries (see
 * modules/mapmap-react-native/ios/Vendored). So this plugin NO LONGER injects
 * any Swift Package Manager package into the app project — that injection is
 * both redundant and a source of duplicate symbols now the code is vendored.
 *
 * What remains: it writes the Info.plist keys the SDK's `CoreLocationProvider`
 * and `VoiceGuidance` require (the SDK deliberately declares none of its own),
 * and enables the `location` + `audio` background modes for turn-by-turn
 * guidance that keeps running with the screen off.
 *
 * Registered by the app (T9 / integration) in app.json:
 *   "plugins": [ ["./modules/mapmap-react-native/app.plugin.js", { ... }] ]
 */

const { withInfoPlist } = require('@expo/config-plugins');

/**
 * @typedef {object} MapMapNavProps
 * @property {string} [locationWhenInUsePermission]
 * @property {string} [locationAlwaysPermission]
 */

/**
 * Info.plist keys the SDK needs but does not itself declare.
 * @type {import('@expo/config-plugins').ConfigPlugin<MapMapNavProps | void>}
 */
const withMapMapNavInfoPlist = (config, props) =>
  withInfoPlist(config, (cfg) => {
    /** @type {MapMapNavProps} */
    const p = props || {};
    const plist = cfg.modResults;

    plist.NSLocationWhenInUseUsageDescription =
      plist.NSLocationWhenInUseUsageDescription ||
      p.locationWhenInUsePermission ||
      'MapMap Nav uses your location to guide you turn-by-turn.';
    plist.NSLocationAlwaysAndWhenInUseUsageDescription =
      plist.NSLocationAlwaysAndWhenInUseUsageDescription ||
      p.locationAlwaysPermission ||
      'MapMap Nav uses your location to keep guiding you with the screen off.';

    const modes = new Set(plist.UIBackgroundModes || []);
    modes.add('location');
    modes.add('audio'); // spoken guidance continues in the background
    plist.UIBackgroundModes = Array.from(modes);
    return cfg;
  });

/**
 * @type {import('@expo/config-plugins').ConfigPlugin<MapMapNavProps | void>}
 */
const withMapMapNav = (config, props) => withMapMapNavInfoPlist(config, props);

module.exports = withMapMapNav;
