'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Select_tokens = require('../../../../Select.tokens.js');
var Textfield_styles = require('./Textfield.styles.js');
var sizeToIconSize = require('../../../../utils/sizeToIconSize.js');
var getItemId = require('../../../../utils/getItemId.js');

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

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

var Textfield = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var inputWrapperRef = _ref.inputWrapperRef,
    opened = _ref.opened,
    multiselect = _ref.multiselect,
    value = _ref.value,
    label = _ref.label,
    labelPlacement = _ref.labelPlacement,
    keepPlaceholder = _ref.keepPlaceholder,
    placeholder = _ref.placeholder,
    onKeyDown = _ref.onKeyDown,
    size = _ref.size,
    view = _ref.view,
    contentLeft = _ref.contentLeft,
    contentRight = _ref.contentRight,
    helperText = _ref.helperText,
    treeId = _ref.treeId,
    activeDescendantItemValue = _ref.activeDescendantItemValue,
    disabled = _ref.disabled,
    readOnly = _ref.readOnly,
    isTargetAmount = _ref.isTargetAmount,
    valueToItemMap = _ref.valueToItemMap,
    renderValue = _ref.renderValue,
    handleChange = _ref.handleChange,
    chipView = _ref.chipView,
    chipType = _ref.chipType,
    chipClickArea = _ref.chipClickArea,
    requiredProps = _ref.requiredProps,
    hintProps = _ref.hintProps;
  var withArrowInverse = opened ? Select_tokens.classes.arrowInverse : undefined;
  var getValue = function getValue() {
    var _valueToItemMap$get;
    if (multiselect || Array.isArray(value) || !value) {
      return '';
    }
    if (renderValue) {
      return renderValue(valueToItemMap.get(value.toString()) || {
        value: value,
        label: value.toString()
      });
    }
    return ((_valueToItemMap$get = valueToItemMap.get(value.toString())) === null || _valueToItemMap$get === void 0 ? void 0 : _valueToItemMap$get.label) || value.toString();
  };
  var getChips = function getChips() {
    if (multiselect && Array.isArray(value)) {
      if (value.length === 0) return [];
      if (isTargetAmount) {
        return [{
          value: '_removeAll',
          label: "\u0412\u044B\u0431\u0440\u0430\u043D\u043E ".concat(value.length),
          disabled: value.every(function (val) {
            var _valueToItemMap$get2;
            return valueToItemMap === null || valueToItemMap === void 0 || (_valueToItemMap$get2 = valueToItemMap.get(val)) === null || _valueToItemMap$get2 === void 0 ? void 0 : _valueToItemMap$get2.disabled;
          })
        }];
      }
      return value.map(function (value) {
        var _valueToItemMap$get3, _valueToItemMap$get4;
        var currentLabel = renderValue ? renderValue(valueToItemMap.get(value) || {
          value: value,
          label: value.toString()
        }) : ((_valueToItemMap$get3 = valueToItemMap.get(value)) === null || _valueToItemMap$get3 === void 0 ? void 0 : _valueToItemMap$get3.label) || value.toString();
        return {
          value: value,
          label: currentLabel,
          disabled: ((_valueToItemMap$get4 = valueToItemMap.get(value)) === null || _valueToItemMap$get4 === void 0 ? void 0 : _valueToItemMap$get4.disabled) || false
        };
      });
    }
    return [];
  };

  // Обработчик чипов
  var handleChipClick = function handleChipClick(chip) {
    if (readOnly || !Array.isArray(value)) return;
    if (isTargetAmount) {
      // При закрытии чипа в режиме isTargetAmount в value оставляем только disabled-элементы
      handleChange(value.filter(function (val) {
        var _valueToItemMap$get5;
        return valueToItemMap === null || valueToItemMap === void 0 || (_valueToItemMap$get5 = valueToItemMap.get(val)) === null || _valueToItemMap$get5 === void 0 ? void 0 : _valueToItemMap$get5.disabled;
      }), null);
    } else {
      handleChange(value.filter(function (val) {
        return val !== chip.value;
      }), valueToItemMap.get(chip.value) || {
        value: chip.value,
        label: chip.label
      });
    }
  };
  var helperTextStopPropagation = function helperTextStopPropagation(event) {
    event.stopPropagation();
  };
  return /*#__PURE__*/React__default.default.createElement(Textfield_styles.StyledTextField, _rollupPluginBabelHelpers.extends({
    size: size,
    view: view,
    ref: ref,
    inputWrapperRef: inputWrapperRef,
    readOnly: readOnly,
    className: readOnly ? Select_tokens.classes.readOnly : undefined,
    value: getValue(),
    labelPlacement: labelPlacement,
    disabled: disabled,
    label: label,
    keepPlaceholder: keepPlaceholder,
    placeholder: value instanceof Array && value.length ? '' : placeholder,
    contentLeft: contentLeft,
    contentRight: contentRight || contentRight !== undefined ? contentRight : /*#__PURE__*/React__default.default.createElement(Textfield_styles.IconArrowWrapper, {
      disabled: disabled,
      readOnly: readOnly
    }, /*#__PURE__*/React__default.default.createElement(Textfield_styles.StyledArrow, {
      color: "inherit",
      size: sizeToIconSize.sizeToIconSize(size),
      className: withArrowInverse
    })),
    onKeyDown: onKeyDown,
    leftHelper: helperText && /*#__PURE__*/React__default.default.createElement(Textfield_styles.StyledLeftHelper, {
      onClick: helperTextStopPropagation
    }, helperText),
    role: "combobox",
    "aria-autocomplete": "list",
    "aria-controls": "".concat(treeId, "_tree_level_1"),
    "aria-expanded": opened,
    "aria-activedescendant": activeDescendantItemValue ? getItemId.getItemId(treeId, activeDescendantItemValue) : ''
  }, multiselect ? {
    enumerationType: 'chip',
    _chips: getChips(),
    _onChipCloseClick: handleChipClick,
    _chipClickArea: chipClickArea,
    chipType: chipType,
    chipView: chipView
  } : {
    enumerationType: 'plain'
  }, {
    _onEnterDisabled: true // Пропс для отключения обработчика Enter внутри Textfield
    ,
    opened: opened
    // TODO: #1547
    ,
    _forceChipManipulationWithReadonly: !readOnly,
    _interaction_disabled: true
  }, hintProps, requiredProps));
});

exports.Textfield = Textfield;
