"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DEFAULT_COLOR_PICKER_COLUMNS = void 0;
exports.getColorsPerRowFromPalette = getColorsPerRowFromPalette;
Object.defineProperty(exports, "getSelectedRowAndColumn", {
  enumerable: true,
  get: function get() {
    return _getSelectedRowAndColumn.getSelectedRowAndColumn;
  }
});
exports.getSelectedRowAndColumnFromPalette = getSelectedRowAndColumnFromPalette;
exports.getTokenCSSVariableValueForNonActiveTheme = exports.getTokenCSSVariableValue = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _palettesRaw = _interopRequireDefault(require("@atlaskit/tokens/palettes-raw"));
var _tokenNames = _interopRequireDefault(require("@atlaskit/tokens/token-names"));
var _tokensRaw = require("@atlaskit/tokens/tokens-raw");
var _getSelectedRowAndColumn = require("./getSelectedRowAndColumn");
var DEFAULT_COLOR_PICKER_COLUMNS = exports.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.default).map(function (_ref) {
  var _ref2 = (0, _slicedToArray2.default)(_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 = _palettesRaw.default.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
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
function getSelectedRowAndColumnFromPalette(palette, selectedColor) {
  var cols = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_COLOR_PICKER_COLUMNS;
  var colorsPerRow = getColorsPerRowFromPalette(palette, cols);
  return (0, _getSelectedRowAndColumn.getSelectedRowAndColumn)(colorsPerRow, selectedColor);
}

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
var getTokenCSSVariableValue = exports.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
var getTokenCSSVariableValueForNonActiveTheme = exports.getTokenCSSVariableValueForNonActiveTheme = function getTokenCSSVariableValueForNonActiveTheme(variableExpression, fallback) {
  var cssVariableParts = getCSSVariableParts(variableExpression);
  var tokenName = cssVariableParts ? getTokenName(cssVariableParts.variable) : undefined;
  var rawToken = (getNonActiveThemeMode() === 'light' ? _tokensRaw.light : _tokensRaw.dark).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