'use strict';

var _extends = require('@babel/runtime/helpers/extends');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var PropTypes = require('prop-types');
var Settings = require('../../constants/Settings.js');
var react = require('@carbon/react');

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

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

var _excluded = ["itemToString", "testId"];
var iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  /**
   * 'aria-label' of the ListBox component.
   */
  ariaLabel: PropTypes__default.default.string,
  /**
   * Provide a custom className to be applied on the bx--dropdown node
   */
  className: PropTypes__default.default.string,
  /**
   * Specify the direction of the dropdown. Can be either top or bottom.
   */
  direction: PropTypes__default.default.oneOf(['top', 'bottom']),
  /**
   * Disable the control
   */
  disabled: PropTypes__default.default.bool,
  /**
   * Additional props passed to Downshift
   */
  // eslint-disable-next-line react/forbid-prop-types
  downshiftProps: PropTypes__default.default.object,
  /**
   * Provide helper text that is used alongside the control label for
   * additional help
   */
  helperText: PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.node]),
  /**
   * Specify a custom `id`
   */
  id: PropTypes__default.default.string.isRequired,
  /**
   * Allow users to pass in an arbitrary item or a string (in case their items are an array of strings)
   * from their collection that are pre-selected
   */
  initialSelectedItem: PropTypes__default.default.oneOfType([PropTypes__default.default.object, PropTypes__default.default.string]),
  /**
   * Specify if the currently selected value is invalid.
   */
  invalid: PropTypes__default.default.bool,
  /**
   * Message which is displayed if the value is invalid.
   */
  invalidText: PropTypes__default.default.string,
  /**
   * Function to render items as custom components instead of strings.
   * Defaults to null and is overriden by a getter
   */
  itemToElement: PropTypes__default.default.func,
  /**
   * Helper function passed to downshift that allows the library to render a
   * given item to a string label. By default, it extracts the `label` field
   * from a given item to serve as the item label in the list.
   */
  itemToString: PropTypes__default.default.func,
  /**
   * We try to stay as generic as possible here to allow individuals to pass
   * in a collection of whatever kind of data structure they prefer
   */
  // eslint-disable-next-line react/forbid-prop-types
  items: PropTypes__default.default.array.isRequired,
  /**
   * Generic `label` that will be used as the textual representation of what
   * this field is for
   */
  label: PropTypes__default.default.node.isRequired,
  /**
   * `true` to use the light version.
   */
  light: PropTypes__default.default.bool,
  /**
   * `onChange` is a utility for this controlled component to communicate to a
   * consuming component what kind of internal state changes are occuring.
   */
  onChange: PropTypes__default.default.func,
  /**
   * In the case you want to control the dropdown selection entirely.
   */
  selectedItem: PropTypes__default.default.oneOfType([PropTypes__default.default.object, PropTypes__default.default.string]),
  /**
   * Specify the size of the ListBox. Currently supports either `sm`, or `xl` as an option.
   */
  size: PropTypes__default.default.oneOf(['sm', 'xl']),
  /**
   * Provide the title text that will be read by a screen reader when
   * visiting this control
   */
  titleText: PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.node]),
  /**
   * Callback function for translating ListBoxMenuIcon SVG title
   */
  translateWithId: PropTypes__default.default.func,
  /**
   * The dropdown type, `default` or `inline`
   */
  type: PropTypes__default.default.oneOf(['default', 'inline']),
  testId: PropTypes__default.default.string
};
var defaultPropTypes = {
  ariaLabel: undefined,
  className: undefined,
  downshiftProps: undefined,
  initialSelectedItem: null,
  invalid: false,
  invalidText: '',
  onChange: null,
  selectedItem: undefined,
  disabled: false,
  size: undefined,
  translateWithId: function translateWithId() {},
  type: 'default',
  itemToString: null,
  itemToElement: null,
  light: false,
  titleText: '',
  helperText: '',
  direction: 'bottom',
  testId: 'dropdown'
};
var defaultItemToString = function defaultItemToString(item) {
  var content;
  if (typeof item === 'string') {
    content = item;
  } else if (item !== null && item !== void 0 && item.icon) {
    content = /*#__PURE__*/React__default.default.createElement("div", {
      className: "".concat(iotPrefix, "--dropdown__item"),
      title: item.text
    }, /*#__PURE__*/React__default.default.createElement("div", {
      className: "".concat(iotPrefix, "--dropdown__label")
    }, /*#__PURE__*/React__default.default.createElement(item === null || item === void 0 ? void 0 : item.icon), /*#__PURE__*/React__default.default.createElement("div", {
      className: "".concat(iotPrefix, "--dropdown__label__content")
    }, item.text)));
  } else {
    content = item ? item.text : '';
  }
  return content;
};
var Dropdown = function Dropdown(_ref) {
  var itemToString = _ref.itemToString,
    testId = _ref.testId,
    other = _objectWithoutProperties__default.default(_ref, _excluded);
  return /*#__PURE__*/React__default.default.createElement(react.Dropdown, _extends__default.default({
    "data-testid": testId
  }, other, {
    className: "".concat(iotPrefix, "--dropdown"),
    itemToString: itemToString !== null ? itemToString : defaultItemToString
  }));
};
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultPropTypes;
Dropdown.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "Dropdown",
  "props": {
    "ariaLabel": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "'aria-label' of the ListBox component.",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "className": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Provide a custom className to be applied on the bx--dropdown node",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "downshiftProps": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Additional props passed to Downshift",
      "type": {
        "name": "object"
      },
      "required": false
    },
    "initialSelectedItem": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Allow users to pass in an arbitrary item or a string (in case their items are an array of strings)\nfrom their collection that are pre-selected",
      "type": {
        "name": "union",
        "value": [{
          "name": "object"
        }, {
          "name": "string"
        }]
      },
      "required": false
    },
    "invalid": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Specify if the currently selected value is invalid.",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "invalidText": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Message which is displayed if the value is invalid.",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onChange": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "`onChange` is a utility for this controlled component to communicate to a\nconsuming component what kind of internal state changes are occuring.",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "selectedItem": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "In the case you want to control the dropdown selection entirely.",
      "type": {
        "name": "union",
        "value": [{
          "name": "object"
        }, {
          "name": "string"
        }]
      },
      "required": false
    },
    "disabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Disable the control",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "size": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Specify the size of the ListBox. Currently supports either `sm`, or `xl` as an option.",
      "type": {
        "name": "enum",
        "value": [{
          "value": "'sm'",
          "computed": false
        }, {
          "value": "'xl'",
          "computed": false
        }]
      },
      "required": false
    },
    "translateWithId": {
      "defaultValue": {
        "value": "() => {}",
        "computed": false
      },
      "description": "Callback function for translating ListBoxMenuIcon SVG title",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "type": {
      "defaultValue": {
        "value": "'default'",
        "computed": false
      },
      "description": "The dropdown type, `default` or `inline`",
      "type": {
        "name": "enum",
        "value": [{
          "value": "'default'",
          "computed": false
        }, {
          "value": "'inline'",
          "computed": false
        }]
      },
      "required": false
    },
    "itemToString": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Helper function passed to downshift that allows the library to render a\ngiven item to a string label. By default, it extracts the `label` field\nfrom a given item to serve as the item label in the list.",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "itemToElement": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Function to render items as custom components instead of strings.\nDefaults to null and is overriden by a getter",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "light": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "`true` to use the light version.",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "titleText": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Provide the title text that will be read by a screen reader when\nvisiting this control",
      "type": {
        "name": "union",
        "value": [{
          "name": "string"
        }, {
          "name": "node"
        }]
      },
      "required": false
    },
    "helperText": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Provide helper text that is used alongside the control label for\nadditional help",
      "type": {
        "name": "union",
        "value": [{
          "name": "string"
        }, {
          "name": "node"
        }]
      },
      "required": false
    },
    "direction": {
      "defaultValue": {
        "value": "'bottom'",
        "computed": false
      },
      "description": "Specify the direction of the dropdown. Can be either top or bottom.",
      "type": {
        "name": "enum",
        "value": [{
          "value": "'top'",
          "computed": false
        }, {
          "value": "'bottom'",
          "computed": false
        }]
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'dropdown'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "id": {
      "description": "Specify a custom `id`",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "items": {
      "description": "We try to stay as generic as possible here to allow individuals to pass\nin a collection of whatever kind of data structure they prefer",
      "type": {
        "name": "array"
      },
      "required": true
    },
    "label": {
      "description": "Generic `label` that will be used as the textual representation of what\nthis field is for",
      "type": {
        "name": "node"
      },
      "required": true
    }
  }
};
var Dropdown$1 = Dropdown;

module.exports = Dropdown$1;
