"use strict";

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

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

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

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

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

  if (!results.allKeysValid) {
    console.warn("%cCivicCard Input Violations", "font-weight: bold; font-size: 30px; line-height: 40px; color: red; tex-shadow: 1px 1px 0px black, 1px -1px 0px black, -1px 1px 0px black, -1px -1px 0px black");
    console.warn("%c".concat(message), "font-weight: bold; font-size: 15px; line-height: 40px; color: red; tex-shadow: 1px 1px 0px black, 1px -1px 0px black, -1px 1px 0px black, -1px -1px 0px black");

    if (results.invalidType) {
      console.error("Invalid Type - check if array or object is expected");
    }

    var accessorMessage = function accessorMessage(isValid, isOptional, isNull, key, keyValue, validCount, totalCount) {
      var keyMessage = "";

      if (isOptional) {
        keyMessage = "".concat(key, " is optional");
      } else if (isNull) {
        keyMessage = "".concat(key, " cannot be null");
      } else if (validCount === 0) {
        keyMessage = "".concat(key, " is invalid. \"").concat(keyValue, "\" not found in data");
      } else if (!isValid) {
        return "".concat(key, " is invalid. \"").concat(keyValue, "\" missing in ").concat(totalCount - validCount, "/").concat(totalCount, " objects in data");
      }

      return "\n        ".concat(isOptional ? "✅ Optional --" : isValid && !isNull ? "✅" : "🥺", " ").concat(key, ": ").concat(keyValue && JSON.stringify(keyValue, undefined, 2), "\n        ").concat(keyMessage || "", "\n      ");
    };

    var dataAccessorViolations = keys.map(function (key, index) {
      var isValid = results[values[index]].valid === results[values[index]].total;
      var isOptional = optionalKeys[key];
      var isNull = results[values[index]].isNull;
      var validCount = results[values[index]].valid;
      var totalCount = results[values[index]].total;
      var keyValue = values[index];
      return accessorMessage(isValid, isOptional, isNull, key, keyValue, validCount, totalCount);
    });
    console.warn("\n      Data Accessors\n\n      ".concat(dataAccessorViolations.join("\n"), "\n    "));
  }

  return children;
};

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;