import chroma from 'chroma-js';


const createObject = (value) => {
  /*
    NOTE that HSL support scaffolding is exists in this function.
    However this component does not yet support recieving an HSL format.
  */
  const colorObject =
    {
      hex: '',
      rgb: { r: '', g: '', b: '' },
      hsl: { h: '', s: '', l: '' }
    };

  /* hex codes */
  if (value.charAt(0) === '#') {
    colorObject.hex = value;
    /* create rest of the obj */

    const rgb = chroma(value).rgb();
    colorObject.rgb.r = rgb[0];
    colorObject.rgb.g = rgb[1];
    colorObject.rgb.b = rgb[2];

    const hsl = chroma(value).hsl();
    colorObject.hsl.h = hsl[0];
    colorObject.hsl.s = hsl[1];
    colorObject.hsl.l = hsl[2];

  /* rbg colors */
  } else if (value.substr(0, 3) === 'rgb') {
    const rgb = value.substring(4, (value.length - 1)).split(', ');
    colorObject.rgb.r = rgb[0];
    colorObject.rgb.g = rgb[1];
    colorObject.rgb.b = rgb[2];

    if (rgb[3]) {
      colorObject.rgb.a = rgb[3];

      const tempHex = chroma(parseInt(rgb[0], 10), parseInt(rgb[1], 10), parseInt(rgb[2], 10)).hex();

      const alphaHex = ((rgb[3] * 255).toFixed() / 1).toString(16);

      if (alphaHex < 10) {
        const prependedAlpha = `0${alphaHex}`;
        colorObject.hex = (chroma(tempHex) + prependedAlpha);
      } else {
        colorObject.hex = (chroma(tempHex) + alphaHex);
      }
    } else {
      colorObject.hex = chroma(parseInt(rgb[0], 10), parseInt(rgb[1], 10), parseInt(rgb[2], 10)).hex();
    }

    const hsl = chroma(colorObject.hex).hsl();
    colorObject.hsl.h = hsl[0];
    colorObject.hsl.s = hsl[1];
    colorObject.hsl.l = hsl[2];

  /* hsl colors */
  } else if (value.substr(0, 2) === 'hsl') {
    /* todo hsl */
  }

  return colorObject;
};

/* Gets the contrast ratio between two colors */
const getContrast = (color1, color2) => chroma.contrast(color1, color2);

/*
  ratio: contrast ratio returned by 'getContrast' above
  size: text size for the test. WCAG traditionally tests 14pt and 18pt font for 'normal' and 'large' respectively
  level: the level of the test (e.g 'A', 'AA', or 'AAA')
*/
const runWCAGTest = (ratio, size, level) => {
  switch (level) {
    /* fun fact, 'A' WCAG standard does not have a contrast ratio criteria. */
    case 'A':
      return 'PASS';

    case 'AA':
      if (size === 'large' && ratio > 3) {
        return 'PASS';
      } else if (size === 'normal' && ratio > 4.5) {
        return 'PASS';
      }
      return 'FAIL';

    case 'AAA':
      if (size === 'large' && ratio > 4.5) {
        return 'PASS';
      } else if (size === 'normal' && ratio > 7) {
        return 'PASS';
      }
      return 'FAIL';

    default:
      if (size === 'large' && ratio > 3) {
        return 'PASS';
      } else if (size === 'normal' && ratio > 4.5) {
        return 'PASS';
      }
      return 'FAIL';
  }
};


module.exports = { createObject, getContrast, runWCAGTest };

@@ -454,45 +454,9 @@
     
-const rgb = chroma(value).rgb();%0A    
+%5B
 colo
@@ -464,38 +464,25 @@
 Object.rgb.r
- = rgb%5B0%5D;%0A   
+,
  colorObject
@@ -483,38 +483,25 @@
 Object.rgb.g
- = rgb%5B1%5D;%0A   
+,
  colorObject
@@ -510,33 +510,9 @@
 gb.b
- = rgb%5B2%5D;%0A%0A    const hsl
+%5D
  = c
@@ -524,27 +524,27 @@
 (value).
-hsl
+rgb
 ();%0A    
 colorObj
@@ -527,32 +527,33 @@
 lue).rgb();%0A    
+%5B
 colorObject.hsl.
@@ -549,38 +549,25 @@
 Object.hsl.h
- = hsl%5B0%5D;%0A   
+,
  colorObject
@@ -568,38 +568,25 @@
 Object.hsl.s
- = hsl%5B1%5D;%0A   
+,
  colorObject
@@ -587,33 +587,47 @@
 Object.hsl.l
+%5D
  = 
-hsl%5B2%5D
+chroma(value).hsl()
 ;%0A%0A  /* rbg 
@@ -749,24 +749,25 @@
 (', ');%0A    
+%5B
 colorObject.
@@ -775,22 +775,9 @@
 gb.r
- = rgb%5B0%5D;%0A   
+,
  col
@@ -794,22 +794,9 @@
 gb.g
- = rgb%5B1%5D;%0A   
+,
  col
@@ -813,17 +813,15 @@
 gb.b
+%5D
  = rgb
-%5B2%5D
 ;%0A%0A 
@@ -835,32 +835,33 @@
 rgb%5B3%5D) %7B%0A      
+%5B
 colorObject.rgb.
@@ -865,17 +865,20 @@
 gb.a
+%5D
  = 
+%5B
 rgb%5B3%5D
+%5D
 ;%0A%0A 
@@ -1395,54 +1395,27 @@
     
-const hsl = chroma(colorObject.hex).hsl();%0A   
+%5BcolorObject.hsl.h,
  col
@@ -1431,23 +1431,10 @@
 hsl.
-h = hsl%5B0%5D;%0A   
+s,
  col
@@ -1450,24 +1450,20 @@
 hsl.
-s = hsl%5B1%5D;%0A    
+l%5D = chroma(
 colo
@@ -1475,24 +1475,22 @@
 ct.h
-sl.l = hsl%5B2%5D
+ex).hsl()
 ;%0A%0A
+  
   /*

resolve "prefer-destructuring" linting error