"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "UCLContext", {
  enumerable: true,
  get: function get() {
    return _uclContext["default"];
  }
});
exports["default"] = UCLProvider;
exports.useThemeByComponent = exports.useTheme = void 0;

var _react = _interopRequireWildcard(require("react"));

require("../../components/library.all.module.css");

var _uclContext = _interopRequireDefault(require("./uclContext"));

var _UCLProvider = _interopRequireDefault(require("./UCLProvider.propTypes"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

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; }

var getModeClassName = function getModeClassName(mode) {
  return "lib-mode-".concat(mode);
};

var getThemeClassName = function getThemeClassName(theme) {
  return "lib-theme-".concat(theme);
};

var defaultFontBaseSizeName = "--lib_base_textsize";
var defaultConfiguration = {
  "configuration": {
    "--lib_base_textsize": 16
  }
};

var renderGlobalVariables = function renderGlobalVariables(configuration) {
  return Object.keys(configuration).map(function (variableName, index) {
    return "".concat(variableName, " : ").concat(configuration[variableName], ";");
  }).join('\n');
}; // overriding the public library variables
// let styleTag = global.document.getElementById("zoho-desk-ucl-variables");
// We need to handle and check nested use case of theme provider... like material ui

/**
 * @param {*} props 
 * @returns
 */


function UCLProvider(props) {
  var themeInfo = _objectSpread(_objectSpread({}, defaultConfiguration), props.value || {});

  var variableNameClassName = ".".concat(getModeClassName(props.mode), ".").concat(getThemeClassName(props.theme));
  var docsFontStyle = '';

  if (false) {
    docsFontStyle = "\n      html.isRem{\n        font-size: ".concat(themeInfo.configuration[defaultFontBaseSizeName], "px;\n      }\n   ");
  }

  return /*#__PURE__*/_react["default"].createElement(_uclContext["default"].Provider, {
    value: props.value
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(getModeClassName(props.mode), " ").concat(getThemeClassName(props.theme), " ").concat(props.customClassName || '')
  }, /*#__PURE__*/_react["default"].createElement("style", {
    id: "zoho-desk-ucl-variables"
  }, "\n                  ".concat(variableNameClassName, " {\n                    ").concat(renderGlobalVariables(themeInfo.configuration), "\n                  }\n                 ").concat(docsFontStyle, "\n              ")), props.children));
}

UCLProvider.propTypes = _UCLProvider["default"];

var useTheme = function useTheme() {
  return (0, _react.useContext)(_uclContext["default"]);
};

exports.useTheme = useTheme;

var useThemeByComponent = function useThemeByComponent(componentName) {
  var themeContextValue = (0, _react.useContext)(_uclContext["default"]);
  return themeContextValue && themeContextValue.components && themeContextValue.components[componentName];
};

exports.useThemeByComponent = useThemeByComponent;