1import { ExpoConfig } from '@expo/config-types';
2
3import { createInfoPlistPluginWithPropertyGuard } from '../plugins/ios-plugins';
4import { InfoPlist, InterfaceOrientation } from './IosConfig.types';
5
6export const withOrientation = createInfoPlistPluginWithPropertyGuard(
7  setOrientation,
8  {
9    infoPlistProperty: 'UISupportedInterfaceOrientations',
10    expoConfigProperty: 'orientation',
11  },
12  'withOrientation'
13);
14
15export function getOrientation(config: Pick<ExpoConfig, 'orientation'>) {
16  return config.orientation ?? null;
17}
18
19export const PORTRAIT_ORIENTATIONS: InterfaceOrientation[] = [
20  'UIInterfaceOrientationPortrait',
21  'UIInterfaceOrientationPortraitUpsideDown',
22];
23
24export const LANDSCAPE_ORIENTATIONS: InterfaceOrientation[] = [
25  'UIInterfaceOrientationLandscapeLeft',
26  'UIInterfaceOrientationLandscapeRight',
27];
28
29function getUISupportedInterfaceOrientations(orientation: string | null): InterfaceOrientation[] {
30  if (orientation === 'portrait') {
31    return PORTRAIT_ORIENTATIONS;
32  } else if (orientation === 'landscape') {
33    return LANDSCAPE_ORIENTATIONS;
34  } else {
35    return [...PORTRAIT_ORIENTATIONS, ...LANDSCAPE_ORIENTATIONS];
36  }
37}
38
39export function setOrientation(
40  config: Pick<ExpoConfig, 'orientation'>,
41  infoPlist: InfoPlist
42): InfoPlist {
43  const orientation = getOrientation(config);
44
45  return {
46    ...infoPlist,
47    UISupportedInterfaceOrientations: getUISupportedInterfaceOrientations(orientation),
48  };
49}
50