import _extends from '@babel/runtime/helpers/extends';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _defaultsDeep from '../../node_modules/lodash-es/defaultsDeep.js';
import isEmpty from '../../node_modules/lodash-es/isEmpty.js';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.iterator.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/web.dom-collections.for-each.js';
import 'core-js/modules/web.dom-collections.iterator.js';
import React__default, { useRef } from 'react';
import { ComboChart } from '@carbon/charts-react';
import classnames from 'classnames';
import { CARD_SIZES } from '../../constants/LayoutConstants.js';
import { handleCardVariables, getResizeHandles, increaseSmallCardSize } from '../../utils/cardUtilityFunctions.js';
import Card from '../Card/Card.js';
import { CardPropTypes, ComboChartPropTypes } from '../../constants/CardPropTypes.js';
import { settings } from '../../constants/Settings.js';
import StatefulTable from '../Table/StatefulTable.js';
import { csvDownloadHandler } from '../../utils/componentUtilityFunctions.js';
import { DAYJS_INPUT_FORMATS } from '../../utils/dayjs.js';
import { useChartOptions, useChartData, useTableColumns, useTableData } from './comboChartHelpers.js';

var _excluded = ["className", "children", "content", "title", "size", "values", "availableDimensions", "locale", "i18n", "isExpanded", "isLazyLoading", "isEditable", "isDashboardPreview", "isLoading", "isResizable", "interval", "domainRange", "showTimeInGMT", "timeRange", "tooltipDateFormatPattern", "testID", "testId", "defaultDateFormatPattern"];
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 = _objectSpread(_objectSpread({}, CardPropTypes), ComboChartPropTypes);
var defaultProps = {
  size: CARD_SIZES.MEDIUMWIDE,
  locale: 'en',
  title: '',
  isLoading: false,
  isExpanded: false,
  i18n: {
    noDataLabel: 'No data',
    defaultFilterStringPlaceholdText: 'type and hit enter to apply'
  },
  values: [],
  content: {
    includeZeroOnXaxis: false,
    includeZeroOnYaxis: false,
    showLegend: true
  },
  showTimeInGMT: false,
  domainRange: null,
  tooltipDateFormatPattern: DAYJS_INPUT_FORMATS.SECONDS,
  defaultDateFormatPattern: DAYJS_INPUT_FORMATS.SECONDS
};
var ComboChartCard = function ComboChartCard(_ref) {
  var className = _ref.className,
    children = _ref.children,
    contentProp = _ref.content,
    titleProp = _ref.title,
    sizeProp = _ref.size,
    initialValues = _ref.values;
    _ref.availableDimensions;
    var locale = _ref.locale,
    i18n = _ref.i18n,
    isExpanded = _ref.isExpanded,
    isLazyLoading = _ref.isLazyLoading,
    isEditable = _ref.isEditable;
    _ref.isDashboardPreview;
    var isLoading = _ref.isLoading,
    isResizable = _ref.isResizable,
    interval = _ref.interval;
    _ref.domainRange;
    var showTimeInGMT = _ref.showTimeInGMT,
    timeRange = _ref.timeRange,
    tooltipDateFormatPattern = _ref.tooltipDateFormatPattern,
    testID = _ref.testID,
    testId = _ref.testId,
    defaultDateFormatPattern = _ref.defaultDateFormatPattern,
    others = _objectWithoutProperties(_ref, _excluded);
  var mergedI18n = _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  var noDataLabel = mergedI18n.noDataLabel;
  var _handleCardVariables = handleCardVariables(titleProp, _defaultsDeep({}, contentProp, defaultProps.content), initialValues, others),
    content = _handleCardVariables.content,
    title = _handleCardVariables.title,
    values = _handleCardVariables.values;
  var timeDataSourceId = content.timeDataSourceId;
  var previousTick = useRef();
  var chartOptions = useChartOptions(_objectSpread({
    previousTick: previousTick,
    isEditable: isEditable,
    interval: interval,
    isLoading: isLoading,
    showTimeInGMT: showTimeInGMT,
    tooltipDateFormatPattern: tooltipDateFormatPattern,
    defaultDateFormatPattern: defaultDateFormatPattern,
    chartTitle: content.title,
    values: values,
    i18n: mergedI18n
  }, content));
  var chartData = useChartData(values, {
    series: chartOptions.series,
    timeDataSourceId: timeDataSourceId,
    type: chartOptions.type
  });
  var tableColumns = useTableColumns(values, chartOptions);
  var tableData = useTableData(values, chartOptions);
  return /*#__PURE__*/React__default.createElement(Card, _extends({
    isEmpty: isEmpty(chartData),
    className: classnames(className, "".concat(iotPrefix, "--combo-chart-card")),
    title: title,
    isLazyLoading: isLazyLoading,
    isExpanded: isExpanded,
    isEditable: isEditable,
    isResizable: isResizable,
    i18n: mergedI18n,
    resizeHandles: isResizable ? getResizeHandles(children) : [],
    timeRange: timeRange,
    size: increaseSmallCardSize(sizeProp, 'ComboChartCard'),
    testId: testID || testId,
    locale: locale
  }, others), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--combo-chart-card__container")
  }, /*#__PURE__*/React__default.createElement(ComboChart, {
    key: "combo-chart",
    data: chartData,
    options: chartOptions,
    width: "100%",
    height: "100%"
  }), isExpanded && !isLoading ? /*#__PURE__*/React__default.createElement(StatefulTable, {
    id: "BarChartCard-table",
    className: "".concat(iotPrefix, "--bar-chart-card--stateful-table"),
    columns: tableColumns,
    data: tableData,
    options: {
      hasPagination: true,
      hasSearch: true,
      hasFilter: true
    },
    actions: {
      toolbar: {
        onDownloadCSV: function onDownloadCSV(filteredData) {
          return csvDownloadHandler(filteredData, title);
        }
      }
    },
    view: {
      pagination: {
        pageSize: 10,
        pageSizes: [10, 20, 30]
      },
      toolbar: {
        activeBar: null
      },
      filters: [],
      table: {
        sort: {
          columnId: timeDataSourceId,
          direction: 'DESC'
        },
        emptyState: {
          message: noDataLabel
        }
      }
    },
    i18n: mergedI18n
  }) : null));
};
ComboChartCard.propTypes = propTypes;
ComboChartCard.defaultProps = defaultProps;
ComboChartCard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ComboChartCard",
  "props": {
    "size": {
      "defaultValue": {
        "value": "CARD_SIZES.MEDIUMWIDE",
        "computed": true
      },
      "required": false
    },
    "locale": {
      "defaultValue": {
        "value": "'en'",
        "computed": false
      },
      "required": false
    },
    "title": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "required": false
    },
    "isLoading": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "isExpanded": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  noDataLabel: 'No data',\n  defaultFilterStringPlaceholdText: 'type and hit enter to apply',\n}",
        "computed": false
      },
      "required": false
    },
    "values": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "required": false
    },
    "content": {
      "defaultValue": {
        "value": "{\n  includeZeroOnXaxis: false,\n  includeZeroOnYaxis: false,\n  showLegend: true,\n}",
        "computed": false
      },
      "required": false
    },
    "showTimeInGMT": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "domainRange": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "required": false
    },
    "tooltipDateFormatPattern": {
      "defaultValue": {
        "value": "DAYJS_INPUT_FORMATS.SECONDS",
        "computed": true
      },
      "required": false
    },
    "defaultDateFormatPattern": {
      "defaultValue": {
        "value": "DAYJS_INPUT_FORMATS.SECONDS",
        "computed": true
      },
      "required": false
    }
  },
  "composes": ["../../constants/CardPropTypes"]
};
var ComboChartCard$1 = ComboChartCard;

export { ComboChartCard$1 as default };
