"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _view = _interopRequireDefault(require("../view"));
var _icon = _interopRequireDefault(require("../icon"));
var _createChildren = _interopRequireDefault(require("./create-children"));
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var getInitCheckedValue = function (props) {
  if ('checked' in props) {
    return props.checked;
  } else if (props.defaultChecked) {
    return props.defaultChecked;
  } else {
    return false;
  }
};
var SelectableTag = function (props, ref) {
  var _classNames, _classNames2;
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    _props$size = props.size,
    size = _props$size === void 0 ? 'medium' : _props$size,
    _props$type = props.type,
    type = _props$type === void 0 ? 'normal' : _props$type,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    _props$checked = props.checked,
    checked = _props$checked === void 0 ? false : _props$checked,
    _props$defaultChecked = props.defaultChecked,
    defaultChecked = _props$defaultChecked === void 0 ? false : _props$defaultChecked,
    _props$onChange = props.onChange,
    onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
    _props$style = props.style,
    style = _props$style === void 0 ? {} : _props$style,
    children = props.children,
    className = props.className,
    others = (0, _tslib.__rest)(props, ["prefix", "size", "type", "disabled", "checked", "defaultChecked", "onChange", "style", "children", "className"]);
  var shape = 'selectable';
  var initCheckedValue = getInitCheckedValue(props);
  var _useState = (0, _react.useState)(initCheckedValue),
    hasChecked = _useState[0],
    setChecked = _useState[1];
  var cssType = type === 'normal' ? 'outline' : 'solid';
  var tagCls = (0, _classnames.default)(className, prefix + "tag", prefix + "tag--" + size, prefix + "tag--" + shape + "-" + cssType, (_classNames = {}, _classNames[prefix + "tag--" + shape + "-" + cssType + "-selected-disabled"] = disabled && hasChecked, _classNames[prefix + "tag--" + shape + "-" + cssType + "-disabled"] = disabled, _classNames[prefix + "tag--" + shape + "-" + cssType + "-selected"] = hasChecked, _classNames));
  var selectCls = (0, _classnames.default)(prefix + "tag--selectable-select");
  var selectIconCls = (0, _classnames.default)(prefix + "tag-icon--" + size + "-select", (_classNames2 = {}, _classNames2[prefix + "tag--" + shape + "-" + cssType + "-icon-selected-disabled"] = disabled && hasChecked, _classNames2));
  var handleClick = function (e) {
    if (disabled) {
      return;
    }
    if (onChange) {
      onChange(!hasChecked, e);
    }
    if (!('checked' in props)) {
      setChecked(!hasChecked);
    }
  };
  (0, _react.useEffect)(function () {
    if ('checked' in props) {
      setChecked(checked);
    }
  }, [checked]);
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    ref: ref,
    className: tagCls,
    onClick: handleClick,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(props.style)
  }), (0, _createChildren.default)(children, {
    textCls: prefix + "tag-font--" + size,
    iconCls: prefix + "tag-font--" + size
  }, prefix, size), type === 'normal' && hasChecked ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: selectCls,
    onChange: handleClick
  }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    name: "select",
    className: selectIconCls
  })) : null);
};
SelectableTag.displayName = 'SelectableTag';
var _default = /*#__PURE__*/(0, _react.forwardRef)(SelectableTag);
exports.default = _default;