'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var TextFieldChip_styles = require('../TextFieldChip/TextFieldChip.styles.js');
var TextField_styles = require('../../TextField.styles.js');
var TextField_tokens = require('../../TextField.tokens.js');
var TextFieldChipNew_styles = require('./TextFieldChipNew.styles.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);

var TextFieldChipNew = function TextFieldChipNew(_ref) {
  var chips = _ref.chips,
    onChipCloseClick = _ref.onChipCloseClick,
    getRef = _ref.getRef,
    handleChipKeyDown = _ref.handleChipKeyDown,
    onChipClear = _ref.onChipClear,
    readOnly = _ref.readOnly,
    chipType = _ref.chipType,
    view = _ref.view,
    _ref$chipClickArea = _ref.chipClickArea,
    chipClickArea = _ref$chipClickArea === void 0 ? 'full' : _ref$chipClickArea,
    _forceChipManipulationWithReadonly = _ref._forceChipManipulationWithReadonly;
  return /*#__PURE__*/React__default.default.createElement(TextField_styles.StyledChips, {
    className: TextField_tokens.classes.chipsWrapper
  }, chips.map(function (_ref2, index) {
    var value = _ref2.value,
      label = _ref2.label,
      disabled = _ref2.disabled;
    var chipId = "".concat(index, "_").concat(value);
    var handleChipClick = function handleChipClick(event) {
      event.stopPropagation();
    };
    var onKeyDownHandle = function onKeyDownHandle(event) {
      if (handleChipKeyDown) {
        handleChipKeyDown(event, chipId, index);
      }
      if (event.key === 'Enter' || event.key === 'Backspace') {
        if (onChipCloseClick) {
          onChipCloseClick({
            value: value,
            label: label,
            disabled: disabled
          });
        }
      }
    };
    var handleCloseClick = function handleCloseClick(event) {
      event.stopPropagation();
      if (onChipCloseClick) {
        onChipCloseClick({
          value: value,
          label: label,
          disabled: disabled
        });
      }
      if (onChipClear) {
        onChipClear(chipId, index);
      }
    };
    return chipType === 'default' ? /*#__PURE__*/React__default.default.createElement(TextFieldChipNew_styles.StyledChip, _rollupPluginBabelHelpers.extends({
      key: chipId,
      ref: function ref(element) {
        return getRef(element, index);
      },
      tabIndex: -1,
      disabled: disabled,
      hasClear: !disabled,
      readOnly: readOnly,
      onKeyDown: onKeyDownHandle,
      onClear: function onClear() {
        return onChipClear(chipId, index);
      },
      text: label,
      view: view,
      chipClickArea: chipClickArea
    }, chipClickArea === 'full' ? {
      onClick: handleCloseClick
    } : {
      onClick: handleChipClick,
      onClickClose: handleCloseClick
    }, {
      // TODO: #1547
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      _forceChipManipulationWithReadonly: _forceChipManipulationWithReadonly
    })) : /*#__PURE__*/React__default.default.createElement(TextFieldChip_styles.TextChip, {
      tabIndex: -1,
      onClick: handleCloseClick,
      onKeyDown: onKeyDownHandle
    }, label);
  }));
};

exports.TextFieldChipNew = TextFieldChipNew;
