"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OptionGroup = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Styles = require("./Styles");
var _Checkbox = require("../../Checkbox/Checkbox");
var _List = require("../../List");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
var OptionGroup = exports.OptionGroup = function OptionGroup(_ref) {
  var id = _ref.id,
    name = _ref.name,
    options = _ref.options,
    type = _ref.type,
    hover = _ref.hover,
    filter = _ref.filter,
    checked = _ref.checked,
    onChange = _ref.onChange,
    setHover = _ref.setHover,
    renderOptions = _ref.renderOptions,
    optionGroupClassName = _ref.optionGroupClassName,
    _ref$mixedOptions = _ref.mixedOptions,
    mixedOptions = _ref$mixedOptions === void 0 ? [] : _ref$mixedOptions;
  var list = (0, _react.useMemo)(function () {
    return filter ? options.filter(function (v) {
      return v.name.toLowerCase().includes(filter.trim().toLowerCase());
    }) : options;
  }, [filter, options]);
  (0, _react.useEffect)(function () {
    if (hover === id && type !== "single") {
      var _document$getElementB;
      (_document$getElementB = document.getElementById("".concat(id))) === null || _document$getElementB === void 0 || _document$getElementB.focus();
    }
  }, [hover, type, id]);
  var isAllOptionsChecked = (0, _react.useMemo)(function () {
    return options.every(function (option) {
      return checked.includes(option.id);
    });
  }, [options, checked]);
  var handleHover = (0, _react.useCallback)(function () {
    if (options.some(function (option) {
      return checked.includes(option.id);
    })) return;
    if (setHover) {
      setHover(type === "single" ? undefined : id);
    }
  }, [setHover, type, id, checked, options]);
  var allOptionValues = (0, _react.useMemo)(function () {
    return options.map(function (option) {
      return option.id;
    });
  }, [options]);
  var isMixed = (0, _react.useMemo)(function () {
    return (options.some(function (option) {
      return checked.includes(option.id);
    }) || mixedOptions.some(function (item) {
      return allOptionValues.includes(item);
    })) && !isAllOptionsChecked;
  }, [allOptionValues, checked, isAllOptionsChecked, mixedOptions, options]);
  var handleClick = (0, _react.useCallback)(function (e) {
    e.stopPropagation();
    if (onChange) {
      if (isAllOptionsChecked) {
        onChange(checked.filter(function (i) {
          return allOptionValues.includes(i) === false;
        }));
      } else {
        var values = [...checked, ...allOptionValues];
        var unique = values.filter(function (item, pos) {
          return values.indexOf(item) === pos;
        });
        onChange(unique);
      }
    }
  }, [allOptionValues, checked, isAllOptionsChecked, onChange]);
  return /*#__PURE__*/_react.default.createElement(_Styles.StyledOptionGroupLi, {
    key: id,
    className: (0, _classnames.default)("c-option-group", optionGroupClassName)
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledOptionGroup, {
    key: id
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledOptionGroupOption, {
    onMouseEnter: handleHover,
    hover: hover === id && type === "multiple"
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledOptionGroupHeader, null, /*#__PURE__*/_react.default.createElement(_Styles.StyledOptionGroupHeaderName, {
    weight: "bold"
  }, name), type === "multiple" && /*#__PURE__*/_react.default.createElement(_Checkbox.Checkbox, {
    id: "".concat(id),
    checked: isAllOptionsChecked,
    mixed: isMixed,
    onChange: handleClick,
    type: "checkbox"
  }))), /*#__PURE__*/_react.default.createElement(_List.List, null, list.map(function (item, index) {
    return typeof renderOptions === "function" ? renderOptions(item, index) : item.name;
  }))));
};
OptionGroup.displayName = "OptionGroup";
//# sourceMappingURL=OptionGroup.js.map