"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _view = _interopRequireDefault(require("../view"));
var _form = require("../form");
var _tag = _interopRequireDefault(require("../tag"));
var _hooks = require("../utils/hooks");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var Selectable = _tag.default.Selectable;
var TagSelect = function (props, ref) {
  var _classNames;
  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 = (0, _react.useContext)(_form.ItemContext),
    isInsideForm = _useContext.isInsideForm,
    contextSize = _useContext.size;
  var size = sizeProp || (isInsideForm ? contextSize : 'medium');
  var _useValue = (0, _hooks.useValue)(props, [], {
      fitValue: function fitValue(v) {
        return Array.isArray(v) ? v : [v];
      },
      compare: function compare(a, b) {
        return a.join(',') === b.join(',');
      }
    }),
    value = _useValue[0],
    setValue = _useValue[1],
    isControlled = _useValue[2];
  var handleClick = function (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 = [].concat(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 = prefix + "tag-select";
  return /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix, clsPrefix + "--" + size, className, (_classNames = {}, _classNames[clsPrefix + "--inline"] = inlineMode, _classNames)),
    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__*/(0, _react.createElement)(Selectable, {
      key: v,
      size: size,
      type: tagType,
      disabled: itemDisabled,
      onChange: handleClick.bind(null, v),
      checked: isPreview ? false : value.includes(v)
    }, label);
  }));
};
var _default = /*#__PURE__*/(0, _react.forwardRef)(TagSelect);
exports.default = _default;