import _extends from '@babel/runtime/helpers/extends';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import isEqual from '../../node_modules/lodash-es/isEqual.js';
import _find from '../../node_modules/lodash-es/find.js';
import _omit from '../../node_modules/lodash-es/omit.js';
import React__default, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { CARD_TYPES } from '../../constants/LayoutConstants.js';
import ValueCard from '../ValueCard/ValueCard.js';
import TimeSeriesCard from '../TimeSeriesCard/TimeSeriesCard.js';
import BarChartCard from '../BarChartCard/BarChartCard.js';
import ImageCard from '../ImageCard/ImageCard.js';
import TableCard from '../TableCard/TableCard.js';
import ListCard from '../ListCard/ListCard.js';
import Card from '../Card/Card.js';
import { timeRangeToJSON, isCardJsonValid } from './editorUtils.js';

var _excluded = ["id", "children"];

/**
 * Renders a card and lists the JSON within
 * @param {Object} props
 * @returns {Node}
 */
// eslint-disable-next-line react/prop-types
var renderDefaultCard = function renderDefaultCard(_ref) {
  var id = _ref.id,
    children = _ref.children,
    others = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/React__default.createElement(Card, _extends({
    isEditable: true
  }, others), /*#__PURE__*/React__default.createElement("div", {
    style: {
      padding: '1rem'
    }
  }, JSON.stringify(id, null, 4)), children);
};

/**
 * This component renders our default representation of a card in the editor based on the card type
 */
var DashboardEditorDefaultCardRenderer = function DashboardEditorDefaultCardRenderer(_ref2) {
  var _cardProps$content;
  var cardProps = _ref2.card,
    availableDimensions = _ref2.availableDimensions;
  // For time based cards, we need to deduce the correct interval
  var timeRangeJSON = _find(timeRangeToJSON, function (_ref3) {
    var _cardProps$dataSource;
    var range = _ref3.range;
    return isEqual(range, cardProps === null || cardProps === void 0 || (_cardProps$dataSource = cardProps.dataSource) === null || _cardProps$dataSource === void 0 ? void 0 : _cardProps$dataSource.range);
  });
  var interval = useMemo(function () {
    return (timeRangeJSON === null || timeRangeJSON === void 0 ? void 0 : timeRangeJSON.interval) || 'day';
  }, [timeRangeJSON]);

  // if our card isn't valid render the default
  if (!isCardJsonValid(cardProps)) {
    // eslint-disable-next-line no-console
    console.warn("Warning: The card JSON for ".concat(cardProps.id, " is invalid."));
    return renderDefaultCard(cardProps);
  }

  // otherwise we can check the type to decide which card to render
  switch (cardProps.type) {
    case CARD_TYPES.VALUE:
      return /*#__PURE__*/React__default.createElement(ValueCard, _extends({
        isEditable: true
      }, cardProps));
    case CARD_TYPES.TIMESERIES:
      {
        return /*#__PURE__*/React__default.createElement(TimeSeriesCard, _extends({
          isEditable: true,
          interval: interval
        }, cardProps));
      }
    case CARD_TYPES.BAR:
      return /*#__PURE__*/React__default.createElement(BarChartCard, _extends({
        isEditable: true,
        isDashboardPreview: true,
        availableDimensions: availableDimensions,
        interval: interval
      }, cardProps));
    case CARD_TYPES.TABLE:
      return /*#__PURE__*/React__default.createElement(TableCard
      // workaround needed to regen TableCard if columns change
      , _extends({
        key: JSON.stringify(cardProps === null || cardProps === void 0 || (_cardProps$content = cardProps.content) === null || _cardProps$content === void 0 ? void 0 : _cardProps$content.columns),
        isEditable: true
      }, cardProps));
    case CARD_TYPES.IMAGE:
      return /*#__PURE__*/React__default.createElement(ImageCard, _extends({}, cardProps, {
        isEditable: true // render the icon in the right color in the card preview
      }));
    case CARD_TYPES.LIST:
      return /*#__PURE__*/React__default.createElement(ListCard, _extends({
        isEditable: true
      }, cardProps));
    default:
      // if the user passes an element for a custom card type, render it
      return /*#__PURE__*/React__default.isValidElement(cardProps.content) || typeof cardProps.content === 'function' ? /*#__PURE__*/React__default.createElement(Card, _extends({
        isEditable: true
        // need to omit the content because its getting passed content to be rendered, which should not
        // get attached to the card wrapper
      }, _omit(cardProps, 'content')), cardProps.content, cardProps.children // children is the resizable element attached by react-grid-layout which must be rendered to support resizing
      ) :
      // all else fails render the default card
      renderDefaultCard(cardProps);
  }
};
DashboardEditorDefaultCardRenderer.propTypes = {
  card: PropTypes.oneOfType([PropTypes.object]),
  availableDimensions: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])))
};
DashboardEditorDefaultCardRenderer.defaultProps = {
  card: undefined,
  availableDimensions: undefined
};
DashboardEditorDefaultCardRenderer.__docgenInfo = {
  "description": "This component renders our default representation of a card in the editor based on the card type",
  "methods": [],
  "displayName": "DashboardEditorDefaultCardRenderer",
  "props": {
    "card": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "union",
        "value": [{
          "name": "object"
        }]
      },
      "required": false
    },
    "availableDimensions": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "objectOf",
        "value": {
          "name": "arrayOf",
          "value": {
            "name": "union",
            "value": [{
              "name": "string"
            }, {
              "name": "number"
            }]
          }
        }
      },
      "required": false
    }
  }
};
var DashboardEditorDefaultCardRenderer$1 = DashboardEditorDefaultCardRenderer;

export { DashboardEditorDefaultCardRenderer$1 as default };
