"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CheckboxGroup = CheckboxGroup;
exports.CheckboxGroupContext = CheckboxGroupContext;
exports.CheckboxGroupLayer = CheckboxGroupLayer;
exports["default"] = CheckboxSkeleton;

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

var _getCompliedClassName9 = _interopRequireDefault(require("../../utils/getCompliedClassName"));

var _Group = _interopRequireDefault(require("../group/Group"));

var _CheckboxModule = _interopRequireDefault(require("./Checkbox.module.css"));

var _getCompliedCustomId = _interopRequireDefault(require("../../utils/getCompliedCustomId"));

var _Checkbox = _interopRequireDefault(require("./Checkbox"));

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function CheckboxSkeleton(props) {
  var _getCompliedClassName, _getCompliedClassName2, _getCompliedClassName5, _getCompliedClassName6;

  var disabled = props.disabled,
      isReadOnly = props.isReadOnly,
      eleRef = props.eleRef,
      text = props.text,
      indeterminate = props.indeterminate,
      _props$customClassNam = props.customClassName,
      customClassName = _props$customClassNam === void 0 ? {} : _props$customClassNam,
      customId = props.customId,
      onChange = props.onChange,
      value = props.value,
      name = props.name,
      size = props.size,
      color = props.color,
      id = props.id,
      customIdPrefix = props.customIdPrefix,
      _props$customIdSuffix = props.customIdSuffix,
      customIdSuffix = _props$customIdSuffix === void 0 ? '' : _props$customIdSuffix,
      tagAttributeProps = props.tagAttributeProps,
      selectedValue = props.selectedValue,
      checkCondition = props.checkCondition,
      checked = props.checked;
  var isChecked = checkCondition && checkCondition(value, selectedValue);

  if (!checkCondition && props.hasOwnProperty("checked")) {
    isChecked = checked;
  }

  var checkClass = (0, _getCompliedClassName9["default"])(customClassName, (_getCompliedClassName = {}, _defineProperty(_getCompliedClassName, _CheckboxModule["default"].base, true), _defineProperty(_getCompliedClassName, _CheckboxModule["default"].checkContainment, !!isChecked), _defineProperty(_getCompliedClassName, _CheckboxModule["default"].indeterminate, !!indeterminate), _defineProperty(_getCompliedClassName, _CheckboxModule["default"][size], !!size), _defineProperty(_getCompliedClassName, _CheckboxModule["default"][color], !!color), _defineProperty(_getCompliedClassName, _CheckboxModule["default"].pointer, isReadOnly || disabled ? false : true), _defineProperty(_getCompliedClassName, _CheckboxModule["default"].disabled, !!disabled), _getCompliedClassName), (_getCompliedClassName2 = {}, _defineProperty(_getCompliedClassName2, customClassName.base, !!customClassName.base), _defineProperty(_getCompliedClassName2, customClassName.checked, !!customClassName.checked), _defineProperty(_getCompliedClassName2, customClassName.disabled, !!customClassName.disabled), _defineProperty(_getCompliedClassName2, customClassName.size, !!customClassName.size), _defineProperty(_getCompliedClassName2, customClassName.color, !!customClassName.color), _getCompliedClassName2));
  var inputClass = (0, _getCompliedClassName9["default"])(customClassName, _defineProperty({}, _CheckboxModule["default"].inputCheckbox, true), _defineProperty({}, customClassName.input, !!customClassName.input));
  var labelClass = (0, _getCompliedClassName9["default"])(customClassName, (_getCompliedClassName5 = {}, _defineProperty(_getCompliedClassName5, _CheckboxModule["default"].label, true), _defineProperty(_getCompliedClassName5, _CheckboxModule["default"].readonly, !!isReadOnly), _getCompliedClassName5), (_getCompliedClassName6 = {}, _defineProperty(_getCompliedClassName6, customClassName.label, !!customClassName.label), _defineProperty(_getCompliedClassName6, customClassName.readonly, !!customClassName.readonly), _getCompliedClassName6));
  var chButtonClass = (0, _getCompliedClassName9["default"])(customClassName, _defineProperty({}, _CheckboxModule["default"].check_button, true), _defineProperty({}, customClassName.checkButton, !!customClassName.checkButton));
  return /*#__PURE__*/_react["default"].createElement("span", _extends({
    className: checkClass
  }, tagAttributeProps), /*#__PURE__*/_react["default"].createElement("span", {
    className: chButtonClass
  }, /*#__PURE__*/_react["default"].createElement("input", {
    type: "checkbox",
    className: inputClass,
    id: id ? id : value,
    ref: eleRef,
    disabled: disabled,
    readOnly: isReadOnly,
    value: value,
    name: name,
    checked: isChecked,
    "data-id": (0, _getCompliedCustomId["default"])({
      customId: customId,
      customIdPrefix: customIdPrefix,
      customIdSuffix: customIdSuffix
    }, customId.checkbox, [value]),
    onChange: !disabled ? onChange && onChange.bind(null, value) : null
  })), /*#__PURE__*/_react["default"].createElement("label", {
    className: labelClass,
    htmlFor: id ? id : value
  }, text));
}

function CheckboxGroup(props) {
  var children = props.children,
      selectedValue = props.selectedValue,
      onChange = props.onChange,
      name = props.name,
      onChangeCallbackHandler = props.onChangeCallbackHandler,
      checkCondition = props.checkCondition,
      tagAttributeProps = props.tagAttributeProps;

  var renderChildren = _react["default"].Children.map(children, function (child) {
    return /*#__PURE__*/_react["default"].cloneElement(child, {
      selectedValue: selectedValue,
      name: name,
      checkCondition: checkCondition,
      onChange: onChangeCallbackHandler(onChange, child.props.onChange)
    });
  });

  return /*#__PURE__*/_react["default"].createElement(CheckboxGroupLayer, props, " ", renderChildren, " ");
}

function CheckboxGroupContext(props) {
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, " ", props.children ? props.children : props.options.map(function (item, i) {
    return /*#__PURE__*/_react["default"].createElement(_Checkbox["default"], _extends({
      key: i
    }, item));
  }), " ");
}

function CheckboxGroupLayer(props) {
  var _props$customClassNam2 = props.customClassName,
      customClassName = _props$customClassNam2 === void 0 ? {} : _props$customClassNam2,
      children = props.children,
      direction = props.direction,
      wrap = props.wrap,
      gap = props.gap,
      customId = props.customId,
      customIdPrefix = props.customIdPrefix,
      customIdSuffix = props.customIdSuffix,
      role = props.role,
      tagAttributeProps = props.tagAttributeProps;
  return /*#__PURE__*/_react["default"].createElement(_Group["default"], _extends({}, role, {
    direction: direction,
    customId: (0, _getCompliedCustomId["default"])({
      customId: customId,
      customIdPrefix: customIdPrefix,
      customIdSuffix: customIdSuffix
    }, customId.checkboxgroup),
    customClassName: {
      base: customClassName
    },
    wrap: wrap,
    gap: gap
  }, tagAttributeProps), children);
}