"use strict";

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

var _react = require("react");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _core = require("@emotion/core");

var _shortid = _interopRequireDefault(require("shortid"));

var _checkData = _interopRequireDefault(require("./checkData"));

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

function _templateObject3() {
  var data = _taggedTemplateLiteral([""]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n              color: red;\n            "]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n  margin: 0 auto;\n  max-width: 900px;\n  width: 90%;\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var wrapperStyle = (0, _core.css)(_templateObject());

var DataChecker = function DataChecker(_ref) {
  var data = _ref.data,
      dataIsObject = _ref.dataIsObject,
      dataAccessors = _ref.dataAccessors,
      optionalKeys = _ref.optionalKeys,
      message = _ref.message,
      children = _ref.children;
  var keys = Object.keys(dataAccessors);
  var values = Object.values(dataAccessors);
  var results = (0, _checkData["default"])(data, values, dataIsObject, optionalKeys);
  var component = results.allKeysValid ? children : (0, _core.jsx)("div", {
    css: wrapperStyle
  }, (0, _core.jsx)("h1", null, message), results.invalidType && (0, _core.jsx)("h2", null, "Invalid Type - check if array or object is expected"), (0, _core.jsx)("h2", null, "Data Accessors"), keys.map(function (key, index) {
    var isValid = results[values[index]].valid === results[values[index]].total;
    var validCount = results[values[index]].valid;
    var totalCount = results[values[index]].total;
    var keyValue = values[index];
    var invalidStyle = !isValid ? (0, _core.css)(_templateObject2()) : (0, _core.css)(_templateObject3());
    return (0, _core.jsx)("pre", {
      key: _shortid["default"].generate(),
      css: invalidStyle
    }, (0, _core.jsx)("strong", null, isValid ? "✅ " : "⛔️ ", "".concat(key, ": ")), JSON.stringify(keyValue, undefined, 2), !isValid && (0, _core.jsx)(_react.Fragment, null, (0, _core.jsx)("br", null), (0, _core.jsx)("small", null, validCount === 0 ? "".concat(key, " is invalid. \"").concat(keyValue, "\" not found in data") : "".concat(key, " is invalid. \"").concat(keyValue, "\" missing in ").concat(totalCount - validCount, "/").concat(totalCount, " objects in data"))));
  }), (0, _core.jsx)("h2", null, "Data"), (0, _core.jsx)("pre", null, (0, _core.jsx)("strong", null, "data: "), !dataIsObject && JSON.stringify(data, undefined, 2)));
  return component;
};

DataChecker.propTypes = {
  data: _propTypes["default"].oneOfType([_propTypes["default"].shape({}), _propTypes["default"].arrayOf(_propTypes["default"].shape({}))]).isRequired,
  dataAccessors: _propTypes["default"].objectOf(_propTypes["default"].string).isRequired,
  message: _propTypes["default"].string,
  optionalKeys: _propTypes["default"].shape({})
};
DataChecker.defaultProps = {
  message: "Invalid Data",
  dataIsObject: false
};
var _default = DataChecker;
exports["default"] = _default;