"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const im = require("react-icons/im");
const useBindingState = require("../../form/hooks/useBindingState.cjs");
const arrays = require("../../utils/arrays.cjs");
const classNames = require("../../utils/classNames.cjs");
const maths = require("../../utils/maths.cjs");
require("../../utils/globals.cjs");
require("zod");
const useDidUpdateEffect = require("../../hooks/useDidUpdateEffect.cjs");
const button_component = require("../button/button.component.cjs");
const config_context = require("../config/config.context.cjs");
const label_component = require("../label/label.component.cjs");
const statusWrapper_component = require("../statusWrapper/statusWrapper.component.cjs");
const validationErrors_component = require("../validationErrors/validationErrors.component.cjs");
const rating_utils = require("./rating.utils.cjs");
require("./rating.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 RatingPart = React__namespace.forwardRef(({ index, value, onSelectPart, filledIcon, emptyIcon, step, mode, readOnly, disabled }, ref) => {
  const steps = Math.floor(1 / (step || 1));
  const isFilled = value && value >= index + 1;
  const isPart = value && value < index + 1 && value > index;
  return jsxRuntime.jsxs("div", { ref, className: "arm-rating-part", style: value ? {
    "--rating-amount": `${maths.clamp((value - index) * 100, 0, 100)}%`
  } : void 0, "data-filled": isFilled, "data-part": isPart, children: [jsxRuntime.jsxs("div", { className: "arm-rating-part-icon-wrapper", children: [filledIcon && jsxRuntime.jsx("div", { className: "arm-rating-part-icon arm-rating-part-filled", children: jsxRuntime.jsx("div", { className: "arm-rating-part-icon-inner", children: rating_utils.iconJsxFromDefinition(filledIcon, index) }) }), emptyIcon && jsxRuntime.jsx("div", { className: "arm-rating-part-icon arm-rating-part-empty", children: jsxRuntime.jsx("div", { className: "arm-rating-part-icon-inner", children: rating_utils.iconJsxFromDefinition(emptyIcon, index) }) })] }), !readOnly && mode === "buttons" && jsxRuntime.jsx("div", { className: "arm-rating-part-buttons", children: arrays.repeat(steps, (buttonIndex) => {
    const inputValue = index + (steps === 2 ? 0.5 : 1) + (buttonIndex ? 0.5 : 0);
    return jsxRuntime.jsx(button_component.Button, { role: "radio", "aria-checked": inputValue === value, type: "button", className: "arm-rating-button", onClick: () => onSelectPart((step || 1) * (buttonIndex + 1)), "aria-label": inputValue.toString(), disabled }, buttonIndex);
  }) }), !readOnly && mode === "radio" && jsxRuntime.jsx("div", { className: "arm-rating-part-radios", children: arrays.repeat(steps, (buttonIndex) => {
    const inputValue = index + (steps === 2 ? 0.5 : 1) + (buttonIndex ? 0.5 : 0);
    return jsxRuntime.jsx("div", { className: "arm-rating-part-radio-wrapper", children: jsxRuntime.jsx("input", { className: "arm-rating-part-radio", type: "radio", onChange: () => onSelectPart((step || 1) * (buttonIndex + 1)), "aria-label": inputValue.toString(), value: inputValue, checked: inputValue === value, disabled }) }, buttonIndex);
  }) })] });
});
RatingPart.displayName = "RatingPart";
const Rating = React__namespace.forwardRef(
  ({ bind, value, onValueChange, filledIcon, emptyIcon, maximum, className, validationErrorMessages, validationMode, errorIcon, scrollValidationErrorsIntoView, step, error, statusPosition, pending, leftOverlay, rightOverlay, mode, disabled, statusClassName, validationErrorsClassName, labelClassName, labelId, label, required, requiredIndicator, displaySize, autoValidate, ...htmlProps }, ref) => {
    const [boundValue, setBoundValue, bindConfig] = useBindingState.useBindingState(bind, {
      value,
      onChange: onValueChange,
      validationErrorMessages,
      validationMode,
      autoValidate,
      validationErrorIcon: errorIcon
    });
    const globals = config_context.useArmstrongConfig({
      validationMode: bindConfig.validationMode,
      inputDisplaySize: displaySize,
      scrollValidationErrorsIntoView,
      requiredIndicator,
      validationErrorIcon: bindConfig.validationErrorIcon,
      autoValidate: bindConfig.autoValidate
    });
    const generatedLabelId = React__namespace.useId();
    const finalLabelId = labelId ?? generatedLabelId;
    useDidUpdateEffect.useDidUpdateEffect(() => {
      if (globals.autoValidate) {
        bindConfig.validate();
      }
      bindConfig.setTouched(true);
    }, [boundValue]);
    return jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("div", { ref, role: "radiogroup", "aria-label": boundValue !== void 0 ? `Rating: ${boundValue}/${maximum}` : void 0, ...htmlProps, className: classNames.concat("arm-rating", className), "data-read-only": !setBoundValue, "data-size": globals.inputDisplaySize, children: [label && jsxRuntime.jsx(label_component.Label, { id: finalLabelId, className: classNames.concat("arm-rating-input-label", labelClassName), "data-disabled": disabled, required, displaySize: globals.inputDisplaySize, requiredIndicator: globals.requiredIndicator, children: label }), jsxRuntime.jsx("div", { className: "arm-rating-input-inner", "aria-labelledby": finalLabelId, children: jsxRuntime.jsx(statusWrapper_component.StatusWrapper, { error, statusPosition, errorIcon: bindConfig.validationErrorIcon, validationMode: bindConfig.validationMode, pending, children: jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [leftOverlay, jsxRuntime.jsxs("div", { className: "arm-rating-parts", children: [arrays.repeat(maximum, (index) => jsxRuntime.jsx(RatingPart, { index, filledIcon, emptyIcon, value: boundValue || void 0, onSelectPart: (proportion) => setBoundValue == null ? void 0 : setBoundValue(index + proportion), step, mode, disabled, readOnly: !setBoundValue }, index)), mode === "range" && jsxRuntime.jsx("input", { className: "arm-rating-range", type: "range", step, min: 0, max: maximum, disabled, value: boundValue || void 0, onChange: (event) => setBoundValue == null ? void 0 : setBoundValue(event.currentTarget.valueAsNumber) })] }), rightOverlay] }) }) })] }), !!(validationErrorMessages == null ? void 0 : validationErrorMessages.length) && bindConfig.shouldShowValidationErrorMessage && jsxRuntime.jsx(validationErrors_component.ValidationErrors, { validationErrors: validationErrorMessages, validationMode: bindConfig.validationMode, scrollIntoView: scrollValidationErrorsIntoView })] });
  }
  // type assertion to ensure generic works with RefForwarded component
  // DO NOT CHANGE TYPE WITHOUT CHANGING THIS, FIND TYPE BY INSPECTING React.forwardRef
);
Rating.defaultProps = {
  maximum: 5,
  filledIcon: jsxRuntime.jsx(im.ImStarFull, {}),
  emptyIcon: jsxRuntime.jsx(im.ImStarEmpty, {}),
  step: 1,
  mode: "buttons"
};
Rating.displayName = "Rating";
exports.Rating = Rating;
//# sourceMappingURL=rating.component.cjs.map
