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 const DEFAULT_COLOR_PICKER_COLUMNS = 7;
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const CSS_VAR_EXPRESSION_REGEX = /var\(([^,\)]+)(,.*)?\)/;
const getCSSVariableParts = variableExpression => {
  const 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.
const cssVariableToTokenName = new Map(Object.entries(tokenNames).map(([tokenName, cssVariable]) => [cssVariable, tokenName]));
const getTokenName = cssVariable => cssVariableToTokenName.get(cssVariable);
const resolveRawTokenValue = value => {
  var _paletteRaw$find;
  if (typeof value !== 'string') {
    return undefined;
  }
  const paletteValue = (_paletteRaw$find = paletteRaw.find(paletteToken => paletteToken.path[paletteToken.path.length - 1] === value)) === null || _paletteRaw$find === void 0 ? void 0 : _paletteRaw$find.value;
  return typeof paletteValue === 'string' ? paletteValue : value;
};
const getCurrentThemeMode = () => {
  if (typeof document === 'undefined') {
    return 'light';
  }
  return document.documentElement.getAttribute('data-color-mode') === 'dark' ? 'dark' : 'light';
};
const getNonActiveThemeMode = () => getCurrentThemeMode() === 'dark' ? 'light' : 'dark';

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export function getColorsPerRowFromPalette(palette, cols = DEFAULT_COLOR_PICKER_COLUMNS) {
  return palette.reduce((resultArray, item, index) => {
    const 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, cols = DEFAULT_COLOR_PICKER_COLUMNS) {
  const colorsPerRow = getColorsPerRowFromPalette(palette, cols);
  return getSelectedRowAndColumn(colorsPerRow, selectedColor);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export const getTokenCSSVariableValue = variableExpression => {
  const matcher = variableExpression.match(CSS_VAR_EXPRESSION_REGEX);
  if (matcher) {
    const variable = matcher[1].trim();
    const fallback = matcher[2] ? matcher[2].replace(',', '').trim() : '';
    if (typeof document === 'undefined') {
      return fallback;
    }
    const value = window.getComputedStyle(document.documentElement).getPropertyValue(variable).trim();
    return value || fallback;
  }
  return '';
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export const getTokenCSSVariableValueForNonActiveTheme = (variableExpression, fallback) => {
  const cssVariableParts = getCSSVariableParts(variableExpression);
  const tokenName = cssVariableParts ? getTokenName(cssVariableParts.variable) : undefined;
  const rawToken = (getNonActiveThemeMode() === 'light' ? lightTokensRaw : darkTokensRaw).find(token => 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';