import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import paletteRaw from '@atlaskit/tokens/palettes-raw';
import tokenNames from '@atlaskit/tokens/token-names';
import { dark as darkTokensRaw, light as lightTokensRaw } from '@atlaskit/tokens/tokens-raw';
import { getSelectedRowAndColumn } from './getSelectedRowAndColumn';
export var DEFAULT_COLOR_PICKER_COLUMNS = 7;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var CSS_VAR_EXPRESSION_REGEX = /var\(([^,\)]+)(,.*)?\)/;
var getCSSVariableParts = function getCSSVariableParts(variableExpression) {
  var matcher = variableExpression.match(CSS_VAR_EXPRESSION_REGEX);
  if (!matcher) {
    return undefined;
  }
  return {
    fallback: matcher[2] ? matcher[2].replace(',', '').trim() : '',
    variable: matcher[1].trim()
  };
};

// Build the reverse lookup (CSS variable -> token name) once at module load so that
// each lookup is O(1) instead of O(n) with an extra array allocation per call.
var cssVariableToTokenName = new Map(Object.entries(tokenNames).map(function (_ref) {
  var _ref2 = _slicedToArray(_ref, 2),
    tokenName = _ref2[0],
    cssVariable = _ref2[1];
  return [cssVariable, tokenName];
}));
var getTokenName = function getTokenName(cssVariable) {
  return cssVariableToTokenName.get(cssVariable);
};
var resolveRawTokenValue = function resolveRawTokenValue(value) {
  var _paletteRaw$find;
  if (typeof value !== 'string') {
    return undefined;
  }
  var paletteValue = (_paletteRaw$find = paletteRaw.find(function (paletteToken) {
    return paletteToken.path[paletteToken.path.length - 1] === value;
  })) === null || _paletteRaw$find === void 0 ? void 0 : _paletteRaw$find.value;
  return typeof paletteValue === 'string' ? paletteValue : value;
};
var getCurrentThemeMode = function getCurrentThemeMode() {
  if (typeof document === 'undefined') {
    return 'light';
  }
  return document.documentElement.getAttribute('data-color-mode') === 'dark' ? 'dark' : 'light';
};
var getNonActiveThemeMode = function getNonActiveThemeMode() {
  return getCurrentThemeMode() === 'dark' ? 'light' : 'dark';
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export 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;
  }, []);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export 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);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var getTokenCSSVariableValue = function getTokenCSSVariableValue(variableExpression) {
  var matcher = variableExpression.match(CSS_VAR_EXPRESSION_REGEX);
  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 '';
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var getTokenCSSVariableValueForNonActiveTheme = function getTokenCSSVariableValueForNonActiveTheme(variableExpression, fallback) {
  var cssVariableParts = getCSSVariableParts(variableExpression);
  var tokenName = cssVariableParts ? getTokenName(cssVariableParts.variable) : undefined;
  var rawToken = (getNonActiveThemeMode() === 'light' ? lightTokensRaw : darkTokensRaw).find(function (token) {
    return token.cleanName === tokenName;
  });
  if (rawToken) {
    return resolveRawTokenValue(rawToken.value) || fallback;
  }
  return (cssVariableParts === null || cssVariableParts === void 0 ? void 0 : cssVariableParts.fallback) || fallback;
};

// eslint-disable-next-line @atlaskit/editor/no-re-export
export { getSelectedRowAndColumn } from './getSelectedRowAndColumn';