import * as React from "react";
import { useMyValidationErrorMessages } from "./useMyValidationErrorMessages.js";
function useBindingState(bind, overrides) {
  var _a, _b, _c, _d, _e, _f;
  const value = React.useMemo(() => {
    var _a2, _b2, _c2;
    return (overrides == null ? void 0 : overrides.value) ?? ((_c2 = (_b2 = (_a2 = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a2.format) == null ? void 0 : _b2.fromData) == null ? void 0 : _c2.call(_b2, bind.value)) ?? (bind == null ? void 0 : bind.value);
  }, [overrides == null ? void 0 : overrides.value, (_a = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a.format, bind == null ? void 0 : bind.value]);
  const onChange = React.useCallback((newValue) => {
    var _a2, _b2, _c2, _d2, _e2;
    if (newValue !== void 0) {
      (_a2 = overrides == null ? void 0 : overrides.onChange) == null ? void 0 : _a2.call(overrides, newValue);
    }
    (_e2 = bind == null ? void 0 : bind.setValue) == null ? void 0 : _e2.call(bind, ((_d2 = (_c2 = (_b2 = bind.bindConfig) == null ? void 0 : _b2.format) == null ? void 0 : _c2.toData) == null ? void 0 : _d2.call(_c2, newValue)) ?? newValue);
  }, [bind, overrides]);
  const getFormattedValueFromData = React.useCallback((val) => {
    var _a2, _b2, _c2;
    return ((_c2 = (_b2 = (_a2 = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a2.format) == null ? void 0 : _b2.fromData) == null ? void 0 : _c2.call(_b2, val)) ?? val;
  }, [(_b = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _b.format]);
  const getFormattedValueToData = React.useCallback((val) => {
    var _a2, _b2, _c2;
    return ((_c2 = (_b2 = (_a2 = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a2.format) == null ? void 0 : _b2.toData) == null ? void 0 : _c2.call(_b2, val)) ?? val;
  }, [(_c = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _c.format]);
  const validationErrorMessages = useMyValidationErrorMessages(bind, overrides == null ? void 0 : overrides.validationErrorMessages);
  const validationMode = (overrides == null ? void 0 : overrides.validationMode) ?? ((_d = bind == null ? void 0 : bind.formConfig) == null ? void 0 : _d.validationMode) ?? "both";
  const validationErrorIcon = (overrides == null ? void 0 : overrides.validationErrorIcon) ?? ((_e = bind == null ? void 0 : bind.formConfig) == null ? void 0 : _e.validationErrorIcon);
  return [
    value,
    onChange,
    {
      getFormattedValueFromData,
      getFormattedValueToData,
      validationErrorMessages,
      isTouched: (bind == null ? void 0 : bind.isTouched) ?? false,
      setTouched: (touched) => bind == null ? void 0 : bind.setTouched(touched),
      validate: (setInputTouched, silent) => {
        var _a2;
        return (bind == null ? void 0 : bind.validate(setInputTouched, silent, (_a2 = bind == null ? void 0 : bind.bindConfig) == null ? void 0 : _a2.autoValidateAll)) ?? true;
      },
      isValid: ((bind == null ? void 0 : bind.isValid) ?? true) && !validationErrorMessages.length,
      validationMode,
      validationErrorIcon,
      shouldShowValidationErrorIcon: validationMode === "icon" || validationMode === "both",
      shouldShowValidationErrorMessage: validationMode === "message" || validationMode === "both",
      autoValidate: (overrides == null ? void 0 : overrides.autoValidate) ?? ((_f = bind == null ? void 0 : bind.formConfig) == null ? void 0 : _f.autoValidate)
    }
  ];
}
export {
  useBindingState
};
//# sourceMappingURL=useBindingState.js.map
