"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ThemeProvider = void 0;

var _base = _interopRequireDefault(require("@emotion/styled/base"));

var _react = require("@emotion/react");

var _locales = require("@tablecheck/locales");

var _react2 = require("react");

var _Theme = require("./Theme.js");

var _colors = require("./colors.js");

var _fontSettings = require("./fontSettings.js");

var _palettes = require("./palettes.js");

var _resetCss = require("./resetCss.js");

var _jsxRuntime = require("@emotion/react/jsx-runtime");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var themeColorKeys = Object.keys(_palettes.CLASSIC_COLORS);

function validateTheme(themeArg) {
  if (!themeArg || !themeArg.colors) return;
  var theme = themeArg;
  var unsetColorKeys = themeColorKeys.reduce(function (unsetKeys, key) {
    if (!theme.colors[key]) {
      unsetKeys.push(key);
    }

    return unsetKeys;
  }, []);

  if (unsetColorKeys.length) {
    throw new Error("ThemeProvider is missing the following values; ".concat(unsetColorKeys.map(function (key) {
      return "colors.".concat(key);
    }).join(','), ". Please extend CLASSIC_COLORS or DARK_COLORS with your custom theme."));
  }
}

var VersionTag = (0, _base.default)("div", process.env.NODE_ENV === "production" ? {
  target: "e7fo2461"
} : {
  target: "e7fo2461",
  label: "VersionTag"
})("display:none;position:fixed;bottom:", _Theme.Spacing.L1, ";left:", _Theme.Spacing.L1, ";padding:", _Theme.Spacing.L2, ";box-shadow:0px 0px 4px ", _colors.YELLOW.L5, ";border:1px solid ", _colors.YELLOW.L5, ";border-radius:", _Theme.Spacing.L1, ";background:", _colors.BLUE.L5, ";color:white;z-index:99999;");
var Local = (0, _base.default)("div", process.env.NODE_ENV === "production" ? {
  target: "e7fo2460"
} : {
  target: "e7fo2460",
  label: "Local"
})(function (_ref) {
  var fontFamily = _ref.fontFamily;
  return fontFamily;
}, ";");

var ThemeProvider = function ThemeProvider(_ref2) {
  var country = _ref2.country,
      locale = _ref2.locale,
      children = _ref2.children,
      _ref2$theme = _ref2.theme,
      theme = _ref2$theme === void 0 ? {} : _ref2$theme,
      _ref2$useLocalStyles = _ref2.useLocalStyles,
      useLocalStyles = _ref2$useLocalStyles === void 0 ? false : _ref2$useLocalStyles,
      _ref2$isDark = _ref2.isDark,
      isDark = _ref2$isDark === void 0 ? false : _ref2$isDark,
      fontWeights = _ref2.fontWeights,
      _ref2$renderHeadNodes = _ref2.renderHeadNodes,
      renderHeadNodes = _ref2$renderHeadNodes === void 0 ? function (nodes) {
    return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: nodes
    });
  } : _ref2$renderHeadNodes;

  var _useMemo = (0, _react2.useMemo)(function () {
    return (0, _fontSettings.getFont)(locale, country, fontWeights);
  }, [country, fontWeights, locale]),
      _useMemo2 = _slicedToArray(_useMemo, 2),
      fontHeadNodes = _useMemo2[0],
      fontFace = _useMemo2[1];

  var _useMemo3 = (0, _react2.useMemo)(function () {
    validateTheme(theme);
    var fontFamily = "font-family: ".concat((0, _fontSettings.getFontFamily)(locale, country), ";");
    var isRtl = false;

    if (locale) {
      var matchedLocale = _locales.ordered.find(function (_ref3) {
        var code = _ref3.code;
        return code === locale;
      });

      isRtl = matchedLocale ? matchedLocale.dir === 'rtl' : false;
    }

    return [_objectSpread(_objectSpread({
      colors: isDark ? _palettes.DARK_COLORS : _palettes.CLASSIC_COLORS
    }, theme), {}, {
      fontFamily: fontFamily,
      isRtl: isRtl,
      isDark: isDark
    }), (0, _resetCss.resetCss)(fontFamily, theme.colors, fontWeights), fontFamily];
  }, [country, fontWeights, isDark, locale, theme]),
      _useMemo4 = _slicedToArray(_useMemo3, 3),
      constructedTheme = _useMemo4[0],
      globalStyles = _useMemo4[1],
      resolvedFontFamily = _useMemo4[2];

  if (useLocalStyles) {
    return (0, _jsxRuntime.jsxs)(_react.ThemeProvider, {
      theme: constructedTheme,
      children: [(0, _jsxRuntime.jsx)(_react.Global, {
        styles: fontFace
      }), (0, _jsxRuntime.jsx)(Local, {
        fontFamily: resolvedFontFamily,
        children: children
      })]
    });
  }

  return (0, _jsxRuntime.jsxs)(_react.ThemeProvider, {
    theme: constructedTheme,
    children: [renderHeadNodes(fontHeadNodes), (0, _jsxRuntime.jsx)(_react.Global, {
      styles: fontFace
    }), (0, _jsxRuntime.jsx)(_react.Global, {
      styles: globalStyles
    }), children, process.env.NODE_ENV !== 'test' && CONFIG.appVersion ? (0, _jsxRuntime.jsxs)(VersionTag, {
      id: "tablecheck-version-tag",
      children: ["App Version: ", CONFIG.appVersion]
    }) : null]
  });
};

exports.ThemeProvider = ThemeProvider;
