var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames'; // import Icon from '../icon';

import CheckboxOn from "../icons/checkbox-on";
import CheckboxOff from "../icons/checkbox-off";
import CheckboxIntermediate from "../icons/checkbox-intermediate";
import "./style/checkbox.css";
var prefixCls = 'dmc-checkbox';

var getIconAttr = function getIconAttr(on, indeterminate) {
  return on ? [CheckboxOn, 'checked'] : indeterminate ? [CheckboxIntermediate, 'intermediate'] : [CheckboxOff, 'uncheck'];
};

export var Checkbox = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _a;

  var type = props.type,
      size = props.size,
      defaultChecked = props.defaultChecked,
      checked = props.checked,
      disabled = props.disabled,
      onClick = props.onClick,
      onChange = props.onChange,
      indeterminate = props.indeterminate,
      className = props.className,
      hiddenTitle = props.hiddenTitle,
      value = props.value,
      subTitle = props.subTitle,
      title = props.title,
      children = props.children,
      restProps = __rest(props, ["type", "size", "defaultChecked", "checked", "disabled", "onClick", "onChange", "indeterminate", "className", "hiddenTitle", "value", "subTitle", "title", "children"]);

  var _b = React.useState(defaultChecked || false),
      unControlVal = _b[0],
      setUnControlVal = _b[1];

  var isControl = ('checked' in props);
  var val = !!(isControl ? checked : unControlVal);
  var labelCls = classNames(prefixCls, (_a = {}, _a[prefixCls + "-" + size] = size, _a[prefixCls + "-" + type] = type, _a[prefixCls + "-disabled"] = disabled, _a[prefixCls + "-has-sub-title"] = subTitle, _a), className);

  var handleChange = function handleChange(e) {
    !isControl && setUnControlVal(!val);
    onChange && onChange(!val, value, e);
    onClick === null || onClick === void 0 ? void 0 : onClick(val, value, e);
  };

  var _c = getIconAttr(val, indeterminate),
      Icon = _c[0],
      status = _c[1];

  var cn = children;

  if (!cn && !!title) {
    cn = _jsxs("div", __assign({
      className: prefixCls + "-info"
    }, {
      children: [!!title && _jsx("div", __assign({
        className: prefixCls + "-title"
      }, {
        children: title
      }), void 0), !!subTitle && _jsx("div", __assign({
        className: prefixCls + "-sub-title"
      }, {
        children: subTitle
      }), void 0)]
    }), void 0);
  }

  return _jsxs("label", __assign({}, restProps, {
    ref: ref,
    className: labelCls,
    onClick: disabled ? undefined : handleChange
  }, {
    children: [_jsx("span", __assign({
      className: prefixCls + "-icon-container"
    }, {
      children: _jsx(Icon, {
        className: prefixCls + "-icon-" + status
      }, void 0)
    }), void 0), props.icon, !hiddenTitle && cn]
  }), void 0);
});
Checkbox.displayName = 'Checkbox';
export default Checkbox;
var CheckboxMemo = /*#__PURE__*/React.memo(Checkbox);
CheckboxMemo.displayName = 'CheckboxMemo';
export { CheckboxMemo };

