"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const jsxRuntime = require("react/jsx-runtime");
const useBindingState = require("../../form/hooks/useBindingState.cjs");
const classNames = require("../../utils/classNames.cjs");
require("../../utils/globals.cjs");
require("zod");
const useDidUpdateEffect = require("../../hooks/useDidUpdateEffect.cjs");
const options = require("../../types/options.cjs");
const checkbox_component = require("../checkbox/checkbox.component.cjs");
const config_context = require("../config/config.context.cjs");
const label_component = require("../label/label.component.cjs");
const validationErrors_component = require("../validationErrors/validationErrors.component.cjs");
require("./checkboxList.theme.css.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
const CheckboxList = React__namespace.forwardRef(
  ({ bind, options: options$1, className, value, errorIcon, validationMode, validationErrorMessages, onChange, customIndicator, error, displaySize, label, labelClassName, labelId, required, disabled, scrollValidationErrorsIntoView, validationErrorsClassName, requiredIndicator, autoValidate, ...nativeProps }, ref) => {
    const [boundValue, setBoundValue, bindConfig] = useBindingState.useBindingState(bind, {
      value,
      validationErrorMessages,
      validationErrorIcon: errorIcon,
      validationMode,
      onChange,
      autoValidate
    });
    const globals = config_context.useArmstrongConfig({
      validationMode: bindConfig.validationMode,
      requiredIndicator,
      scrollValidationErrorsIntoView,
      validationErrorIcon: bindConfig.validationErrorIcon,
      inputDisplaySize: displaySize,
      autoValidate: bindConfig.autoValidate
    });
    const onCheckedChange = React__namespace.useCallback((option, newValue) => {
      const newBoundValue = options$1.reduce((arr, op) => {
        const isSelectedOption = op.id === option.id;
        const isCurrentlyBound = boundValue == null ? void 0 : boundValue.includes(op.id);
        const addIfCurrentlyBound = !isSelectedOption || newValue;
        const addIfNotCurrentlyBound = isSelectedOption && newValue;
        const shouldAdd = isCurrentlyBound ? addIfCurrentlyBound : addIfNotCurrentlyBound;
        return [...arr, ...shouldAdd ? [op.id] : []];
      }, []);
      setBoundValue == null ? void 0 : setBoundValue(newBoundValue);
    }, [setBoundValue, boundValue, options$1]);
    useDidUpdateEffect.useDidUpdateEffect(() => {
      if (globals.autoValidate) {
        bindConfig.validate();
      }
      bindConfig.setTouched(true);
    }, [boundValue]);
    return jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [label && jsxRuntime.jsx(label_component.Label, { id: labelId, className: classNames.concat("arm-checkbox-list-label", labelClassName), required, requiredIndicator: globals.requiredIndicator, displaySize: globals.inputDisplaySize, children: label }), jsxRuntime.jsxs("div", { className: classNames.concat("arm-checkbox-list", className), ref, "data-error": error || !!(validationErrorMessages == null ? void 0 : validationErrorMessages.length), "data-disabled": disabled, "data-size": globals.inputDisplaySize, ...nativeProps, children: [options$1.map((option) => {
      const isChecked = boundValue == null ? void 0 : boundValue.includes(option.id);
      return React.createElement(checkbox_component.Checkbox, { customIndicator, ...option.htmlProps ?? {}, key: option.id, className: "arm-checkbox-list-item", checked: isChecked, displaySize: globals.inputDisplaySize, onCheckedChange: (v) => onCheckedChange(option, v), disabled: disabled || option.disabled, label: options.getContentFromOption(option, isChecked) });
    }), bindConfig.shouldShowValidationErrorMessage && bindConfig.validationErrorMessages && jsxRuntime.jsx(validationErrors_component.ValidationErrors, { validationErrors: bindConfig.validationErrorMessages, scrollIntoView: globals.scrollValidationErrorsIntoView, className: classNames.concat("arm-checkbox-list-errors", validationErrorsClassName) })] })] });
  }
  // type assertion to ensure generic works with RefForwarded component
  // DO NOT CHANGE TYPE WITHOUT CHANGING THIS, FIND TYPE BY INSPECTING React.forwardRef
);
CheckboxList.displayName = "CheckboxList";
exports.CheckboxList = CheckboxList;
//# sourceMappingURL=checkboxList.component.cjs.map
