const { withInfoPlist, withEntitlementsPlist, createRunOncePlugin } = require('@expo/config-plugins');
const withLiveActivityNative = require('./withLiveActivityNative');
const withLiveActivityTarget = require('./withLiveActivityTarget');

const pkg = require('../package.json');

const withLiveActivities = (config) => {
  // Info.plist: 开启 Supports Live Activities
  config = withInfoPlist(config, (config) => {
    config.modResults.UIBackgroundModes = Array.from(new Set([...(config.modResults.UIBackgroundModes || []), 'processing']))
    config.modResults.NSSupportsLiveActivities = true;
    return config;
  });

  // entitlements: 添加 Live Activities 能力
  config = withEntitlementsPlist(config, (config) => {
    config.modResults['com.apple.developer.activitykit'] = true;
    // 如果未来需要推送更新 Live Activity，可启用下面 entitlement
    // config.modResults['com.apple.developer.usernotifications.time-sensitive'] = true;
    return config;
  });

  // 复制原生 Swift/ObjC 源码到 iOS 目录（保持 ios 目录无人为修改；构建期复制）
  config = withLiveActivityNative(config);

  // 自动创建 Widget Extension target
  config = withLiveActivityTarget(config);

  return config;
};

module.exports = createRunOncePlugin(withLiveActivities, pkg.name, pkg.version);