const path = require('path');

const metaPath = path.resolve(__dirname, '../../packages/brands/fiee/brand.meta.js');
const meta = require(metaPath);
const metaDir = path.dirname(metaPath);

function resolveAsset(p) {
  if (!p || typeof p !== 'string') return p;
  if (path.isAbsolute(p)) return p;
  return path.resolve(metaDir, p);
}

function resolveSplash(splash) {
  if (!splash) return splash;
  const ios = splash.ios ? { ...splash.ios, image: resolveAsset(splash.ios.image) } : splash.ios;
  const android = splash.android ? { ...splash.android, image: resolveAsset(splash.android.image) } : splash.android;
  return { ...splash, ios, android };
}

const APP_VERSION = meta.version;
const versionCodeNumber = Number(APP_VERSION.split('.').join(''));
const buildNumberString = String(versionCodeNumber);

const WECHAT_APPID = process.env.EXPO_PUBLIC_WECHAT_APPID;
const WECHAT_UL = process.env.EXPO_PUBLIC_WECHAT_UNIVERSAL_LINK;
let associatedDomains = [];
try {
  if (WECHAT_UL) {
    const u = new URL(WECHAT_UL);
    if (u && u.host) associatedDomains = [`applinks:${u.host}`];
  }
} catch (_) {}

const isDev = ['dev', 'test'].includes(process.env.EXPO_PUBLIC_ENV);

module.exports = {
  expo: {
    name: meta.name,
    slug: meta.slug,
    version: APP_VERSION,
    runtimeVersion: APP_VERSION,
    platforms: ['android', 'ios'],
    updates: meta.updates || {},
    orientation: 'portrait',
    userInterfaceStyle: 'light',
    icon: resolveAsset(meta.icons && meta.icons.appIcon),
    scheme: [meta.scheme, WECHAT_APPID].filter(Boolean),
    splash: resolveSplash(meta.splash),
    ios: {
      bundleIdentifier: meta.ios && meta.ios.iosBundleId,
      buildNumber: buildNumberString,
      associatedDomains,
    },
    android: {
      package: meta.android && meta.android.androidPackage,
      versionCode: versionCodeNumber,
      permissions: meta.android && meta.android.permissions,
    },
    plugins: [
      'expo-router',
      '../../NativePlugins/disable-saved-state-plugin/app.plugin.js',
      ['expo-updates'],
      [
        'expo-location',
        {
          locationAlwaysAndWhenInUsePermission:
            '我们会在您进行考勤打卡时使用位置信息，用于记录打卡地点并校验是否在允许范围内。例如：上班/下班打卡时记录当前位置。'
        }
      ],
      [
        'expo-splash-screen',
        {
          ios: {
            image: resolveAsset(meta.ios && meta.ios.splashScreen && meta.ios.splashScreen.image),
            resizeMode: 'cover',
            enableFullScreenImage_legacy: true,
          },
          android: {
            image: resolveAsset(meta.android && meta.android.splashScreen && meta.android.splashScreen.image),
            resizeMode: 'cover',
          }
        }
      ],
      [
        'expo-image-picker',
        {
          photosPermission: '需要访问照片库以便选择图片或视频进行上传或分享。'
        }
      ],
      [
        'expo-build-properties',
        {
          ios: {
            deploymentTarget: '15.5'
          },
          android: {
            usesCleartextTraffic: isDev
          }
        }
      ],
      ['../../NativePlugins/jpush-expo-plugin/app.plugin.js', {}],
      [
        '../../NativePlugins/manifest-permission-cleaner/app.plugin.js',
        {
          removePermissions: [
            'android.permission.ACCESS_BACKGROUND_LOCATION',
            'android.permission.QUERY_ALL_PACKAGES',
            'android.permission.GET_TASKS'
          ]
        }
      ],
      ['../../NativePlugins/notification-forwarder-plugin/app.plugin.js', {}],
      ['../../NativePlugins/signing-config-plugin/app.plugin.js', {}],
      'expo-wechat',
      ['../../NativePlugins/baidu-location-react-native/plugin/app.plugin.js', {}]
    ],
    extra: {
      BRAND: 'fiee'
    }
  }
};
