/* @flow */

import { NativeModules } from 'react-native';
import resolveAssetSource
  from 'react-native/Libraries/Image/resolveAssetSource';
import isEqual from 'lodash/isEqual';
import Background from './Background';
import Text from './Text';
import type {
  Image,
  PaletteInstance,
  Options,
  ColorProfile,
  Swatch,
} from './types';

const nullSwatch = {
  population: 0,
  color: '#000000',
  bodyTextColor: '#000000',
  titleTextColor: '#000000',
};

const doesSwatchExist = (swatch: Swatch): boolean =>
  !isEqual(swatch, nullSwatch);

function getColor(
  color: ColorProfile,
  defaultColor: string = '#FFFFFF',
): Promise<string> {
  return NativeModules.MaterialPalette.getColor(color, defaultColor);
}

async function getSwatch(color: ColorProfile): ?Promise<Swatch> {
  const swatch = await NativeModules.MaterialPalette.getSwatch(color);
  // $FlowFixMe
  return doesSwatchExist(swatch) ? Promise.resolve(swatch) : null;
}

/** API */
export default class MaterialPalette {
  static async create(image: Image): Promise<PaletteInstance> {
    const source = resolveAssetSource(image);
    await NativeModules.MaterialPalette.createMaterialPalette(source);
    // $FlowFixMe
    return Promise.resolve({
      getColor,
      getSwatch,
    });
  }

  static Background: Class<React$Component<void, Options, void>> = Background;
  static Text: Class<React$Component<void, Options, void>> = Text;
}

@@ -762,17 +762,16 @@
 ofile): 
-?
 Promise%3C
@@ -780,16 +780,16 @@
 atch%3E %7B%0A
+
   const 
@@ -851,32 +851,16 @@
 color);%0A
-  // $FlowFixMe%0A
   return
@@ -890,38 +890,27 @@
 ) ? 
-Promise.resolve(
 swatch
-)
  : null
+Swatch
 ;%0A%7D%0A
@@ -1149,49 +1149,15 @@
     
-// $FlowFixMe%0A    return Promise.resolve(
+return 
 %7B%0A  
@@ -1191,14 +1191,13 @@
 ch,%0A
+
     %7D
-)
 ;%0A  

Fix flow issues