function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import classNames from 'classnames';
import React, { createElement, forwardRef, useContext } from "react";
import { ItemContext } from '../form';
import Tag from '../tag';
import { useValue } from '../utils/hooks';
import View from '../view';
var Selectable = Tag.Selectable;

var TagSelect = function TagSelect(props, ref) {
  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      isPreview = props.isPreview,
      className = props.className,
      _props$dataSource = props.dataSource,
      dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
      _props$mode = props.mode,
      mode = _props$mode === void 0 ? 'single' : _props$mode,
      onChange = props.onChange,
      sizeProp = props.size,
      disabled = props.disabled,
      _props$inlineMode = props.inlineMode,
      inlineMode = _props$inlineMode === void 0 ? false : _props$inlineMode;

  var _useContext = useContext(ItemContext),
      isInsideForm = _useContext.isInsideForm,
      contextSize = _useContext.size;

  var size = sizeProp || (isInsideForm ? contextSize : 'medium');

  var _useValue = useValue(props, [], {
    fitValue: function fitValue(v) {
      return Array.isArray(v) ? v : [v];
    },
    compare: function compare(a, b) {
      return a.join(',') === b.join(',');
    }
  }),
      _useValue2 = _slicedToArray(_useValue, 3),
      value = _useValue2[0],
      setValue = _useValue2[1],
      isControlled = _useValue2[2];

  var handleClick = function handleClick(v) {
    if (isPreview) {
      return;
    }

    if (mode === 'single') {
      var hasChecked = value.includes(v);

      if (isControlled) {
        if (onChange) {
          onChange(hasChecked ? undefined : v);
        }
      } else {
        setValue(hasChecked ? [] : [v]);
      }
    } else {
      var newValue = _toConsumableArray(value);

      if (newValue.includes(v)) {
        newValue.splice(newValue.indexOf(v), 1);
      } else {
        newValue.push(v);
      }

      if (isControlled) {
        if (onChange) {
          onChange(newValue);
        }
      } else {
        setValue(newValue);
      }
    }
  };

  var tagType;

  if (isPreview) {
    tagType = 'primary';
  } else {
    tagType = mode === 'single' ? 'primary' : 'normal';
  }

  var clsPrefix = "".concat(prefix, "tag-select");
  return /*#__PURE__*/React.createElement(View, {
    className: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(size), className, _defineProperty({}, "".concat(clsPrefix, "--inline"), inlineMode)),
    ref: ref
  }, dataSource.map(function (it) {
    var v = _typeof(it) === 'object' ? it.value : it;
    var label = _typeof(it) === 'object' ? it.label : it;
    var itemDisabled = disabled || (_typeof(it) === 'object' ? it.disabled : false);
    return /*#__PURE__*/React.createElement(Selectable, {
      key: v,
      size: size,
      type: tagType,
      disabled: itemDisabled,
      onChange: handleClick.bind(null, v),
      checked: isPreview ? false : value.includes(v)
    }, label);
  }));
};

export default /*#__PURE__*/forwardRef(TagSelect);