import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isEmpty from '../../../../../node_modules/lodash-es/isEmpty.js';
import _omit from '../../../../../node_modules/lodash-es/omit.js';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.find.js';
import 'core-js/modules/es.array.includes.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.string.includes.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default, { useMemo, useState, useRef, useCallback } from 'react';
import PropTypes from 'prop-types';
import { v4 } from 'uuid';
import hash from 'object-hash';
import { ComboBox, Dropdown } from '@carbon/react';
import { settings } from '../../../../../constants/Settings.js';
import { DataItemsPropTypes, DashboardEditorActionsPropTypes, handleDataSeriesChange, DATAITEM_COLORS_OPTIONS, defaultDashboardEditorActionsProps } from '../../../../DashboardEditor/editorUtils.js';
import Button from '../../../../Button/Button.js';
import List from '../../../../List/List.js';
import DataSeriesFormItemModal from '../DataSeriesFormItemModal.js';
import { BAR_CHART_TYPES, CARD_TYPES } from '../../../../../constants/LayoutConstants.js';
import ContentFormItemTitle from '../ContentFormItemTitle.js';
import HierarchyDataFormItems, { isHierarchyDataItem } from '../HierarchyDataFormItems/HierarchyDataFormItems.js';
import { getTranslatedLabel } from '../../../../../utils/cardUtilityFunctions.js';
import BarChartDataSeriesContent from './BarChartDataSeriesContent.js';
import { Edit, MisuseOutline } from '@carbon/icons-react';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var iotPrefix = settings.iotPrefix;
var propTypes = {
  /* card value */
  cardConfig: PropTypes.shape({
    id: PropTypes.string,
    title: PropTypes.string,
    size: PropTypes.string,
    type: PropTypes.string,
    content: PropTypes.oneOfType([PropTypes.shape({
      series: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string,
        dataSourceId: PropTypes.string,
        color: PropTypes.string
      })),
      xLabel: PropTypes.string,
      yLabel: PropTypes.string,
      unit: PropTypes.string,
      includeZeroOnXaxis: PropTypes.bool,
      includeZeroOnYaxis: PropTypes.bool,
      timeDataSourceId: PropTypes.string,
      showLegend: PropTypes.bool
    }),
    // custom card content is a function
    PropTypes.func]),
    interval: PropTypes.string
  }),
  /* callback when data item input value changes */
  onChange: PropTypes.func.isRequired,
  /** if provided, returns an array of strings which are the dataItems to be allowed
   * on each card
   * getValidDataItems(card, selectedTimeRange)
   */
  getValidDataItems: PropTypes.func,
  /** an array of dataItems to be included on each card
   * this prop will be ignored if getValidDataItems is defined
   */
  dataItems: DataItemsPropTypes,
  /** an object where the keys are available dimensions and the values are the values available for those dimensions
   *  ex: { manufacturer: ['Rentech', 'GHI Industries'], deviceid: ['73000', '73001', '73002'] }
   */
  availableDimensions: PropTypes.shape({}),
  /** list of dataItem names that have been selected to display on the card */
  selectedDataItems: PropTypes.arrayOf(PropTypes.string),
  setSelectedDataItems: PropTypes.func.isRequired,
  selectedTimeRange: PropTypes.string,
  isSummaryDashboard: PropTypes.bool,
  /** optional link href's for each card type that will appear in a tooltip */
  dataSeriesItemLinks: PropTypes.shape({
    simpleBar: PropTypes.string,
    groupedBar: PropTypes.string,
    stackedBar: PropTypes.string,
    timeSeries: PropTypes.string,
    value: PropTypes.string,
    custom: PropTypes.string
  }),
  i18n: PropTypes.shape({
    dataItemEditorTitle: PropTypes.string,
    dataItemEditorDataItemTitle: PropTypes.string,
    dataItemEditorDataItemLabel: PropTypes.string,
    dataItemEditorLegendColor: PropTypes.string,
    dataItemEditorSectionTitle: PropTypes.string,
    dataItemEditorSectionSimpleBarTooltipText: PropTypes.string,
    dataItemEditorSectionGroupedBarTooltipText: PropTypes.string,
    dataItemEditorSectionStackedBarTooltipText: PropTypes.string,
    dataItemEditorSectionTimeSeriesTooltipText: PropTypes.string,
    dataItemEditorSectionValueTooltipText: PropTypes.string,
    dataItemEditorSectionCustomTooltipText: PropTypes.string,
    dataItemEditorSectionTooltipLinkText: PropTypes.string,
    dataSeriesTitle: PropTypes.string,
    selectDataItems: PropTypes.string,
    selectDataItem: PropTypes.string,
    dataItem: PropTypes.string,
    edit: PropTypes.string,
    remove: PropTypes.string,
    customize: PropTypes.string,
    clearAllText: PropTypes.string,
    clearSelectionText: PropTypes.string,
    openMenuText: PropTypes.string,
    closeMenuText: PropTypes.string,
    incrementNumberText: PropTypes.string,
    decrementNumberText: PropTypes.string
  }),
  /** whether to use translated labels in cards */
  shouldUseTranslatedLabels: PropTypes.bool,
  translateWithId: PropTypes.func.isRequired,
  actions: DashboardEditorActionsPropTypes
};
var defaultProps = {
  cardConfig: {},
  selectedTimeRange: '',
  i18n: {
    dataItemEditorTitle: 'Edit data series',
    dataItemEditorDataItemTitle: 'Data item',
    dataItemEditorDataItemLabel: 'Label',
    dataItemEditorLegendColor: 'Legend color',
    dataItemEditorSectionTitle: 'Data',
    dataItemEditorSectionSimpleBarTooltipText: 'Display a metric using bars. Plot over time or by a dimension from Group by.',
    dataItemEditorSectionGroupedBarTooltipText: 'Group categories side by side in bars. Show groupings of related metrics or different categories of a single metric.',
    dataItemEditorSectionStackedBarTooltipText: 'Stack bars by categories of a single dimension or into multiple related metrics.',
    dataItemEditorSectionTimeSeriesTooltipText: 'Plot time series metrics over time.',
    dataItemEditorSectionValueTooltipText: 'Display metric values, dimension values, or alert counts. Select from Data item. ',
    dataItemEditorSectionCustomTooltipText: 'Show or hide alert fields. Choose dimensions to add as extra columns. ',
    dataItemEditorSectionTooltipLinkText: 'Learn more',
    selectDataItems: 'Select data items',
    selectDataItem: 'Select data item',
    dataItem: 'Data item',
    edit: 'Edit',
    remove: 'Remove',
    customize: 'Customize',
    filter: 'Filter',
    clearAllText: 'Clear all',
    clearSelectionText: 'Clear selection',
    openMenuText: 'Open menu',
    closeMenuText: 'Close menu',
    incrementNumberText: 'Increment number',
    decrementNumberText: 'Decrement number'
  },
  shouldUseTranslatedLabels: false,
  getValidDataItems: null,
  dataItems: [],
  selectedDataItems: [],
  availableDimensions: {},
  isSummaryDashboard: false,
  dataSeriesItemLinks: null,
  actions: defaultDashboardEditorActionsProps
};
var formatDataItemsForDropdown = function formatDataItemsForDropdown(dataItems) {
  return (dataItems === null || dataItems === void 0 ? void 0 : dataItems.map(function (_ref) {
    var dataItemId = _ref.dataItemId,
      label = _ref.label;
    return {
      id: dataItemId,
      text: label
    };
  })) || [];
};

/**
 * Retuns card specific tooltip depending on card type
 * @param {Object} cardConfig
 * @returns {Object} tooltip definition
 */
var defineCardSpecificTooltip = function defineCardSpecificTooltip(cardConfig, dataSeriesItemLinks, i18n) {
  switch (cardConfig.type) {
    case CARD_TYPES.BAR:
      if (cardConfig.content.type === BAR_CHART_TYPES.SIMPLE) {
        return _objectSpread({
          tooltipText: i18n.dataItemEditorSectionSimpleBarTooltipText
        }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.simpleBar ? {
          linkText: i18n.dataItemEditorSectionTooltipLinkText,
          href: dataSeriesItemLinks.simpleBar
        } : {});
      }
      if (cardConfig.content.type === BAR_CHART_TYPES.GROUPED) {
        return _objectSpread({
          tooltipText: i18n.dataItemEditorSectionGroupedBarTooltipText
        }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.groupedBar ? {
          linkText: i18n.dataItemEditorSectionTooltipLinkText,
          href: dataSeriesItemLinks.groupedBar
        } : {});
      }
      // STACKED
      return _objectSpread({
        tooltipText: i18n.dataItemEditorSectionStackedBarTooltipText
      }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.stackedBar ? {
        linkText: i18n.dataItemEditorSectionTooltipLinkText,
        href: dataSeriesItemLinks.stackedBar
      } : {});
    case CARD_TYPES.TIMESERIES:
      return _objectSpread({
        tooltipText: i18n.dataItemEditorSectionTimeSeriesTooltipText
      }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.timeSeries ? {
        linkText: i18n.dataItemEditorSectionTooltipLinkText,
        href: dataSeriesItemLinks.timeSeries
      } : {});
    case CARD_TYPES.VALUE:
      return _objectSpread({
        tooltipText: i18n.dataItemEditorSectionValueTooltipText
      }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.value ? {
        linkText: i18n.dataItemEditorSectionTooltipLinkText,
        href: dataSeriesItemLinks.value
      } : {});
    case CARD_TYPES.CUSTOM:
    default:
      return _objectSpread({
        tooltipText: i18n.dataItemEditorSectionCustomTooltipText
      }, dataSeriesItemLinks !== null && dataSeriesItemLinks !== void 0 && dataSeriesItemLinks.custom ? {
        linkText: i18n.dataItemEditorSectionTooltipLinkText,
        href: dataSeriesItemLinks.custom
      } : {});
  }
};
var baseClassName = "".concat(iotPrefix, "--card-edit-form");
var DataSeriesFormItem = function DataSeriesFormItem(_ref2) {
  var _cardConfig$content, _cardConfig$content2, _cardConfig$content3, _cardConfig$content4, _cardConfig$content5, _cardConfig$content6;
  var cardConfig = _ref2.cardConfig,
    isSummaryDashboard = _ref2.isSummaryDashboard,
    dataItems = _ref2.dataItems,
    getValidDataItems = _ref2.getValidDataItems,
    _onChange = _ref2.onChange,
    selectedDataItems = _ref2.selectedDataItems,
    setSelectedDataItems = _ref2.setSelectedDataItems,
    selectedTimeRange = _ref2.selectedTimeRange,
    availableDimensions = _ref2.availableDimensions,
    i18n = _ref2.i18n,
    shouldUseTranslatedLabels = _ref2.shouldUseTranslatedLabels,
    dataSeriesItemLinks = _ref2.dataSeriesItemLinks,
    translateWithId = _ref2.translateWithId,
    actions = _ref2.actions;
  var onEditDataItem = actions.onEditDataItem;
  var mergedI18n = useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  }, [i18n]);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showEditor = _useState2[0],
    setShowEditor = _useState2[1];
  var _useState3 = useState({}),
    _useState4 = _slicedToArray(_useState3, 2),
    editDataItem = _useState4[0],
    setEditDataItem = _useState4[1];
  var _useState5 = useState(((_cardConfig$content = cardConfig.content) === null || _cardConfig$content === void 0 ? void 0 : _cardConfig$content.series) || []),
    _useState6 = _slicedToArray(_useState5, 2),
    editDataSeries = _useState6[0],
    setEditDataSeries = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    removedDataItems = _useState8[0],
    setRemovedDataItems = _useState8[1];
  var canMultiSelectDataItems = ((_cardConfig$content2 = cardConfig.content) === null || _cardConfig$content2 === void 0 ? void 0 : _cardConfig$content2.type) !== BAR_CHART_TYPES.SIMPLE;
  var type = cardConfig.type;

  // determine which content section to look at
  var dataSection = cardConfig.type === CARD_TYPES.TIMESERIES || cardConfig.type === CARD_TYPES.BAR ? cardConfig === null || cardConfig === void 0 || (_cardConfig$content3 = cardConfig.content) === null || _cardConfig$content3 === void 0 ? void 0 : _cardConfig$content3.series : cardConfig === null || cardConfig === void 0 || (_cardConfig$content4 = cardConfig.content) === null || _cardConfig$content4 === void 0 ? void 0 : _cardConfig$content4.attributes;
  var removedItemsCountRef = useRef(0);
  var validDataItems = useMemo(function () {
    return getValidDataItems ? getValidDataItems(cardConfig, selectedTimeRange) : dataItems;
  }, [cardConfig, dataItems, getValidDataItems, selectedTimeRange]);
  var cardSpecificTooltip = defineCardSpecificTooltip(cardConfig, dataSeriesItemLinks, mergedI18n);
  var handleSimpleDataSeriesChange = useCallback(function (_ref3) {
    var selectedItem = _ref3.selectedItem;
    // ignore the extra value added by the "enter" keypress
    if (selectedItem && !isEmpty(selectedItem.id)) {
      var itemWithMetaData = validDataItems === null || validDataItems === void 0 ? void 0 : validDataItems.find(function (_ref4) {
        var dataItemId = _ref4.dataItemId;
        return dataItemId === selectedItem.id;
      });
      var selectedItems = [].concat(_toConsumableArray(dataSection), [_objectSpread(_objectSpread({}, itemWithMetaData && _objectSpread({}, itemWithMetaData)), {}, {
        // create a unique dataSourceId if it's going into attributes
        // if it's going into the groupBy section then just use the dataItem ID
        dataSourceId: (itemWithMetaData === null || itemWithMetaData === void 0 ? void 0 : itemWithMetaData.destination) === 'groupBy' ? selectedItem.id : "".concat(selectedItem.id, "_").concat(v4())
      })]);
      // need to remove the category if the card is a stacked timeseries bar
      var card = cardConfig.content.type === BAR_CHART_TYPES.STACKED && cardConfig.content.timeDataSourceId && selectedItems.length > 1 ? _omit(cardConfig, 'content.categoryDataSourceId') : cardConfig;
      var newCard = handleDataSeriesChange(selectedItems, card, setEditDataSeries, undefined, removedItemsCountRef);
      setSelectedDataItems(selectedItems.map(function (_ref5) {
        var dataSourceId = _ref5.dataSourceId;
        return dataSourceId;
      }));
      _onChange(newCard);
    }
  }, [cardConfig, dataSection, _onChange, setSelectedDataItems, validDataItems]);
  var handleHierarchyDataItemChange = useCallback(function (items) {
    var updatedItems = items.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        // create a unique dataSourceId if it's going into attributes
        // if it's going into the groupBy section then just use the dataItem ID
        dataSourceId: "".concat(item.dataItemId, "_").concat(v4())
      });
    });
    var selectedItems = canMultiSelectDataItems ? [].concat(_toConsumableArray(dataSection), _toConsumableArray(updatedItems)) : [updatedItems[0]];
    var newCard = handleDataSeriesChange(selectedItems, cardConfig, setEditDataSeries, undefined, removedItemsCountRef);
    setSelectedDataItems(selectedItems.map(function (_ref6) {
      var dataSourceId = _ref6.dataSourceId;
      return dataSourceId;
    }));
    _onChange(newCard);
  }, [canMultiSelectDataItems, cardConfig, dataSection, _onChange, setSelectedDataItems]);
  var handleEditButton = useCallback(/*#__PURE__*/function () {
    var _ref7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dataItem, i) {
      var dataItemWithMetaData, colorIndex, aggregationMethods;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            dataItemWithMetaData = validDataItems === null || validDataItems === void 0 ? void 0 : validDataItems.find(function (_ref8) {
              var dataItemId = _ref8.dataItemId;
              return dataItemId === dataItem.dataItemId;
            });
            colorIndex = (removedItemsCountRef.current + i) % DATAITEM_COLORS_OPTIONS.length; // Call back function for on click of edit button
            if (!onEditDataItem) {
              _context.next = 7;
              break;
            }
            _context.next = 5;
            return onEditDataItem(cardConfig, dataItem, dataItemWithMetaData);
          case 5:
            aggregationMethods = _context.sent;
            if (!isEmpty(aggregationMethods)) {
              dataItemWithMetaData.aggregationMethods = aggregationMethods;
            }
          case 7:
            // need to reset the card to include the latest dataSection
            _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
              content: _objectSpread(_objectSpread({}, cardConfig.content), cardConfig.type === CARD_TYPES.VALUE ? {
                attributes: dataSection
              } : {
                series: dataSection
              })
            }));
            setEditDataItem(_objectSpread(_objectSpread(_objectSpread({}, dataItemWithMetaData), dataItem), cardConfig.type === CARD_TYPES.TIMESERIES || cardConfig.type === CARD_TYPES.BAR ? {
              color: dataItem.color || DATAITEM_COLORS_OPTIONS[colorIndex]
            } : {}));
            setShowEditor(true);
          case 10:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function (_x, _x2) {
      return _ref7.apply(this, arguments);
    };
  }(), [cardConfig, dataSection, _onChange, onEditDataItem, validDataItems]);
  var handleRemoveButton = useCallback(function (dataItem) {
    var filteredItems = dataSection.filter(function (item) {
      return item.dataSourceId !== dataItem.dataSourceId;
    });
    removedItemsCountRef.current += 1;
    setSelectedDataItems(filteredItems.map(function (item) {
      return item.dataSourceId;
    }));
    setRemovedDataItems([].concat(_toConsumableArray(removedDataItems), [dataItem]));
    setEditDataSeries(filteredItems);
    _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
      content: _objectSpread(_objectSpread({}, cardConfig.content), cardConfig.type === CARD_TYPES.VALUE ? {
        attributes: filteredItems
      } : {
        series: filteredItems
      })
    }));
  }, [cardConfig, dataSection, _onChange, removedDataItems, setSelectedDataItems]);
  var generateListItems = useCallback(function (data) {
    var isHierarchy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    return (data === null || data === void 0 ? void 0 : data.filter(function (dataItem) {
      return isHierarchyDataItem(dataItem) === isHierarchy;
    }).map(function (dataItem, i) {
      var colorIndex = (i + removedItemsCountRef.current) % DATAITEM_COLORS_OPTIONS.length;
      var iconColorOption = dataItem.color || DATAITEM_COLORS_OPTIONS[colorIndex];
      return {
        id: dataItem.dataSourceId,
        content: {
          value: getTranslatedLabel(dataItem.label || dataItem.dataItemId, shouldUseTranslatedLabels, i18n),
          icon: cardConfig.type === CARD_TYPES.TIMESERIES || cardConfig.type === CARD_TYPES.BAR ? /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(baseClassName, "--data-item-list--item-color-icon"),
            style: {
              '--icon-color-option': iconColorOption
            }
          }) : null,
          rowActions: function rowActions() {
            return [/*#__PURE__*/React__default.createElement(Button, {
              key: "data-item-".concat(dataItem.dataSourceId, "_edit"),
              renderIcon: Edit,
              hasIconOnly: true,
              kind: "ghost",
              size: "sm",
              onClick: function onClick() {
                return handleEditButton(dataItem, i);
              },
              iconDescription: mergedI18n.edit,
              tooltipPosition: "left",
              tooltipAlignment: "center"
            }), /*#__PURE__*/React__default.createElement(Button, {
              key: "data-item-".concat(dataItem.dataSourceId, "_remove"),
              renderIcon: MisuseOutline,
              hasIconOnly: true,
              kind: "ghost",
              size: "sm",
              onClick: function onClick() {
                return handleRemoveButton(dataItem);
              },
              iconDescription: mergedI18n.remove,
              tooltipPosition: "left",
              tooltipAlignment: "center"
            })];
          }
        }
      };
    })) || [];
  }, [cardConfig.type, handleEditButton, handleRemoveButton, i18n, mergedI18n.edit, mergedI18n.remove, shouldUseTranslatedLabels]);
  var dataItemListItems = useMemo(function () {
    return generateListItems(dataSection);
  }, [dataSection, generateListItems]);
  var hierarchyDataItemListItems = useMemo(function () {
    return generateListItems(dataSection, true);
  }, [dataSection, generateListItems]);
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(DataSeriesFormItemModal, {
    cardConfig: cardConfig,
    isSummaryDashboard: isSummaryDashboard,
    showEditor: showEditor,
    setShowEditor: setShowEditor,
    editDataSeries: editDataSeries,
    setEditDataSeries: setEditDataSeries,
    editDataItem: editDataItem,
    setEditDataItem: setEditDataItem,
    validDataItems: validDataItems,
    availableDimensions: availableDimensions,
    dataSection: dataSection,
    onChange: _onChange,
    i18n: mergedI18n,
    shouldUseTranslatedLabels: shouldUseTranslatedLabels,
    actions: actions,
    options: {
      hasColorDropdown: type === CARD_TYPES.TIMESERIES || type === CARD_TYPES.BAR,
      hasUnit: type === CARD_TYPES.VALUE,
      hasDecimalPlacesDropdown: type === CARD_TYPES.VALUE,
      hasThresholds: type === CARD_TYPES.VALUE,
      hasTooltip: type === CARD_TYPES.VALUE
    },
    isLarge: true
  }), !isEmpty(validDataItems) && /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(ContentFormItemTitle, {
    title: mergedI18n.dataItemEditorSectionTitle
    // Specific to each card type
    ,
    tooltip: _objectSpread({}, cardSpecificTooltip)
  }), cardConfig.type === CARD_TYPES.BAR && /*#__PURE__*/React__default.createElement(BarChartDataSeriesContent, {
    cardConfig: cardConfig,
    onChange: _onChange,
    availableDimensions: availableDimensions,
    i18n: mergedI18n,
    translateWithId: translateWithId
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, canMultiSelectDataItems ? /*#__PURE__*/React__default.createElement(ComboBox
  // need to re-gen if selected card changes or if a dataItem is removed from the list
  , {
    key: "data-item-select-".concat(hash(validDataItems), "-selected_card-id-").concat(cardConfig.id),
    "data-testid": "editor--data-series--combobox",
    id: "".concat(cardConfig.id, "_dataSourceIds-combobox"),
    items: formatDataItemsForDropdown(validDataItems),
    itemToString: function itemToString(item) {
      return item === null || item === void 0 ? void 0 : item.text;
    },
    titleText: mergedI18n.dataItemEditorDataItemTitle,
    addToList: false,
    translateWithId: translateWithId,
    shouldFilterItem: function shouldFilterItem(_ref9) {
      var _item$text;
      var item = _ref9.item,
        inputValue = _ref9.inputValue;
      return isEmpty(inputValue) || (item === null || item === void 0 || (_item$text = item.text) === null || _item$text === void 0 || (_item$text = _item$text.toLowerCase()) === null || _item$text === void 0 ? void 0 : _item$text.includes(inputValue === null || inputValue === void 0 ? void 0 : inputValue.toLowerCase()));
    },
    placeholder: mergedI18n.filter
    // clears out the input field after each selection
    ,
    selectedItem: {
      id: '',
      text: ''
    },
    onChange: handleSimpleDataSeriesChange,
    light: true
  }) :
  /*#__PURE__*/
  // Can't select more than one dataItem
  React__default.createElement(Dropdown, {
    id: "".concat(cardConfig.id, "_dataSourceId"),
    direction: "bottom",
    label: mergedI18n.selectDataItem,
    light: true,
    translateWithId: translateWithId,
    title: mergedI18n.selectDataItem,
    titleText: mergedI18n.dataItem,
    items: validDataItems.map(function (_ref10) {
      var dataSourceId = _ref10.dataSourceId;
      return dataSourceId;
    }),
    selectedItem: !isEmpty(dataItemListItems) && !isEmpty((_cardConfig$content5 = cardConfig.content) === null || _cardConfig$content5 === void 0 ? void 0 : _cardConfig$content5.series) ? (_cardConfig$content6 = cardConfig.content) === null || _cardConfig$content6 === void 0 ? void 0 : _cardConfig$content6.series[0].dataItemId : null,
    onChange: function onChange(_ref11) {
      var selectedItem = _ref11.selectedItem;
      var itemWithMetaData = validDataItems === null || validDataItems === void 0 ? void 0 : validDataItems.find(function (_ref12) {
        var dataSourceId = _ref12.dataSourceId;
        return dataSourceId === selectedItem;
      });
      var newCard = handleDataSeriesChange([_objectSpread(_objectSpread({
        id: selectedItem
      }, itemWithMetaData && _objectSpread({}, itemWithMetaData)), {}, {
        dataSourceId: "".concat(selectedItem, "_").concat(v4())
      })], cardConfig, setEditDataSeries);
      setSelectedDataItems([selectedItem]);
      _onChange(newCard);
    }
  })), /*#__PURE__*/React__default.createElement(List, {
    className: "".concat(baseClassName, "--data-item-list"),
    key: "data-item-list".concat(selectedDataItems.length)
    // need to force an empty "empty state"
    ,
    emptyState: /*#__PURE__*/React__default.createElement("div", null),
    title: "",
    items: dataItemListItems
  })), /*#__PURE__*/React__default.createElement(HierarchyDataFormItems, {
    listClassName: "".concat(baseClassName, "--data-item-list"),
    cardConfig: cardConfig,
    hierarchyDataItemListItems: hierarchyDataItemListItems,
    handleHierarchyDataItemChange: handleHierarchyDataItemChange,
    i18n: i18n,
    actions: actions
  }));
};
DataSeriesFormItem.defaultProps = defaultProps;
DataSeriesFormItem.propTypes = propTypes;
DataSeriesFormItem.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "DataSeriesFormItem",
  "props": {
    "cardConfig": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "id": {
            "name": "string",
            "required": false
          },
          "title": {
            "name": "string",
            "required": false
          },
          "size": {
            "name": "string",
            "required": false
          },
          "type": {
            "name": "string",
            "required": false
          },
          "content": {
            "name": "union",
            "value": [{
              "name": "shape",
              "value": {
                "series": {
                  "name": "arrayOf",
                  "value": {
                    "name": "shape",
                    "value": {
                      "label": {
                        "name": "string",
                        "required": false
                      },
                      "dataSourceId": {
                        "name": "string",
                        "required": false
                      },
                      "color": {
                        "name": "string",
                        "required": false
                      }
                    }
                  },
                  "required": false
                },
                "xLabel": {
                  "name": "string",
                  "required": false
                },
                "yLabel": {
                  "name": "string",
                  "required": false
                },
                "unit": {
                  "name": "string",
                  "required": false
                },
                "includeZeroOnXaxis": {
                  "name": "bool",
                  "required": false
                },
                "includeZeroOnYaxis": {
                  "name": "bool",
                  "required": false
                },
                "timeDataSourceId": {
                  "name": "string",
                  "required": false
                },
                "showLegend": {
                  "name": "bool",
                  "required": false
                }
              }
            }, {
              "name": "func"
            }],
            "required": false
          },
          "interval": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "selectedTimeRange": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  dataItemEditorTitle: 'Edit data series',\n  dataItemEditorDataItemTitle: 'Data item',\n  dataItemEditorDataItemLabel: 'Label',\n  dataItemEditorLegendColor: 'Legend color',\n  dataItemEditorSectionTitle: 'Data',\n  dataItemEditorSectionSimpleBarTooltipText:\n    'Display a metric using bars. Plot over time or by a dimension from Group by.',\n  dataItemEditorSectionGroupedBarTooltipText:\n    'Group categories side by side in bars. Show groupings of related metrics or different categories of a single metric.',\n  dataItemEditorSectionStackedBarTooltipText:\n    'Stack bars by categories of a single dimension or into multiple related metrics.',\n  dataItemEditorSectionTimeSeriesTooltipText: 'Plot time series metrics over time.',\n  dataItemEditorSectionValueTooltipText:\n    'Display metric values, dimension values, or alert counts. Select from Data item. ',\n  dataItemEditorSectionCustomTooltipText:\n    'Show or hide alert fields. Choose dimensions to add as extra columns. ',\n  dataItemEditorSectionTooltipLinkText: 'Learn more',\n  selectDataItems: 'Select data items',\n  selectDataItem: 'Select data item',\n  dataItem: 'Data item',\n  edit: 'Edit',\n  remove: 'Remove',\n  customize: 'Customize',\n  filter: 'Filter',\n  clearAllText: 'Clear all',\n  clearSelectionText: 'Clear selection',\n  openMenuText: 'Open menu',\n  closeMenuText: 'Close menu',\n  incrementNumberText: 'Increment number',\n  decrementNumberText: 'Decrement number',\n}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "dataItemEditorTitle": {
            "name": "string",
            "required": false
          },
          "dataItemEditorDataItemTitle": {
            "name": "string",
            "required": false
          },
          "dataItemEditorDataItemLabel": {
            "name": "string",
            "required": false
          },
          "dataItemEditorLegendColor": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionTitle": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionSimpleBarTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionGroupedBarTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionStackedBarTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionTimeSeriesTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionValueTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionCustomTooltipText": {
            "name": "string",
            "required": false
          },
          "dataItemEditorSectionTooltipLinkText": {
            "name": "string",
            "required": false
          },
          "dataSeriesTitle": {
            "name": "string",
            "required": false
          },
          "selectDataItems": {
            "name": "string",
            "required": false
          },
          "selectDataItem": {
            "name": "string",
            "required": false
          },
          "dataItem": {
            "name": "string",
            "required": false
          },
          "edit": {
            "name": "string",
            "required": false
          },
          "remove": {
            "name": "string",
            "required": false
          },
          "customize": {
            "name": "string",
            "required": false
          },
          "clearAllText": {
            "name": "string",
            "required": false
          },
          "clearSelectionText": {
            "name": "string",
            "required": false
          },
          "openMenuText": {
            "name": "string",
            "required": false
          },
          "closeMenuText": {
            "name": "string",
            "required": false
          },
          "incrementNumberText": {
            "name": "string",
            "required": false
          },
          "decrementNumberText": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "shouldUseTranslatedLabels": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "whether to use translated labels in cards",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "getValidDataItems": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "if provided, returns an array of strings which are the dataItems to be allowed\non each card\ngetValidDataItems(card, selectedTimeRange)",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "dataItems": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "an array of dataItems to be included on each card\nthis prop will be ignored if getValidDataItems is defined",
      "type": {
        "name": "custom",
        "raw": "DataItemsPropTypes"
      },
      "required": false
    },
    "selectedDataItems": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "list of dataItem names that have been selected to display on the card",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "string"
        }
      },
      "required": false
    },
    "availableDimensions": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "an object where the keys are available dimensions and the values are the values available for those dimensions\n ex: { manufacturer: ['Rentech', 'GHI Industries'], deviceid: ['73000', '73001', '73002'] }",
      "type": {
        "name": "shape",
        "value": {}
      },
      "required": false
    },
    "isSummaryDashboard": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "dataSeriesItemLinks": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "optional link href's for each card type that will appear in a tooltip",
      "type": {
        "name": "shape",
        "value": {
          "simpleBar": {
            "name": "string",
            "required": false
          },
          "groupedBar": {
            "name": "string",
            "required": false
          },
          "stackedBar": {
            "name": "string",
            "required": false
          },
          "timeSeries": {
            "name": "string",
            "required": false
          },
          "value": {
            "name": "string",
            "required": false
          },
          "custom": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "actions": {
      "defaultValue": {
        "value": "defaultDashboardEditorActionsProps",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "custom",
        "raw": "DashboardEditorActionsPropTypes"
      },
      "required": false
    },
    "onChange": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "setSelectedDataItems": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "translateWithId": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var DataSeriesFormContent = DataSeriesFormItem;

export { DataSeriesFormContent as default, defineCardSpecificTooltip, formatDataItemsForDropdown };
