"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _includes = _interopRequireDefault(require("lodash/includes"));

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

var _button = _interopRequireDefault(require("../button"));

var _configProvider = require("../config-provider");

var _list = _interopRequireDefault(require("../list"));

var _context = require("./context");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var getChecked = function getChecked(props, defaultChecked) {
  var _ref, _props$checked;

  return (_ref = (_props$checked = props.checked) !== null && _props$checked !== void 0 ? _props$checked : props.defaultChecked) !== null && _ref !== void 0 ? _ref : defaultChecked;
};

;
var Checkbox = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var inputRef = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(getChecked(props, false)),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      checked = _useState2[0],
      setChecked = _useState2[1];

  var disabled = props.disabled;
  var groupContext = (0, _react.useContext)(_context.CheckboxGroupContext);

  if (groupContext && props.value !== undefined) {
    checked = (0, _includes.default)(groupContext.value, props.value);

    setChecked = function setChecked(changedChecked) {
      if (changedChecked) {
        groupContext.check(props.value);
      } else {
        groupContext.uncheck(props.value);
      }
    };

    disabled = disabled || groupContext.disabled;
  }

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = (0, _bem.createBEM)('checkbox', {
    prefixCls: prefixCls
  });
  var cls = bem([{
    disabled: disabled,
    checked: checked && !props.indeterminate,
    untext: !props.children,
    indeterminate: props.indeterminate
  }, props.className]);

  var currentProps = _objectSpread(_objectSpread({}, props), {}, {
    checked: checked
  });

  var textRender = props.children && /*#__PURE__*/_react.default.createElement("span", {
    className: bem('text')
  }, props.children);

  var iconRender = /*#__PURE__*/_react.default.createElement("span", {
    className: bem('icon')
  }, props.renderIcon ? props.renderIcon(currentProps) : /*#__PURE__*/_react.default.createElement("span", {
    className: bem('tick')
  }, props.indeterminate ? /*#__PURE__*/_react.default.createElement(_icons.Minus, null) : /*#__PURE__*/_react.default.createElement(_icons.Success, null)));

  var inputRender = /*#__PURE__*/_react.default.createElement("input", {
    ref: inputRef,
    id: props.id,
    type: "checkbox",
    className: bem('input'),
    "aria-checked": checked,
    disabled: disabled,
    value: props.value,
    checked: checked,
    onChange: function onChange(e) {
      var _props$onChange;

      if (disabled) return;

      if (!('checked' in props)) {
        setChecked(e.target.checked);
      }

      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, e);
    }
  });

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      check: function check() {
        if (checked) return;
        inputRef.current.click();
      },
      uncheck: function uncheck() {
        if (!checked) return;
        inputRef.current.click();
      },
      toggle: function toggle() {
        inputRef.current.click();
      }
    };
  });
  (0, _react.useEffect)(function () {
    if (props.checked === undefined) return;
    if (props.checked === checked) return;
    setChecked(getChecked({
      checked: props.checked,
      defaultChecked: props.defaultChecked
    }, false));
  }, [props.checked, props.defaultChecked]);

  if ((groupContext === null || groupContext === void 0 ? void 0 : groupContext.type) === 'button') {
    return /*#__PURE__*/_react.default.createElement("label", {
      className: cls,
      style: props.style
    }, inputRender, /*#__PURE__*/_react.default.createElement(_button.default, {
      disabled: disabled,
      theme: checked ? 'primary' : 'default',
      size: "xs",
      block: groupContext === null || groupContext === void 0 ? void 0 : groupContext.block,
      onClick: function onClick() {
        inputRef.current.click();
      }
    }, props.children));
  }

  if ((groupContext === null || groupContext === void 0 ? void 0 : groupContext.type) === 'list') {
    var tickRender = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, inputRender, iconRender);

    return /*#__PURE__*/_react.default.createElement(_list.default.Item, {
      hasArrow: false,
      className: cls,
      style: props.style,
      prefix: (groupContext === null || groupContext === void 0 ? void 0 : groupContext.iconAlign) === 'before' ? tickRender : undefined,
      suffix: (groupContext === null || groupContext === void 0 ? void 0 : groupContext.iconAlign) === 'after' ? tickRender : undefined,
      title: textRender,
      onClick: !disabled ? function () {
        if (disabled) return;
        inputRef.current.click();
      } : undefined
    });
  }

  var contentRender = props.render ? props.render(currentProps) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, iconRender, textRender);
  return /*#__PURE__*/_react.default.createElement("label", {
    className: cls,
    style: props.style
  }, inputRender, contentRender);
});
Checkbox.displayName = 'Checkbox';
Checkbox.defaultProps = {
  indeterminate: false
};
var _default = Checkbox;
exports.default = _default;