import { clamp, floatFromPercent } from "../ColorHelpers";
import { ColorFactory } from "../ColorFactory";
var DEFAULT_DARK = ColorFactory.create('#333333');
var DEFAULT_LIGHT = ColorFactory.create('#fff');
var ColorFunctionsCache = Object.create(null);
var buildCacheKey = function buildCacheKey(name) {
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    args[_key - 1] = arguments[_key];
  }
  return name + "(" + args.join() + ")";
};
var shiftColor = function shiftColor(colorString, a, sign, method) {
  if (!colorString) {
    return '';
  }
  if (colorString.toLowerCase() === 'transparent') {
    return 'transparent';
  }
  var amount;
  if (typeof a === 'string') {
    amount = a.endsWith('%') ? floatFromPercent(a) : parseFloat(a);
  } else {
    amount = a;
  }
  amount = clamp(amount, 1);
  var color = ColorFactory.create(colorString);
  var hsl = color.toHSL();
  if (sign === '+') {
    hsl.l += method === 'relative' ? hsl.l * amount : amount;
  } else {
    hsl.l -= method === 'relative' ? hsl.l * amount : amount;
  }
  hsl.l = clamp(hsl.l);
  var newColor;
  if (color.alpha < 1) {
    newColor = ColorFactory.create("hsla(" + hsl.h + ", " + hsl.s + ", " + hsl.l + ", " + hsl.a + ")");
  } else {
    newColor = ColorFactory.create("hsl(" + hsl.h + ", " + hsl.s + ", " + hsl.l + ")");
  }
  return newColor.toColorString(color.type);
};
export function lighten(colorString, amount, method) {
  var key = buildCacheKey('lighten', colorString, amount, method);
  if (ColorFunctionsCache[key] === undefined) {
    ColorFunctionsCache[key] = shiftColor(colorString, amount, '+', method);
    return ColorFunctionsCache[key];
  }
  return ColorFunctionsCache[key];
}
export function darken(colorString, amount, method) {
  var key = buildCacheKey('darken', colorString, amount, method);
  if (ColorFunctionsCache[key] === undefined) {
    ColorFunctionsCache[key] = shiftColor(colorString, amount, '-', method);
    return ColorFunctionsCache[key];
  }
  return ColorFunctionsCache[key];
}
export function contrast(colorString, darkString, lightString, threshold) {
  if (threshold === void 0) {
    threshold = 0.43;
  }
  var key = buildCacheKey('contrast', colorString, darkString, lightString, threshold);
  if (!colorString) {
    ColorFunctionsCache[key] = '';
  }
  if (ColorFunctionsCache[key] === undefined) {
    var color = ColorFactory.create(colorString);
    var dark = typeof darkString === 'undefined' ? DEFAULT_DARK : ColorFactory.create(darkString);
    var light = typeof lightString === 'undefined' ? DEFAULT_LIGHT : ColorFactory.create(lightString);

    // Figure out which is actually light and dark:
    if (dark.luma() > light.luma()) {
      var _ref = [light, dark];
      dark = _ref[0];
      light = _ref[1];
    }
    if (color.luma() < threshold) {
      ColorFunctionsCache[key] = light.alpha < 1 ? light.toRGBString() : light.toHEXString();
    } else {
      ColorFunctionsCache[key] = dark.alpha < 1 ? dark.toRGBString() : dark.toHEXString();
    }
  }
  return ColorFunctionsCache[key];
}
export var red = function red(colorString) {
  var color = ColorFactory.create(colorString);
  return color.rgb[0];
};
export var green = function green(colorString) {
  var color = ColorFactory.create(colorString);
  return color.rgb[1];
};
export var blue = function blue(colorString) {
  var color = ColorFactory.create(colorString);
  return color.rgb[2];
};
export var alpha = function alpha(colorString) {
  var color = ColorFactory.create(colorString);
  return color.alpha;
};
export var isValid = function isValid(colorString) {
  try {
    ColorFactory.create(colorString);
    return true;
  } catch (e) {
    return false;
  }
};
export var fade = function fade(colorString, alpha) {
  var key = buildCacheKey('fade', colorString, alpha);
  if (ColorFunctionsCache[key] === undefined) {
    var color = ColorFactory.create(colorString).clone();
    color.alpha = alpha;
    ColorFunctionsCache[key] = color.toColorString(color.type === 'hex' ? 'rgba' : color.type);
  }
  return ColorFunctionsCache[key];
};