import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

/* eslint-disable no-restricted-syntax */

/* eslint-disable guard-for-in */
import React, { useMemo } from "react";
import { ThemeProvider } from "@emotion/react";
import { isObject } from "../../utils/helpers";

var getKeys = function getKeys(origin) {
  var res = {};

  function processObject(obj, current) {
    for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
      var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
          key = _Object$entries$_i[0],
          value = _Object$entries$_i[1];

      var newKey = current ? "".concat(current, ".").concat(key) : key;

      if (isObject(value)) {
        processObject(value, newKey);
      } else {
        res[newKey] = value;
      }
    }
  }

  ;
  processObject(origin);
  return res;
};

var getBreakpoints = function getBreakpoints() {
  var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  var queryString = function queryString(size) {
    return "@media (min-width: ".concat(size, "px)");
  };

  var medias = Object.values(values).map(queryString);
  var objectMedias = Object.keys(values).reduce(function (acc, key) {
    acc[key] = queryString(values[key]);
    return acc;
  }, {});

  var asObject = _objectSpread({
    base: null
  }, objectMedias);

  var isResponsive = function isResponsive(value) {
    if (!isObject(value)) {
      return false;
    }

    var keys = Object.keys(value);
    return keys.length > 0 && keys.every(function (key) {
      return key in asObject;
    });
  };

  var normalize = function normalize(obj) {
    var result = Object.keys(asObject).map(function (bp) {
      var _obj$bp;

      return (_obj$bp = obj[bp]) !== null && _obj$bp !== void 0 ? _obj$bp : null;
    });
    return result;
  };

  return {
    medias: [null].concat(_toConsumableArray(medias)),
    isResponsive: isResponsive,
    normalize: normalize
  };
};

export var createStyleComponentsTheme = function createStyleComponentsTheme(theme) {
  var _theme$breakpoints;

  return _objectSpread(_objectSpread({}, theme), {}, {
    __map: getKeys(theme),
    __breakpoints: getBreakpoints((_theme$breakpoints = theme.breakpoints) === null || _theme$breakpoints === void 0 ? void 0 : _theme$breakpoints.values)
  });
};

var StyleComponentsProvider = function StyleComponentsProvider(_ref) {
  var children = _ref.children,
      _theme = _ref.theme;
  var theme = useMemo(function () {
    return createStyleComponentsTheme(_theme);
  }, [_theme]);
  return /*#__PURE__*/React.createElement(ThemeProvider, {
    theme: theme
  }, children);
};

export default StyleComponentsProvider;