"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _checkbox = _interopRequireDefault(require("../checkbox"));

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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

var preCls = 'titaui-checkbox-list';

var CheckBoxList = function CheckBoxList(_ref) {
  var dataList = _ref.dataList,
      onChange = _ref.onChange,
      disabled = _ref.disabled,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className;
  var onClickItemHandler = (0, _react.useCallback)(function (idx) {
    var newList = _toConsumableArray(dataList);

    if (newList[idx].disabled || disabled && !newList[idx].checked) return;
    if (!newList[idx].isFolder) newList[idx].checked = !newList[idx].checked;
    if (onChange) onChange(newList, idx, newList[idx]);
  }, [onChange, disabled]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, " ").concat(className)
  }, dataList.map(function (item, i) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__item"),
      onClick: onClickItemHandler.bind(null, i)
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__item__content")
    }, item.component), item.isFolder ? /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(preCls, "__item__next tu-icon-APP-xi")
    }) : /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
      disabled: disabled || item.disabled,
      checked: item.checked
    }));
  }));
};

var _default = /*#__PURE__*/_react["default"].memo(CheckBoxList);

exports["default"] = _default;