"use strict";

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

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

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

var _widget = _interopRequireDefault(require("all:part:@sanity/dashboard/widget?"));

var _NotFoundWidget = _interopRequireDefault(require("../components/NotFoundWidget"));

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 WidgetContainer(props) {
  var config = props.config || {};
  var definition = Array.isArray(_widget.default) ? _widget.default.find(wid => wid.name === config.name) : null;

  if (definition) {
    var options = _objectSpread(_objectSpread({}, definition.options || {}), config.options || {});

    var _layout = _objectSpread(_objectSpread({}, definition.layout || {}), config.layout || {});

    return /*#__PURE__*/_react.default.createElement("div", {
      "data-width": _layout.width,
      "data-height": _layout.height
    }, /*#__PURE__*/_react.default.createElement(definition.component, options));
  }

  var layout = config.layout || {};
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-width": layout.width,
    "data-height": layout.height
  }, /*#__PURE__*/_react.default.createElement(_NotFoundWidget.default, {
    title: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "Not found: \"", config.name, "\"")
  }, /*#__PURE__*/_react.default.createElement("p", null, "Make sure your ", /*#__PURE__*/_react.default.createElement("code", null, "sanity.json"), " file mentions such a widget and that it\u2019s an implementation of ", /*#__PURE__*/_react.default.createElement("code", null, "part:@sanity/dashboard/widget"), ".")));
}

WidgetContainer.propTypes = {
  // eslint-disable-next-line react/forbid-prop-types
  config: _propTypes.default.any
};
WidgetContainer.defaultProps = {
  config: null
};
var _default = WidgetContainer;
exports.default = _default;