"use strict";

exports.__esModule = true;
exports.default = void 0;

require("core-js/modules/es6.function.name");

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _lodash = require("lodash");

var _ = require("../..");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

var DeviceListCall =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(DeviceListCall, _React$PureComponent);

  function DeviceListCall() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
    _this.state = {
      selectedIndex: _this.props.defaultSelected,
      id: _this.props.id || (0, _lodash.uniqueId)('cui-device-list-call-')
    };

    _this.handleSelect = function (e, value, index) {
      var _this$props = _this.props,
          onSelect = _this$props.onSelect,
          devices = _this$props.devices;
      var currentIndex = devices.length < index - 1 ? 0 : index - 1;
      e.preventDefault();
      return _this.setState({
        selectedIndex: currentIndex
      }, function () {
        return onSelect && onSelect(e, currentIndex, value);
      });
    };

    return _this;
  }

  var _proto = DeviceListCall.prototype;

  _proto.render = function render() {
    var _this$props2 = this.props,
        className = _this$props2.className,
        header = _this$props2.header,
        devices = _this$props2.devices;
    var _this$state = this.state,
        selectedIndex = _this$state.selectedIndex,
        id = _this$state.id;

    var getLeftSection = function getLeftSection(deviceType) {
      switch (deviceType) {
        case 'device':
          return _react.default.createElement(_.Icon, {
            name: "spark-board_20"
          });

        default:
          return _react.default.createElement(_.Icon, {
            name: "laptop_20"
          });
      }
    };

    var getRightSection = function getRightSection(device, idx) {
      if (idx === selectedIndex) {
        return _react.default.createElement(_.Icon, {
          name: "check_20",
          color: "blue"
        });
      }
    };

    return _react.default.createElement(_.List, {
      className: className,
      onSelect: this.handleSelect,
      active: selectedIndex
    }, _react.default.createElement(_.ListItemHeader, {
      header: header
    }), devices.map(function (ele, idx) {
      return _react.default.createElement(_.ListItem, {
        key: "device-" + idx,
        id: id,
        value: ele,
        label: ele.name,
        title: ele.title || ele.name
      }, _react.default.createElement(_.ListItemSection, {
        position: "left"
      }, getLeftSection(ele.type)), _react.default.createElement(_.ListItemSection, {
        position: "center"
      }, _react.default.createElement("div", {
        className: "cui-list-item__header"
      }, ele.name)), _react.default.createElement(_.ListItemSection, {
        position: "right"
      }, getRightSection(ele, idx)));
    }));
  };

  return DeviceListCall;
}(_react.default.PureComponent);

DeviceListCall.displayName = 'DeviceListCall';
DeviceListCall.defaultProps = {
  className: '',
  defaultSelected: 0,
  id: null,
  onSelect: null
};
DeviceListCall.propTypes = {
  /** Default Index Value selected */
  defaultSelected: _propTypes.default.number,

  /** required list of devices to show in list */
  devices: _propTypes.default.arrayOf(_propTypes.default.shape({
    name: _propTypes.default.string.isRequired,
    value: _propTypes.default.string,
    type: _propTypes.default.oneOf(['', 'device']),
    title: _propTypes.default.string
  })).isRequired,

  /** HTML Class for associated input */
  className: _propTypes.default.string,

  /** ListItem header */
  header: _propTypes.default.string.isRequired,

  /** HTML ID for associated input */
  id: _propTypes.default.string,

  /** optional function called when list item is selected */
  onSelect: _propTypes.default.func
};
var _default = DeviceListCall;
exports.default = _default;