import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _typeof from "@babel/runtime/helpers/typeof";
import allColors from "../Provider/colors";

var undefineColorWarning = function undefineColorWarning(message) {
  if (process.env.NODE_ENV === "development") {
    // eslint-disable-next-line no-console
    console.warn("[Clever-react]: ".concat(message));
  }
};

var addProxyColor = function addProxyColor(colorObj, keyName) {
  var handler = {
    get: function get(target, name) {
      if (_typeof(name) === "symbol") {
        return target[name];
      }

      if (!(name in target)) {
        undefineColorWarning("".concat(name, " is not available in ").concat(keyName));
      }

      switch (name) {
        case "darker":
          return target[900];

        case "dark":
          return target[700];

        case "main":
          return target[500];

        case "light":
          return target[300];

        case "lighter":
          return target[50];

        default:
          return target[name];
      }
    }
  };
  return new Proxy(colorObj, handler);
};

export function getColors() {
  var colors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : allColors;
  var level = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var flattened = {};
  Object.keys(colors).forEach(function (key) {
    if (_typeof(colors[key]) === "object" && colors[key] !== null && level < 1) {
      Object.assign(flattened, getColors(colors[key], 1));
    } else {
      flattened[key] = addProxyColor(colors[key], key);
    }
  });
  return flattened;
}
export function getColorsLabels() {
  var inherit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var colors = Object.keys(getColors());
  if (inherit) colors.push("inherit");
  return colors;
}
export function getShades() {
  return [50, 100, 200, 300, 400, 500, 600, 700, 800, 900];
}
export function hex2rgb(hex) {
  return hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function (m, r, g, b) {
    return "#".concat(r).concat(r).concat(g).concat(g).concat(b).concat(b);
  }).substring(1).match(/.{2}/g).map(function (x) {
    return parseInt(x, 16);
  });
}
export function hex2rgba(hex) {
  var rgb = hex2rgb(hex);
  var min = Math.min.apply(Math, _toConsumableArray(rgb));
  var alpha = (255 - min) / 255;
  var r = (rgb[0] - min) / alpha,
      g = (rgb[1] - min) / alpha,
      b = (rgb[2] - min) / alpha;
  return [r, g, b, alpha];
}
export function cssStringHex2rgba(hex) {
  var _hex2rgba = hex2rgba(hex),
      _hex2rgba2 = _slicedToArray(_hex2rgba, 4),
      r = _hex2rgba2[0],
      g = _hex2rgba2[1],
      b = _hex2rgba2[2],
      a = _hex2rgba2[3];

  return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(a.toFixed(3), ")");
}