"use strict";exports.__esModule = true;exports.blue = exports.alpha = void 0;exports.contrast = contrast;exports.darken = darken;exports.isValid = exports.green = exports.fade = void 0;exports.lighten = lighten;exports.red = void 0;var _ColorHelpers = require("./ColorHelpers");
var _ColorFactory = require("./ColorFactory");





var DEFAULT_DARK = _ColorFactory.ColorFactory.create('#333333');
var DEFAULT_LIGHT = _ColorFactory.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('%') ? (0, _ColorHelpers.floatFromPercent)(a) : parseFloat(a);
  } else {
    amount = a;
  }
  amount = (0, _ColorHelpers.clamp)(amount, 1);

  var color = _ColorFactory.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 = (0, _ColorHelpers.clamp)(hsl.l);

  var newColor;
  if (color.alpha < 1) {
    newColor = _ColorFactory.ColorFactory.create("hsla(" + hsl.h + ", " + hsl.s + ", " + hsl.l + ", " + hsl.a + ")");
  } else {
    newColor = _ColorFactory.ColorFactory.create("hsl(" + hsl.h + ", " + hsl.s + ", " + hsl.l + ")");
  }
  return newColor.toColorString(color.type);
};

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];
}
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];
}
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.ColorFactory.create(colorString);
    var dark = typeof darkString === 'undefined' ? DEFAULT_DARK : _ColorFactory.ColorFactory.create(darkString);
    var light = typeof lightString === 'undefined' ? DEFAULT_LIGHT : _ColorFactory.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];
}

var red = exports.red = function red(colorString) {
  var color = _ColorFactory.ColorFactory.create(colorString);
  return color.rgb[0];
};
var green = exports.green = function green(colorString) {
  var color = _ColorFactory.ColorFactory.create(colorString);
  return color.rgb[1];
};
var blue = exports.blue = function blue(colorString) {
  var color = _ColorFactory.ColorFactory.create(colorString);
  return color.rgb[2];
};
var alpha = exports.alpha = function alpha(colorString) {
  var color = _ColorFactory.ColorFactory.create(colorString);
  return color.alpha;
};
var isValid = exports.isValid = function isValid(colorString) {
  try {
    _ColorFactory.ColorFactory.create(colorString);
    return true;
  } catch (e) {
    return false;
  }
};
var fade = exports.fade = function fade(colorString, alpha) {
  var key = buildCacheKey('fade', colorString, alpha);

  if (ColorFunctionsCache[key] === undefined) {
    var color = _ColorFactory.ColorFactory.create(colorString).clone();
    color.alpha = alpha;
    ColorFunctionsCache[key] = color.toColorString(color.type === 'hex' ? 'rgba' : color.type);
  }
  return ColorFunctionsCache[key];
};