"use strict";
const React = require("react");
const is = require("../_util/is.js");
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = { error };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var useCheckbox = function(values, defaultSelected) {
  var _a = __read(React.useState(__spreadArray([], __read(defaultSelected || []), false)), 2), selected = _a[0], setSelected = _a[1];
  var _b = React.useMemo(function() {
    var isSelected2 = function(value) {
      return selected.indexOf(value) > -1;
    };
    var setValueSelected2 = function(value, selectStatus) {
      var list = is.isArray(value) ? value : [value];
      var newSelected;
      if (selectStatus) {
        newSelected = __spreadArray(__spreadArray([], __read(selected), false), __read(list), false);
      } else {
        newSelected = selected.filter(function(x) {
          return list.indexOf(x) === -1;
        });
      }
      setSelected(Array.from(new Set(newSelected)));
    };
    return {
      isSelected: isSelected2,
      setValueSelected: setValueSelected2
    };
  }, [selected]), isSelected = _b.isSelected, setValueSelected = _b.setValueSelected;
  var _c = React.useMemo(function() {
    var selectAll2 = function() {
      setSelected(values);
    };
    var unSelectAll2 = function() {
      setSelected([]);
    };
    var toggle2 = function(value) {
      if (value === void 0) {
        value = values;
      }
      var list = is.isArray(value) ? value : [value];
      var newSelected = __spreadArray([], __read(selected), false);
      list.forEach(function(x) {
        var index = newSelected.indexOf(x);
        if (index > -1) {
          newSelected.splice(index, 1);
        } else {
          newSelected.push(x);
        }
      });
      setSelected(newSelected);
    };
    var isAllSelected2 = function() {
      return values.every(function(x) {
        return isSelected(x);
      });
    };
    var isPartialSelected2 = function() {
      return values.some(function(x) {
        return isSelected(x);
      }) && !isAllSelected2();
    };
    return {
      selectAll: selectAll2,
      unSelectAll: unSelectAll2,
      toggle: toggle2,
      isAllSelected: isAllSelected2,
      isPartialSelected: isPartialSelected2
    };
  }, [selected, values, isSelected]), selectAll = _c.selectAll, unSelectAll = _c.unSelectAll, toggle = _c.toggle, isAllSelected = _c.isAllSelected, isPartialSelected = _c.isPartialSelected;
  return {
    selected,
    setSelected,
    setValueSelected,
    selectAll,
    unSelectAll,
    toggle,
    isSelected,
    isAllSelected,
    isPartialSelected
  };
};
const useCheckbox$1 = useCheckbox;
module.exports = useCheckbox$1;
//# sourceMappingURL=useCheckbox.js.map
