"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.CheckBox = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _get3 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

var _react = _interopRequireDefault(require("react"));

var _mobx = require("mobx");

var _mobxReact = require("mobx-react");

var _enum = require("../field/enum");

var _Radio2 = require("../radio/Radio");

var _icon = _interopRequireDefault(require("../icon"));

var _singleton = require("../tooltip/singleton");

var _enum2 = require("../data-set/enum");

var _autobind = _interopRequireDefault(require("../_util/autobind"));

var CheckBox = /*#__PURE__*/function (_Radio) {
  (0, _inherits2["default"])(CheckBox, _Radio);

  var _super = (0, _createSuper2["default"])(CheckBox);

  function CheckBox(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, CheckBox);
    _this = _super.call(this, props, context);
    _this.type = 'checkbox';
    (0, _mobx.runInAction)(function () {
      _this.value = _this.props.defaultChecked ? _this.checkedValue : _this.unCheckedValue;
    });
    return _this;
  }

  (0, _createClass2["default"])(CheckBox, [{
    key: "unCheckedValue",
    get: function get() {
      var unCheckedValue = this.props.unCheckedValue;

      if (unCheckedValue !== undefined) {
        return unCheckedValue;
      }

      var field = this.field;

      if (field) {
        return field.get(_enum2.BooleanValue.falseValue);
      }

      return false;
    }
  }, {
    key: "checkedValue",
    get: function get() {
      var value = this.props.value;

      if (value !== undefined) {
        return value;
      }

      var field = this.field;

      if (field) {
        return field.get(_enum2.BooleanValue.trueValue);
      }

      return true;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      (0, _get3["default"])((0, _getPrototypeOf2["default"])(CheckBox.prototype), "componentWillUnmount", this).call(this);

      if (this.showHelp === _enum.ShowHelp.tooltip) {
        (0, _singleton.hide)();
      }
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(CheckBox.prototype), "getOmitPropsKeys", this).call(this).concat(['defaultChecked', 'unCheckedValue', 'unCheckedChildren', 'indeterminate']);
    }
  }, {
    key: "handleHelpMouseEnter",
    value: function handleHelpMouseEnter(e) {
      var _this$context = this.context,
          getTooltipTheme = _this$context.getTooltipTheme,
          getTooltipPlacement = _this$context.getTooltipPlacement;
      var helpTooltipProps = this.helpTooltipProps;
      var helpTooltipCls = "".concat(this.getContextConfig('proPrefixCls'), "-tooltip-popup-help");

      if (helpTooltipProps && helpTooltipProps.popupClassName) {
        helpTooltipCls = helpTooltipCls.concat(' ', helpTooltipProps.popupClassName);
      }

      (0, _singleton.show)(e.currentTarget, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        title: this.getDisplayProp('help'),
        theme: getTooltipTheme('help'),
        placement: getTooltipPlacement('help')
      }, helpTooltipProps), {}, {
        popupClassName: helpTooltipCls
      }));
    }
  }, {
    key: "handleHelpMouseLeave",
    value: function handleHelpMouseLeave() {
      (0, _singleton.hide)();
    }
  }, {
    key: "renderTooltipHelp",
    value: function renderTooltipHelp() {
      var help = this.getDisplayProp('help');

      if (help) {
        return /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "help",
          onMouseEnter: this.handleHelpMouseEnter,
          onMouseLeave: this.handleHelpMouseLeave
        });
      }
    }
  }, {
    key: "renderInner",
    value: function renderInner() {
      return /*#__PURE__*/_react["default"].createElement("i", {
        className: "".concat(this.prefixCls, "-inner")
      });
    }
  }, {
    key: "getChildrenText",
    value: function getChildrenText() {
      var _this$props = this.props,
          children = _this$props.children,
          unCheckedChildren = _this$props.unCheckedChildren;
      return this.isChecked() ? children : unCheckedChildren || children;
    }
  }, {
    key: "getWrapperClassNames",
    value: function getWrapperClassNames() {
      var _get2;

      var prefixCls = this.prefixCls,
          indeterminate = this.props.indeterminate;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return (_get2 = (0, _get3["default"])((0, _getPrototypeOf2["default"])(CheckBox.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-indeterminate"), indeterminate)].concat(args));
    }
  }, {
    key: "isChecked",
    value: function isChecked() {
      var _this$props2 = this.props,
          checked = _this$props2.checked,
          indeterminate = _this$props2.indeterminate;

      if (indeterminate) {
        return false;
      }

      var name = this.name,
          dataSet = this.dataSet,
          checkedValue = this.checkedValue;

      if (!this.isControlled && dataSet && name) {
        return this.getValues().indexOf(checkedValue) !== -1;
      }

      if (checked !== undefined) {
        return checked;
      }

      return this.value === checkedValue;
    }
  }, {
    key: "getDataSetValues",
    value: function getDataSetValues() {
      var values = this.getDataSetValue();

      if (values === undefined) {
        return [];
      }

      return [].concat(values);
    }
  }, {
    key: "setValue",
    value: function setValue(value, noVaidate) {
      var record = this.record,
          checkedValue = this.checkedValue,
          multiple = this.multiple;

      if (record) {
        var values;

        if (multiple) {
          values = this.getValues();

          if (value === checkedValue) {
            values.push(value);
          } else {
            var index = values.indexOf(checkedValue);

            if (index !== -1) {
              values.splice(index, 1);
            }
          }
        } else {
          values = value;
        }

        (0, _get3["default"])((0, _getPrototypeOf2["default"])(CheckBox.prototype), "setValue", this).call(this, values, noVaidate);
      } else {
        (0, _get3["default"])((0, _getPrototypeOf2["default"])(CheckBox.prototype), "setValue", this).call(this, value, noVaidate);
      }
    }
  }, {
    key: "setChecked",
    value: function setChecked(checked) {
      this.setValue(checked ? this.checkedValue : this.unCheckedValue);
    }
  }, {
    key: "getOldValue",
    value: function getOldValue() {
      return this.isChecked() ? this.checkedValue : this.unCheckedValue;
    }
  }]);
  return CheckBox;
}(_Radio2.Radio);

exports.CheckBox = CheckBox;
CheckBox.displayName = 'CheckBox';
/**
 * tooltip disable sign
 */
// eslint-disable-next-line camelcase

CheckBox.__PRO_CHECKBOX = true; // eslint-disable-next-line camelcase

CheckBox.__IS_IN_CELL_EDITOR = true;
CheckBox.defaultProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _Radio2.Radio.defaultProps), {}, {
  suffixCls: 'checkbox',
  indeterminate: false
});
(0, _tslib.__decorate)([_autobind["default"]], CheckBox.prototype, "handleHelpMouseEnter", null);
(0, _tslib.__decorate)([_mobx.action], CheckBox.prototype, "setValue", null);
(0, _tslib.__decorate)([_mobx.action], CheckBox.prototype, "setChecked", null);

var ObserverCheckBox = /*#__PURE__*/function (_CheckBox) {
  (0, _inherits2["default"])(ObserverCheckBox, _CheckBox);

  var _super2 = (0, _createSuper2["default"])(ObserverCheckBox);

  function ObserverCheckBox() {
    (0, _classCallCheck2["default"])(this, ObserverCheckBox);
    return _super2.apply(this, arguments);
  }

  return (0, _createClass2["default"])(ObserverCheckBox);
}(CheckBox);

ObserverCheckBox.defaultProps = CheckBox.defaultProps; // eslint-disable-next-line camelcase

ObserverCheckBox.__PRO_CHECKBOX = true; // eslint-disable-next-line camelcase

ObserverCheckBox.__IS_IN_CELL_EDITOR = true;
ObserverCheckBox = (0, _tslib.__decorate)([_mobxReact.observer], ObserverCheckBox);
var _default = ObserverCheckBox;
exports["default"] = _default;
//# sourceMappingURL=CheckBox.js.map
