"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DEFAULT_COLOR_PICKER_COLUMNS = void 0;
exports.getColorsPerRowFromPalette = getColorsPerRowFromPalette;
exports.getSelectedRowAndColumn = getSelectedRowAndColumn;
exports.getSelectedRowAndColumnFromPalette = getSelectedRowAndColumnFromPalette;
exports.getTokenCSSVariableValue = void 0;
var DEFAULT_COLOR_PICKER_COLUMNS = exports.DEFAULT_COLOR_PICKER_COLUMNS = 7;
function getColorsPerRowFromPalette(palette) {
  var cols = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_COLOR_PICKER_COLUMNS;
  return palette.reduce(function (resultArray, item, index) {
    var chunkIndex = Math.floor(index / cols);
    resultArray[chunkIndex] = resultArray[chunkIndex] || []; // start a new chunk
    resultArray[chunkIndex].push(item);
    return resultArray;
  }, []);
}
function getSelectedRowAndColumn(colorsPerRow, selectedColor) {
  var selectedRowIndex = -1;
  var selectedColumnIndex = -1;
  colorsPerRow.forEach(function (row, rowIndex) {
    row.forEach(function (_ref, columnIndex) {
      var value = _ref.value;
      if (value === selectedColor) {
        selectedRowIndex = rowIndex;
        selectedColumnIndex = columnIndex;
      }
    });
  });
  return {
    selectedRowIndex: selectedRowIndex,
    selectedColumnIndex: selectedColumnIndex
  };
}
function getSelectedRowAndColumnFromPalette(palette, selectedColor) {
  var cols = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_COLOR_PICKER_COLUMNS;
  var colorsPerRow = getColorsPerRowFromPalette(palette, cols);
  return getSelectedRowAndColumn(colorsPerRow, selectedColor);
}
var getTokenCSSVariableValue = exports.getTokenCSSVariableValue = function getTokenCSSVariableValue(variableExpression) {
  var matcher = variableExpression.match(/var\(([^,\)]+)(,.*)?\)/);
  if (matcher) {
    var variable = matcher[1].trim();
    var fallback = matcher[2] ? matcher[2].replace(',', '').trim() : '';
    if (typeof document === 'undefined') {
      return fallback;
    }
    var value = window.getComputedStyle(document.documentElement).getPropertyValue(variable).trim();
    return value || fallback;
  }
  return '';
};