'use strict';

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

var _extends = require('@babel/runtime/helpers/extends');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
require('core-js/modules/es.function.name.js');
require('core-js/modules/es.object.values.js');
var PropTypes = require('prop-types');
var React = require('react');
var classnames = require('classnames');
var react = require('@carbon/react');
var Settings = require('../../constants/Settings.js');

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

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

var _excluded = ["className", "index", "name", "renderIcon", "onClick", "onKeyDown", "selected", "size", "text", "testId", "children", "disabled"];
var prefix = Settings.settings.prefix,
  iotPrefix = Settings.settings.iotPrefix;
var ICON_SWITCH_SIZES = {
  small: 'sm',
  default: 'md',
  large: 'lg'
};
var IconSwitch = /*#__PURE__*/React__default.default.forwardRef(function (props, ref) {
  var className = props.className,
    index = props.index,
    name = props.name,
    renderIcon = props.renderIcon,
    onClick = props.onClick,
    onKeyDown = props.onKeyDown,
    selected = props.selected;
    props.size;
    var text = props.text,
    testId = props.testId,
    children = props.children,
    disabled = props.disabled,
    other = _objectWithoutProperties__default.default(props, _excluded);
  var handleClick = function handleClick(e) {
    e.preventDefault();
    onClick({
      index: index,
      name: name,
      text: text
    });
  };
  var handleKeyDown = function handleKeyDown(e) {
    var key = e.key || e.which;
    if (key === 'Enter' || key === 13 || key === ' ' || key === 32) {
      onKeyDown({
        index: index,
        name: name,
        text: text
      });
    }
  };
  var classes = classnames__default.default(className, "".concat(prefix, "--content-switcher-btn"), // Class from carbon
  "".concat(iotPrefix, "--icon-switch"), _defineProperty__default.default({}, "".concat(prefix, "--content-switcher--selected"), selected));
  var iconButtonClasses = classnames__default.default("".concat(prefix, "--content-switcher-popover__wrapper"), // Class from carbon
  _defineProperty__default.default(_defineProperty__default.default({}, "".concat(prefix, "--content-switcher-popover--selected"), selected), "".concat(prefix, "--content-switcher-popover--disabled"), disabled));
  var commonProps = {
    onClick: handleClick,
    onKeyDown: handleKeyDown,
    className: classes,
    disabled: disabled
  };
  return /*#__PURE__*/React__default.default.createElement(react.Button, _extends__default.default({
    ref: ref,
    renderIcon: renderIcon,
    iconDescription: text,
    kind: "secondary",
    hasIconOnly: true,
    tooltipPosition: "top",
    "data-testid": testId,
    wrapperClasses: iconButtonClasses
  }, other, commonProps), children);
});
IconSwitch.propTypes = {
  /**
   * Specify an optional className to be added to your Switch
   */
  className: PropTypes__default.default.string,
  /**
   * The index of your Switch in your ContentSwitcher that is used for event handlers.
   * Reserved for usage in ContentSwitcher
   */
  index: PropTypes__default.default.number.isRequired,
  /**
   * Provide the name of your Switch that is used for event handlers
   */
  name: PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.number]),
  /**
   * A handler that is invoked when a user clicks on the control.
   * Reserved for usage in ContentSwitcher
   */
  onClick: PropTypes__default.default.func,
  /**
   * A handler that is invoked on the key down event for the control.
   * Reserved for usage in ContentSwitcher
   */
  onKeyDown: PropTypes__default.default.func,
  /**
   * Whether your Switch is selected. Reserved for usage in ContentSwitcher
   */
  selected: PropTypes__default.default.bool,
  /**
   * The tooltip text which will display when hovering over the switch
   */
  text: PropTypes__default.default.string.isRequired,
  /**
   * @deprecated The `size` prop for `IconSwitch` has been deprecated. This should now be set using the `size` prop
   * in the ContentSwitcher component. The options are 'sm', 'md', 'lg'.
   */
  size: PropTypes__default.default.oneOf(Object.values(ICON_SWITCH_SIZES)),
  /**
   * Define the icon render to be rendered.
   * Can be a React component class
   */
  renderIcon: PropTypes__default.default.oneOfType([PropTypes__default.default.func, PropTypes__default.default.object]).isRequired,
  testId: PropTypes__default.default.string,
  /** The button should be disabled */
  disabled: PropTypes__default.default.bool,
  children: PropTypes__default.default.node
};
IconSwitch.defaultProps = {
  className: undefined,
  selected: false,
  name: '',
  size: 'sm',
  onClick: undefined,
  onKeyDown: undefined,
  testId: 'icon-switch',
  disabled: false,
  children: undefined
};
IconSwitch.displayName = 'IconSwitch';
IconSwitch.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "IconSwitch",
  "props": {
    "className": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Specify an optional className to be added to your Switch",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "selected": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Whether your Switch is selected. Reserved for usage in ContentSwitcher",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "name": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "Provide the name of your Switch that is used for event handlers",
      "type": {
        "name": "union",
        "value": [{
          "name": "string"
        }, {
          "name": "number"
        }]
      },
      "required": false
    },
    "size": {
      "defaultValue": {
        "value": "'sm'",
        "computed": false
      },
      "description": "@deprecated The `size` prop for `IconSwitch` has been deprecated. This should now be set using the `size` prop\nin the ContentSwitcher component. The options are 'sm', 'md', 'lg'.",
      "type": {
        "name": "enum",
        "value": [{
          "value": "\"md\"",
          "computed": false
        }, {
          "value": "\"lg\"",
          "computed": false
        }, {
          "value": "\"sm\"",
          "computed": false
        }]
      },
      "required": false
    },
    "onClick": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "A handler that is invoked when a user clicks on the control.\nReserved for usage in ContentSwitcher",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "onKeyDown": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "A handler that is invoked on the key down event for the control.\nReserved for usage in ContentSwitcher",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'icon-switch'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "disabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "The button should be disabled",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "children": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "index": {
      "description": "The index of your Switch in your ContentSwitcher that is used for event handlers.\nReserved for usage in ContentSwitcher",
      "type": {
        "name": "number"
      },
      "required": true
    },
    "text": {
      "description": "The tooltip text which will display when hovering over the switch",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "renderIcon": {
      "description": "Define the icon render to be rendered.\nCan be a React component class",
      "type": {
        "name": "union",
        "value": [{
          "name": "func"
        }, {
          "name": "object"
        }]
      },
      "required": true
    }
  }
};
var IconSwitch$1 = IconSwitch;

exports.ICON_SWITCH_SIZES = ICON_SWITCH_SIZES;
exports.default = IconSwitch$1;
