'use strict';

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
require('core-js/modules/es.function.name.js');
var React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var editorUtils = require('../DashboardEditor/editorUtils.js');
var Settings = require('../../constants/Settings.js');
var deprecate = require('../../internal/deprecate.js');
var SharedPropTypes = require('../../constants/SharedPropTypes.js');

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

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

var iotPrefix = Settings.settings.iotPrefix;
var iconProptype = PropTypes__default.default.shape({
  carbonIcon: SharedPropTypes.CarbonIconPropType,
  name: PropTypes__default.default.string
});
var propTypes = {
  /** Array of icons to be shown */
  icons: PropTypes__default.default.arrayOf(iconProptype),
  /** The title of the Dropdown, defaults to 'Icon' */
  titleText: PropTypes__default.default.string,
  /** Required Id string */
  id: PropTypes__default.default.string.isRequired,
  /** True if the light theme is to be used, defaults to false */
  light: PropTypes__default.default.bool,
  /** Callback for when any of the Dropdown icon value changes */
  onChange: PropTypes__default.default.func.isRequired,
  /** Callback to translate common icons */
  translateWithId: PropTypes__default.default.func,
  /** The selected icon, use to set initial icon */
  selectedIcon: iconProptype,
  // TODO: remove deprecated 'testID' in v3
  // eslint-disable-next-line react/require-default-props
  testID: deprecate(PropTypes__default.default.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  /** Id used if needed for testing */
  testId: PropTypes__default.default.string
};
var defaultProps = {
  icons: editorUtils.validThresholdIcons,
  light: false,
  selectedIcon: undefined,
  testId: undefined,
  titleText: '',
  translateWithId: undefined
};
var SimpleIconDropdown = function SimpleIconDropdown(_ref) {
  var icons = _ref.icons,
    id = _ref.id,
    light = _ref.light,
    _onChange = _ref.onChange,
    selectedIconProp = _ref.selectedIcon,
    titleText = _ref.titleText,
    testID = _ref.testID,
    testId = _ref.testId,
    translateWithId = _ref.translateWithId;
  var _useState = React.useState(selectedIconProp),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    selectedIcon = _useState2[0],
    setSelectedIcon = _useState2[1];
  var renderIconItem = function renderIconItem(item) {
    return /*#__PURE__*/React__default.default.createElement("div", {
      title: item.name,
      style: {
        color: item.color || 'unset'
      },
      className: "".concat(iotPrefix, "--icon-dropdown__item")
    }, /*#__PURE__*/React__default.default.createElement("div", {
      className: "".concat(iotPrefix, "--icon-dropdown__item-border")
    }, item.carbonIcon));
  };
  return /*#__PURE__*/React__default.default.createElement(react.Dropdown, {
    className: "".concat(iotPrefix, "--icon-dropdown"),
    id: id,
    itemToElement: renderIconItem,
    renderSelectedItem: renderIconItem,
    itemToString: function itemToString(item) {
      return item.name;
    },
    items: icons,
    label: "",
    light: light,
    onChange: function onChange(_ref2) {
      var selectedItem = _ref2.selectedItem;
      setSelectedIcon(selectedItem);
      _onChange({
        icon: selectedItem
      });
    },
    translateWithId: translateWithId,
    selectedItem: selectedIcon || icons[0],
    titleText: titleText,
    type: "default"
    // TODO: remove deprecated 'testID' in v3
    ,
    "data-testid": testID || testId,
    autoAlign: true
  });
};
SimpleIconDropdown.propTypes = propTypes;
SimpleIconDropdown.defaultProps = defaultProps;
SimpleIconDropdown.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "SimpleIconDropdown",
  "props": {
    "icons": {
      "defaultValue": {
        "value": "validThresholdIcons",
        "computed": true
      },
      "description": "Array of icons to be shown",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "custom",
          "raw": "iconProptype"
        }
      },
      "required": false
    },
    "light": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "True if the light theme is to be used, defaults to false",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "selectedIcon": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "The selected icon, use to set initial icon",
      "type": {
        "name": "custom",
        "raw": "iconProptype"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Id used if needed for testing",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "titleText": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "The title of the Dropdown, defaults to 'Icon'",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "translateWithId": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Callback to translate common icons",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "id": {
      "description": "Required Id string",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "onChange": {
      "description": "Callback for when any of the Dropdown icon value changes",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "testID": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "deprecate(\n  PropTypes.string,\n  `The 'testID' prop has been deprecated. Please use 'testId' instead.`\n)"
      },
      "required": false
    }
  }
};
var SimpleIconDropdown$1 = SimpleIconDropdown;

module.exports = SimpleIconDropdown$1;
