import { mergeAll } from 'ramda';
import { Platform } from 'react-native';

export type AppPlatform = 'android' | 'ios';

/**
 * Returns true if the current platform is the platform in the parameter
 * @param platform Platform to check
 */
function isPlatform(platform: AppPlatform): boolean {
  return Platform.OS === platform;
}

/**
 * Returns true if the current platform is Android
 */
function isAndroid(): boolean {
  return isPlatform('android');
}

/**
 * Returns true if the current platform is iOS
 */
function isIos(): boolean {
  return isPlatform('ios');
}

/**
 * Returns the Android value if the platform is Android, and the ios value if the platform is iOS
 * @param android Value to return if is Android
 * @param platform Value to return if is iOS
 */
function select<T>(android: T, ios: T): T {
  return Platform.select({
    ios,
    android,
  });
}

/**
 * Returns the current platform
 */
function getOs(): AppPlatform {
  return Platform.OS as AppPlatform;
}

/**
 * If the current platform is the platform of the first parameter,
 * merges base and append and return that merge. If not, returns base.
 * @param platform Platform to check
 * @param base Base object
 * @param append Object to append
 */
function merge<T extends object>(platform: AppPlatform, base: Partial<T>, append: Partial<T>): T {
  return (isPlatform(platform) ? mergeAll([base, append]) : base) as T;
}

export default { isPlatform, isAndroid, isIos, select, getOs, merge };
