'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var isNil = require('../../node_modules/lodash-es/isNil.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.includes.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.string.includes.js');
var React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var classnames = require('classnames');
var CardPropTypes = require('../../constants/CardPropTypes.js');
var Settings = require('../../constants/Settings.js');
var iconsReact = require('@carbon/icons-react');

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

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var iotPrefix = Settings.settings.iotPrefix;
var CardRangePickerPropTypes = {
  /** Optional selected range to pass at the card level */
  timeRange: PropTypes__default.default.string,
  /** Generates the available time range selection options. Each option should include 'this' or 'last'.
   * i.e. { thisWeek: 'This week', lastWeek: 'Last week'}
   */
  timeRangeOptions: CardPropTypes.TimeRangeOptionsPropTypes,
  // eslint-disable-line react/require-default-props
  /** callback to handle interactions with the range picker */
  onCardAction: PropTypes__default.default.func.isRequired,
  /** set of internationalized labels */
  i18n: PropTypes__default.default.objectOf(PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.func, PropTypes__default.default.element])).isRequired,
  cardWidth: PropTypes__default.default.number,
  testId: PropTypes__default.default.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 = React.useState(timeRangeProp),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    timeRange = _useState2[0],
    setTimeRange = _useState2[1];
  var handleTimeRange = React.useCallback(function (value) {
    onCardAction('CHANGE_TIME_RANGE', {
      range: value
    });
    setTimeRange(value);
  }, [setTimeRange, onCardAction]);
  React.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.default.createElement("div", {
    className: "".concat(iotPrefix, "--card--toolbar-date-range-wrapper")
  }, /*#__PURE__*/React__default.default.createElement("div", {
    id: "timeRange",
    className: classnames__default.default("".concat(iotPrefix, "--card--toolbar-timerange-label"), _defineProperty__default.default({}, "".concat(iotPrefix, "--card--toolbar-timerange-label--hidden"), cardWidth < 400))
  }, getTimeRangeText(timeRange, i18n.defaultLabel)), /*#__PURE__*/React__default.default.createElement(react.OverflowMenu, {
    size: "md",
    className: classnames__default.default("".concat(iotPrefix, "--card--toolbar-date-range-action")),
    flipped: true,
    title: i18n.selectTimeRangeLabel,
    iconDescription: i18n.selectTimeRangeLabel,
    menuOptionsClass: "".concat(iotPrefix, "--card--overflow"),
    renderIcon: iconsReact.EventSchedule,
    "data-testid": testId
  }, /*#__PURE__*/React__default.default.createElement(react.OverflowMenuItem, {
    key: "default",
    onClick: function onClick() {
      return handleTimeRange('default');
    },
    itemText: i18n.defaultLabel,
    className: classnames__default.default(_defineProperty__default.default({}, "".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.default.createElement(react.OverflowMenuItem, {
      key: i,
      hasDivider: index === 0,
      onClick: function onClick() {
        return handleTimeRange(i);
      },
      itemText: getTimeRangeText(i),
      className: classnames__default.default(_defineProperty__default.default({}, "".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.default.createElement(react.OverflowMenuItem, {
      key: i,
      hasDivider: index === 0,
      onClick: function onClick() {
        return handleTimeRange(i);
      },
      itemText: getTimeRangeText(i),
      className: classnames__default.default(_defineProperty__default.default({}, "".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;

exports.CardRangePickerPropTypes = CardRangePickerPropTypes;
exports.default = CardRangePicker$1;
