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); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { __rest } from "tslib";
import classNames from 'classnames';
import React, { createElement, forwardRef, useCallback } from "react";
import { Text } from "@alifd/meet-react-component-one";
import Icon from '../icon/index';
import { isString, isUndef } from '../utils';
import { useValue } from '../utils/hooks';
import View from '../view';

var Checkbox = function Checkbox(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 = __rest(props, ["size", "prefix", "children", "checked", "indeterminate", "disabled", "className", "defaultChecked", "defaultIndeterminate", "checkIcon", "indeterminateIcon", "readOnly", "onChange", "value"]);

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

  var _useValue3 = useValue(props, false, {
    valueName: 'indeterminate'
  }),
      _useValue4 = _slicedToArray(_useValue3, 3),
      indeterminate = _useValue4[0],
      setIndeterminate = _useValue4[1],
      isIndeterminateControlled = _useValue4[2];

  var state = 'normal';
  var selectedPrefix = !indeterminate && checked ? '--selected' : '--unselected';
  var disabledPrefix = disabled ? '-disabled' : '';
  var indeterminatePrefix = indeterminate ? '-indeterminate' : '';
  var statePrefix = state !== 'normal' ? "-".concat(state) : '';
  var iconCls = classNames("".concat(prefix, "checkbox-icon"), "".concat(prefix, "checkbox-icon--").concat(size), "".concat(prefix, "checkbox-icon").concat(selectedPrefix).concat(indeterminatePrefix).concat(disabledPrefix));
  var textCls = classNames("".concat(prefix, "checkbox-text"), "".concat(prefix, "checkbox-text--").concat(size), "".concat(prefix, "checkbox-text").concat(selectedPrefix).concat(indeterminatePrefix).concat(disabledPrefix));
  var handleClick = 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]);

  var renderChildren = function renderChildren(c) {
    if (isString(c)) {
      return /*#__PURE__*/React.createElement(Text, {
        className: textCls
      }, children);
    } else if (!isUndef(c)) {
      return /*#__PURE__*/React.createElement(View, {
        className: textCls
      }, children);
    }

    return null;
  };

  return /*#__PURE__*/React.createElement(View, _extends({
    ref: ref
  }, others, {
    className: classNames("".concat(prefix, "checkbox-container"), "".concat(prefix, "checkbox-container--").concat(size), className),
    onClick: handleClick
  }), /*#__PURE__*/React.createElement(View, {
    className: classNames("".concat(prefix, "checkbox-circle"), "".concat(prefix, "checkbox-circle--").concat(size), "".concat(prefix, "checkbox-circle").concat(selectedPrefix).concat(indeterminatePrefix).concat(statePrefix).concat(disabledPrefix))
  }, indeterminate && /*#__PURE__*/React.createElement(Icon, {
    name: indeterminateIcon,
    className: iconCls
  }), checked && !indeterminate && /*#__PURE__*/React.createElement(Icon, {
    name: checkIcon,
    className: iconCls
  })), renderChildren(children));
};

export default /*#__PURE__*/forwardRef(Checkbox);