/**
 * @flow
 */

import { StyleSheet, Platform, Dimensions } from 'react-native'

export const screenWidth = Dimensions.get('window').width
export const rpx = screenWidth / 375
export const isPad = Boolean(Number(screenWidth) >= 640)

function create(styles: Object): {[name: string]: any} {
  function styleFor(name) {
    const { ios, android, iPad, androidPad, phone, pad, ...style } = { ...styles[name] }
    if (ios && Platform.OS === 'ios') {
      if (iPad && isPad) {
        return { ...style, ...ios, ...iPad }
      }
      return { ...style, ...ios }
    } else if (android && Platform.OS === 'android') {
      if (androidPad && isPad) {
        return { ...style, ...android, ...androidPad }
      }
      return { ...style, ...android }
    } else if (phone && !isPad) {
      return { ...style, ...phone }
    } else if (pad && isPad) {
      return { ...style, ...pad }
    }
    return style
  }
  const result: Object = Object.keys(styles).map(key => ({ [key]: styleFor(key) }))
    .reduce((loop, next) => ({
      ...loop,
      ...next,
    }), {})
  return StyleSheet.create(result)
}

export default {
  ...StyleSheet,
  create,
}
