import Theme$1, { SharedUtils } from 'primevue/themes';
import Theme from 'primevue/themes/config';

function normalizeColor(color) {
  if (color.length === 4) {
    color = "#".concat(color[1]).concat(color[1]).concat(color[2]).concat(color[2]).concat(color[3]).concat(color[3]);
  }
  return color;
}
function hexToRgb(hex) {
  var bigint = parseInt(hex.substring(1), 16);
  var r = bigint >> 16 & 255;
  var g = bigint >> 8 & 255;
  var b = bigint & 255;
  return {
    r: r,
    g: g,
    b: b
  };
}
function rgbToHex(r, g, b) {
  return "#".concat(r.toString(16).padStart(2, '0')).concat(g.toString(16).padStart(2, '0')).concat(b.toString(16).padStart(2, '0'));
}
var mix = (function (color1, color2, weight) {
  color1 = normalizeColor(color1);
  color2 = normalizeColor(color2);
  var p = weight / 100;
  var w = p * 2 - 1;
  var w1 = (w + 1) / 2.0;
  var w2 = 1 - w1;
  var rgb1 = hexToRgb(color1);
  var rgb2 = hexToRgb(color2);
  var r = Math.round(rgb1.r * w1 + rgb2.r * w2);
  var g = Math.round(rgb1.g * w1 + rgb2.g * w2);
  var b = Math.round(rgb1.b * w1 + rgb2.b * w2);
  return rgbToHex(r, g, b);
});

var shade = (function (color, percent) {
  return mix('#000000', color, percent);
});

var tint = (function (color, percent) {
  return mix('#ffffff', color, percent);
});

var scales = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
var palette = (function (color) {
  if (/{([^}]*)}/g.test(color)) {
    var token = color.replace(/{|}/g, '');
    return scales.reduce(function (acc, scale) {
      return acc[scale] = "{".concat(token, ".").concat(scale, "}"), acc;
    }, {});
  }
  return typeof color === 'string' ? scales.reduce(function (acc, scale, i) {
    return acc[scale] = i <= 5 ? tint(color, (5 - i) * 19) : shade(color, (i - 5) * 15), acc;
  }, {}) : color;
});

var $dt = function $dt(tokenPath) {
  var _variable$match;
  var theme = Theme.getTheme();
  var variable = dtwt(theme, tokenPath, undefined, 'variable');
  var name = (_variable$match = variable.match(/--[\w-]+/g)) === null || _variable$match === void 0 ? void 0 : _variable$match[0];
  var value = dtwt(theme, tokenPath, undefined, 'value');
  return {
    name: name,
    variable: variable,
    value: value
  };
};
var dt = function dt() {
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }
  return dtwt.apply(void 0, [Theme.getTheme()].concat(args));
};
var dtwt = function dtwt() {
  var theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var tokenPath = arguments.length > 1 ? arguments[1] : undefined;
  var fallback = arguments.length > 2 ? arguments[2] : undefined;
  var type = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'variable';
  if (tokenPath) {
    var VARIABLE = Theme.defaults.variable;
    var _ref = (theme === null || theme === void 0 ? void 0 : theme.options) || {},
      prefix = _ref.prefix,
      transform = _ref.transform;
    var regex = /{([^}]*)}/g;
    var token = SharedUtils.object.test(regex, tokenPath) ? tokenPath : "{".concat(tokenPath, "}");
    var isStrictTransform = type === 'value' || transform === 'strict'; // @todo - TRANSFORM: strict | lenient(default)

    return isStrictTransform ? Theme.getTokenValue(tokenPath) : SharedUtils.object.getVariableValue(token, undefined, prefix, [VARIABLE.excludedKeyRegex], fallback);
  }
  return '';
};

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function toVariables (theme) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var VARIABLE = Theme$1.defaults.variable;
  var _options$prefix = options.prefix,
    prefix = _options$prefix === void 0 ? VARIABLE.prefix : _options$prefix,
    _options$selector = options.selector,
    selector = _options$selector === void 0 ? VARIABLE.selector : _options$selector,
    _options$excludedKeyR = options.excludedKeyRegex,
    excludedKeyRegex = _options$excludedKeyR === void 0 ? VARIABLE.excludedKeyRegex : _options$excludedKeyR;
  var _toVariables = function _toVariables(_theme) {
    var _prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    return Object.entries(_theme).reduce(function (acc, _ref) {
      var _ref2 = _slicedToArray(_ref, 2),
        key = _ref2[0],
        value = _ref2[1];
      var px = SharedUtils.object.test(excludedKeyRegex, key) ? SharedUtils.object.toNormalizeVariable(_prefix) : SharedUtils.object.toNormalizeVariable(_prefix, SharedUtils.object.toKebabCase(key));
      var v = SharedUtils.object.toValue(value);
      if (SharedUtils.object.isObject(v)) {
        var variables = _toVariables(v, px);
        SharedUtils.object.merge(acc, variables);
      } else {
        SharedUtils.object.setProperty(acc, SharedUtils.object.getVariableName(px), SharedUtils.object.getVariableValue(v, px, prefix, [excludedKeyRegex]));
      }
      return acc;
    }, []);
  };
  var value = _toVariables(theme, prefix);
  return {
    value: value,
    declarations: value.join(''),
    css: SharedUtils.object.getRule(selector, value.join(''))
  };
}

export { $dt, dt, dtwt, mix, palette, shade, tint, toVariables };
