'use strict';

require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var omit = require('../../../../../node_modules/lodash-es/omit.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.object.keys.js');
var React = require('react');
var PropTypes = require('prop-types');
var Settings = require('../../../../../constants/Settings.js');
var LayoutConstants = require('../../../../../constants/LayoutConstants.js');
var react = require('@carbon/react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

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__default.default(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.settings.iotPrefix;
var propTypes = {
  /* card value */
  cardConfig: PropTypes__default.default.shape({
    id: PropTypes__default.default.string,
    title: PropTypes__default.default.string,
    size: PropTypes__default.default.string,
    type: PropTypes__default.default.string,
    content: PropTypes__default.default.shape({
      series: PropTypes__default.default.arrayOf(PropTypes__default.default.shape({
        label: PropTypes__default.default.string,
        dataSourceId: PropTypes__default.default.string,
        color: PropTypes__default.default.string
      })),
      xLabel: PropTypes__default.default.string,
      yLabel: PropTypes__default.default.string,
      unit: PropTypes__default.default.string,
      includeZeroOnXaxis: PropTypes__default.default.bool,
      includeZeroOnYaxis: PropTypes__default.default.bool,
      timeDataSourceId: PropTypes__default.default.string,
      showLegend: PropTypes__default.default.bool
    }),
    interval: PropTypes__default.default.string
  }),
  /* callback when image input value changes (File object) */
  onChange: PropTypes__default.default.func.isRequired,
  i18n: PropTypes__default.default.shape({
    dataItemEditorTitle: PropTypes__default.default.string,
    dataItemEditorDataItemTitle: PropTypes__default.default.string,
    dataItemEditorDataItemLabel: PropTypes__default.default.string,
    dataItemEditorLegendColor: PropTypes__default.default.string,
    dataSeriesTitle: PropTypes__default.default.string,
    selectDataItems: PropTypes__default.default.string,
    selectDataItem: PropTypes__default.default.string,
    dataItem: PropTypes__default.default.string,
    edit: PropTypes__default.default.string,
    customize: PropTypes__default.default.string,
    selectGroupBy: PropTypes__default.default.string,
    selectCategory: PropTypes__default.default.string,
    groupBy: PropTypes__default.default.string,
    subGroup: PropTypes__default.default.string,
    timeInterval: PropTypes__default.default.string
  }),
  translateWithId: PropTypes__default.default.func.isRequired,
  /** 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__default.default.shape({})
};
var defaultProps = {
  cardConfig: {},
  i18n: {
    dataItemEditorTitle: 'Edit data series',
    dataItemEditorDataItemTitle: 'Data item',
    dataItemEditorDataItemLabel: 'Label',
    dataItemEditorLegendColor: 'Legend color',
    dataItemEditorSectionTitle: 'Data',
    selectDataItems: 'Select data items',
    selectDataItem: 'Select data item',
    dataItem: 'Data item',
    edit: 'Edit',
    customize: 'Customize',
    selectGroupBy: 'Select a group by',
    selectCategory: 'Select a category',
    groupBy: 'Group by',
    subGroup: 'Sub-group',
    timeInterval: 'Time interval'
  },
  availableDimensions: {}
};
var BarChartDataSeriesContent = function BarChartDataSeriesContent(_ref) {
  var _cardConfig$content, _cardConfig$content2, _cardConfig$content3, _Object$keys, _cardConfig$content4;
  var _ref$cardConfig = _ref.cardConfig,
    cardConfig = _ref$cardConfig === void 0 ? {} : _ref$cardConfig,
    _onChange = _ref.onChange,
    availableDimensions = _ref.availableDimensions,
    i18n = _ref.i18n,
    translateWithId = _ref.translateWithId;
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var baseClassName = "".concat(iotPrefix, "--card-edit-form");
  return /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement(react.Dropdown, {
    id: "".concat(cardConfig.id, "_group-by"),
    direction: "bottom",
    label: mergedI18n.selectGroupBy,
    title: mergedI18n.selectGroupBy,
    light: true,
    translateWithId: translateWithId,
    titleText: mergedI18n.groupBy,
    items: [].concat(_toConsumableArray__default.default(cardConfig.content.type !== LayoutConstants.BAR_CHART_TYPES.GROUPED ? [mergedI18n.timeInterval] : []), _toConsumableArray__default.default(Object.keys(availableDimensions))),
    selectedItem: (_cardConfig$content = cardConfig.content) !== null && _cardConfig$content !== void 0 && _cardConfig$content.timeDataSourceId ? mergedI18n.timeInterval : cardConfig.content.type !== LayoutConstants.BAR_CHART_TYPES.GROUPED ? ((_cardConfig$content2 = cardConfig.content) === null || _cardConfig$content2 === void 0 ? void 0 : _cardConfig$content2.categoryDataSourceId) || mergedI18n.timeInterval : (_cardConfig$content3 = cardConfig.content) === null || _cardConfig$content3 === void 0 ? void 0 : _cardConfig$content3.categoryDataSourceId,
    onChange: function onChange(_ref2) {
      var selectedItem = _ref2.selectedItem;
      if (selectedItem !== mergedI18n.timeInterval) {
        _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
          content: _objectSpread(_objectSpread({}, omit(cardConfig.content, 'timeDataSourceId')), {}, {
            categoryDataSourceId: selectedItem
          }),
          dataSource: _objectSpread(_objectSpread({}, cardConfig.dataSource), {}, {
            groupBy: [selectedItem]
          })
        }));
      } else {
        _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
          content: _objectSpread(_objectSpread({}, omit(cardConfig.content, 'categoryDataSourceId')), {}, {
            timeDataSourceId: 'timestamp'
          }),
          dataSource: _objectSpread({}, omit(cardConfig.dataSource, 'groupBy'))
        }));
      }
    }
  })), cardConfig.content.timeDataSourceId && ((_Object$keys = Object.keys(availableDimensions)) === null || _Object$keys === void 0 ? void 0 : _Object$keys.length) > 0 && cardConfig.content.type === LayoutConstants.BAR_CHART_TYPES.STACKED && cardConfig.content.series.length <= 1 ? /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(baseClassName, "--input")
  }, /*#__PURE__*/React__default.default.createElement(react.Dropdown, {
    id: "".concat(cardConfig.id, "_sub-group-by"),
    direction: "bottom",
    label: mergedI18n.selectCategory,
    title: mergedI18n.selectCategory,
    light: true,
    translateWithId: translateWithId,
    titleText: mergedI18n.subGroup,
    items: Object.keys(availableDimensions),
    selectedItem: (_cardConfig$content4 = cardConfig.content) === null || _cardConfig$content4 === void 0 ? void 0 : _cardConfig$content4.categoryDataSourceId,
    onChange: function onChange(_ref3) {
      var selectedItem = _ref3.selectedItem;
      _onChange(_objectSpread(_objectSpread({}, cardConfig), {}, {
        content: _objectSpread(_objectSpread({}, cardConfig.content), {}, {
          timeDataSourceId: 'timestamp',
          categoryDataSourceId: selectedItem
        }),
        dataSource: _objectSpread(_objectSpread({}, cardConfig.dataSource), {}, {
          groupBy: [selectedItem]
        })
      }));
    }
  })) : null);
};
BarChartDataSeriesContent.defaultProps = defaultProps;
BarChartDataSeriesContent.propTypes = propTypes;
BarChartDataSeriesContent.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "BarChartDataSeriesContent",
  "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": "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
              }
            },
            "required": false
          },
          "interval": {
            "name": "string",
            "required": false
          }
        }
      },
      "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  selectDataItems: 'Select data items',\n  selectDataItem: 'Select data item',\n  dataItem: 'Data item',\n  edit: 'Edit',\n  customize: 'Customize',\n\n  selectGroupBy: 'Select a group by',\n  selectCategory: 'Select a category',\n  groupBy: 'Group by',\n  subGroup: 'Sub-group',\n  timeInterval: 'Time interval',\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
          },
          "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
          },
          "customize": {
            "name": "string",
            "required": false
          },
          "selectGroupBy": {
            "name": "string",
            "required": false
          },
          "selectCategory": {
            "name": "string",
            "required": false
          },
          "groupBy": {
            "name": "string",
            "required": false
          },
          "subGroup": {
            "name": "string",
            "required": false
          },
          "timeInterval": {
            "name": "string",
            "required": false
          }
        }
      },
      "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
    },
    "onChange": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "translateWithId": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var BarChartDataSeriesContent$1 = BarChartDataSeriesContent;

module.exports = BarChartDataSeriesContent$1;
