"use strict";

exports.__esModule = true;
exports.default = void 0;

var _tslib = require("tslib");

var _classnames = _interopRequireDefault(require("classnames"));

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

var _meetReactComponentOne = require("@alifd/meet-react-component-one");

var _index = _interopRequireDefault(require("../icon/index"));

var _utils = require("../utils");

var _hooks = require("../utils/hooks");

var _view = _interopRequireDefault(require("../view"));

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 || 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 Checkbox = function (props, ref) {
  var _props$size = props.size,
      size = _props$size === void 0 ? 'medium' : _props$size,
      _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      children = props.children,
      _checked = props.checked,
      _indeterminate = props.indeterminate,
      disabled = props.disabled,
      className = props.className,
      _defaultChecked = props.defaultChecked,
      _defaultIndeterminate = props.defaultIndeterminate,
      _props$checkIcon = props.checkIcon,
      checkIcon = _props$checkIcon === void 0 ? 'select' : _props$checkIcon,
      _props$indeterminateI = props.indeterminateIcon,
      indeterminateIcon = _props$indeterminateI === void 0 ? 'semi-select' : _props$indeterminateI,
      readOnly = props.readOnly,
      onChange = props.onChange,
      value = props.value,
      others = (0, _tslib.__rest)(props, ["size", "prefix", "children", "checked", "indeterminate", "disabled", "className", "defaultChecked", "defaultIndeterminate", "checkIcon", "indeterminateIcon", "readOnly", "onChange", "value"]);

  var _useValue = (0, _hooks.useValue)(props, false, {
    valueName: 'checked'
  }),
      checked = _useValue[0],
      setChecked = _useValue[1],
      isCheckedControlled = _useValue[2];

  var _useValue2 = (0, _hooks.useValue)(props, false, {
    valueName: 'indeterminate'
  }),
      indeterminate = _useValue2[0],
      setIndeterminate = _useValue2[1],
      isIndeterminateControlled = _useValue2[2];

  var state = 'normal';
  var selectedPrefix = !indeterminate && checked ? '--selected' : '--unselected';
  var disabledPrefix = disabled ? '-disabled' : '';
  var indeterminatePrefix = indeterminate ? '-indeterminate' : '';
  var statePrefix = '';
  var iconCls = (0, _classnames.default)(prefix + "checkbox-icon", prefix + "checkbox-icon--" + size, prefix + "checkbox-icon" + selectedPrefix + indeterminatePrefix + disabledPrefix);
  var textCls = (0, _classnames.default)(prefix + "checkbox-text", prefix + "checkbox-text--" + size, prefix + "checkbox-text" + selectedPrefix + indeterminatePrefix + disabledPrefix);
  var handleClick = (0, _react.useCallback)(function (e) {
    if (disabled || readOnly) {
      return;
    }

    var newChecked = null;

    if (indeterminate) {
      if (isIndeterminateControlled) {
        if (onChange) {
          onChange(!checked, e);
        }

        return;
      } else {
        setIndeterminate(false);
        newChecked = true;
      }
    }

    if (newChecked === null) {
      newChecked = !checked;
    }

    if (!isCheckedControlled) {
      setChecked(newChecked);
    }

    if (onChange) {
      onChange(newChecked, e);
    }
  }, [onChange, disabled, readOnly, checked, setChecked, isCheckedControlled, indeterminate, setIndeterminate, isIndeterminateControlled]);
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref
  }, others, {
    className: (0, _classnames.default)(prefix + "checkbox-container", prefix + "checkbox-container--" + size, className),
    onClick: handleClick
  }), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(prefix + "checkbox-circle", prefix + "checkbox-circle--" + size, prefix + "checkbox-circle" + selectedPrefix + indeterminatePrefix + statePrefix + disabledPrefix)
  }, indeterminate && /*#__PURE__*/(0, _react.createElement)(_index.default, {
    name: indeterminateIcon,
    className: iconCls
  }), checked && !indeterminate && /*#__PURE__*/(0, _react.createElement)(_index.default, {
    name: checkIcon,
    className: iconCls
  })), function renderChildren(c) {
    if ((0, _utils.isString)(c)) {
      return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
        className: textCls
      }, children);
    } else if (!(0, _utils.isUndef)(c)) {
      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: textCls
      }, children);
    }

    return null;
  }(children));
};

var _default = /*#__PURE__*/(0, _react.forwardRef)(Checkbox);

exports.default = _default;