'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var color_helper = require('./helper.js');
var color_transform = require('./transform.js');

function commonParseRGBOrHSL(_ref) {
  var value = _ref.value,
    _ref$reg = _ref.reg,
    reg = _ref$reg === void 0 ? /hsla|hsl|\(|\)/gm : _ref$reg,
    _ref$transformFunc = _ref.transformFunc,
    transformFunc = _ref$transformFunc === void 0 ? color_transform.hsl2hsv : _ref$transformFunc;
  var alpha = 100;
  var h = 0;
  var s = 100;
  var v = 100;
  var parts = value.replace(reg, '').split(/\s|,/g).filter(function (val) {
    return val !== '';
  }).map(function (val, index) {
    return index > 2 ? parseFloat(val) : parseInt(val, 10);
  });
  if (parts.length === 4) {
    alpha = Math.floor(parseFloat("".concat(parts[3])) * 100);
  } else if (parts.length === 3) {
    alpha = 100;
  }
  if (parts.length >= 3) {
    var res = transformFunc(parts[0], parts[1], parts[2]);
    h = res.h;
    s = res.s;
    v = res.v;
    // fromHSV(h, s, v);
  }
  return {
    h: h,
    s: s,
    v: v,
    alpha: alpha
  };
}
function parseHSLString(value) {
  return commonParseRGBOrHSL({
    value: value,
    reg: /hsla|hsl|\(|\)/gm,
    transformFunc: color_transform.hsl2hsv
  });
}
function parseHSVString(value) {
  var h = 0;
  var s = 100;
  var v = 100;
  var alpha = 100;
  var parts = value.replace(/hsva|hsv|\(|\)/gm, '').split(/\s|,/g).filter(function (val) {
    return val !== '';
  }).map(function (val, index) {
    return index > 2 ? parseFloat(val) : parseInt(val, 10);
  });
  if (parts.length === 4) {
    alpha = Math.floor(parseFloat("".concat(parts[3])) * 100);
  } else if (parts.length === 3) {
    alpha = 100;
  }
  h = parts[0];
  s = parts[1];
  v = parts[2];
  return {
    h: h,
    s: s,
    v: v,
    alpha: alpha
  };
}
function parseRGBBracket(value) {
  return commonParseRGBOrHSL({
    value: value,
    reg: /rgba|rgb|\(|\)/gm,
    transformFunc: color_transform.rgb2hsv
  });
}
function parseRGBHex(value) {
  var hex = value.replace('#', '').trim();
  var r = 0;
  var g = 0;
  var b = 0;
  var alpha = 0;
  if (!/^(?:[0-9a-fA-F]{3}){1,2}|[0-9a-fA-F]{8}$/.test(hex)) {
    return {
      r: r,
      g: g,
      b: b,
      alpha: alpha
    };
  }
  if (hex.length === 3) {
    r = color_helper.parseHexChannel(hex[0] + hex[0]);
    g = color_helper.parseHexChannel(hex[1] + hex[1]);
    b = color_helper.parseHexChannel(hex[2] + hex[2]);
  } else if (hex.length === 6 || hex.length === 8) {
    r = color_helper.parseHexChannel(hex.substring(0, 2));
    g = color_helper.parseHexChannel(hex.substring(2, 4));
    b = color_helper.parseHexChannel(hex.substring(4, 6));
  }
  if (hex.length === 8) {
    alpha = Math.floor(color_helper.parseHexChannel(hex.substring(6)) / 255 * 100);
  } else if (hex.length === 3 || hex.length === 6) {
    alpha = 100;
  }
  return {
    r: r,
    g: g,
    b: b,
    alpha: alpha
  };
}
function parseColorToHSV(value) {
  var alpha = 100;
  var h = 0;
  var s = 100;
  var v = 100;
  var fromHSV = function fromHSV(_h, _s, _v) {
    h = Math.max(0, Math.min(360, _h));
    s = Math.max(0, Math.min(100, _s));
    v = Math.max(0, Math.min(100, _v));
  };
  if (value.indexOf('hsl') !== -1) {
    var res = parseHSLString(value);
    h = res.h;
    s = res.s;
    v = res.v;
    alpha = res.alpha;
  } else if (value.indexOf('hsv') !== -1) {
    var _res = parseHSVString(value);
    h = _res.h;
    s = _res.s;
    v = _res.v;
    alpha = _res.alpha;
  } else if (value.indexOf('rgb') !== -1) {
    var _res2 = parseRGBBracket(value);
    h = _res2.h;
    s = _res2.s;
    v = _res2.v;
    alpha = _res2.alpha;
  } else if (value.indexOf('#') !== -1) {
    var rgb = parseRGBHex(value);
    var r = rgb.r,
      g = rgb.g,
      b = rgb.b;
    alpha = rgb.alpha;
    var _res3 = color_transform.rgb2hsv(r, g, b);
    h = _res3.h;
    s = _res3.s;
    v = _res3.v;
  }
  fromHSV(h, s, v);
  return {
    h: h,
    s: s,
    v: v,
    alpha: alpha
  };
}

exports.parseColorToHSV = parseColorToHSV;
exports.parseHSLString = parseHSLString;
exports.parseHSVString = parseHSVString;
exports.parseRGBBracket = parseRGBBracket;
exports.parseRGBHex = parseRGBHex;
