import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import isNil from '../../node_modules/lodash-es/isNil.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.includes.js';
import 'core-js/modules/es.array.map.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 React__default, { useState, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { OverflowMenu, OverflowMenuItem } from '@carbon/react';
import classnames from 'classnames';
import { TimeRangeOptionsPropTypes } from '../../constants/CardPropTypes.js';
import { settings } from '../../constants/Settings.js';
import { EventSchedule } from '@carbon/icons-react';

var iotPrefix = settings.iotPrefix;
var CardRangePickerPropTypes = {
  /** Optional selected range to pass at the card level */
  timeRange: PropTypes.string,
  /** Generates the available time range selection options. Each option should include 'this' or 'last'.
   * i.e. { thisWeek: 'This week', lastWeek: 'Last week'}
   */
  timeRangeOptions: TimeRangeOptionsPropTypes,
  // eslint-disable-line react/require-default-props
  /** callback to handle interactions with the range picker */
  onCardAction: PropTypes.func.isRequired,
  /** set of internationalized labels */
  i18n: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.element])).isRequired,
  cardWidth: PropTypes.number,
  testId: PropTypes.string
};
var defaultProps = {
  timeRange: null,
  cardWidth: undefined,
  testId: 'card-range-picker'
};
var CardRangePicker = function CardRangePicker(_ref) {
  var i18n = _ref.i18n,
    timeRangeProp = _ref.timeRange,
    timeRangeOptions = _ref.timeRangeOptions,
    onCardAction = _ref.onCardAction,
    cardWidth = _ref.cardWidth,
    testId = _ref.testId;
  var _useState = useState(timeRangeProp),
    _useState2 = _slicedToArray(_useState, 2),
    timeRange = _useState2[0],
    setTimeRange = _useState2[1];
  var handleTimeRange = useCallback(function (value) {
    onCardAction('CHANGE_TIME_RANGE', {
      range: value
    });
    setTimeRange(value);
  }, [setTimeRange, onCardAction]);
  useEffect(function () {
    setTimeRange(timeRangeProp);
  }, [timeRangeProp]);
  var getTimeRangeText = function getTimeRangeText(key) {
    var _timeRangeOptions$key;
    var defaultText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    return typeof timeRangeOptions[key] === 'string' ? timeRangeOptions[key] : ((_timeRangeOptions$key = timeRangeOptions[key]) === null || _timeRangeOptions$key === void 0 ? void 0 : _timeRangeOptions$key.label) || defaultText;
  };
  return /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--card--toolbar-date-range-wrapper")
  }, /*#__PURE__*/React__default.createElement("div", {
    id: "timeRange",
    className: classnames("".concat(iotPrefix, "--card--toolbar-timerange-label"), _defineProperty({}, "".concat(iotPrefix, "--card--toolbar-timerange-label--hidden"), cardWidth < 400))
  }, getTimeRangeText(timeRange, i18n.defaultLabel)), /*#__PURE__*/React__default.createElement(OverflowMenu, {
    size: "md",
    className: classnames("".concat(iotPrefix, "--card--toolbar-date-range-action")),
    flipped: true,
    title: i18n.selectTimeRangeLabel,
    iconDescription: i18n.selectTimeRangeLabel,
    menuOptionsClass: "".concat(iotPrefix, "--card--overflow"),
    renderIcon: EventSchedule,
    "data-testid": testId
  }, /*#__PURE__*/React__default.createElement(OverflowMenuItem, {
    key: "default",
    onClick: function onClick() {
      return handleTimeRange('default');
    },
    itemText: i18n.defaultLabel,
    className: classnames(_defineProperty({}, "".concat(iotPrefix, "--card--overflow-menuitem-active"), timeRange === '' || isNil(timeRange)))
  }), Object.keys(timeRangeOptions).filter(function (i) {
    return i.includes('last');
  }).map(function (i, index) {
    return /*#__PURE__*/React__default.createElement(OverflowMenuItem, {
      key: i,
      hasDivider: index === 0,
      onClick: function onClick() {
        return handleTimeRange(i);
      },
      itemText: getTimeRangeText(i),
      className: classnames(_defineProperty({}, "".concat(iotPrefix, "--card--overflow-menuitem-active"), timeRange === i))
    });
  }), Object.keys(timeRangeOptions).filter(function (i) {
    return i.includes('this');
  }).map(function (i, index) {
    return /*#__PURE__*/React__default.createElement(OverflowMenuItem, {
      key: i,
      hasDivider: index === 0,
      onClick: function onClick() {
        return handleTimeRange(i);
      },
      itemText: getTimeRangeText(i),
      className: classnames(_defineProperty({}, "".concat(iotPrefix, "--card--overflow-menuitem-active"), timeRange === i))
    });
  })));
};
CardRangePicker.propTypes = CardRangePickerPropTypes;
CardRangePicker.defaultProps = defaultProps;
CardRangePicker.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "CardRangePicker",
  "props": {
    "timeRange": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Optional selected range to pass at the card level",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "cardWidth": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'card-range-picker'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "timeRangeOptions": {
      "description": "Generates the available time range selection options. Each option should include 'this' or 'last'.\ni.e. { thisWeek: 'This week', lastWeek: 'Last week'}",
      "type": {
        "name": "custom",
        "raw": "TimeRangeOptionsPropTypes"
      },
      "required": false
    },
    "onCardAction": {
      "description": "callback to handle interactions with the range picker",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "i18n": {
      "description": "set of internationalized labels",
      "type": {
        "name": "objectOf",
        "value": {
          "name": "union",
          "value": [{
            "name": "string"
          }, {
            "name": "func"
          }, {
            "name": "element"
          }]
        }
      },
      "required": true
    }
  }
};
var CardRangePicker$1 = CardRangePicker;

export { CardRangePickerPropTypes, CardRangePicker$1 as default };
