import "../chunks/chunk.QU3DSPNU.js";
import { useLayoutEffect, useRef } from "react";
import { getScopedTagName } from "../scoping.js";
function getReactComponent(tagName) {
  return getScopedTagName(tagName);
}
function useControlledValue(elementRef, value, defaultValue, onChange, onInput) {
  const controlledValueRef = useRef(void 0);
  const isControlled = value !== void 0;
  controlledValueRef.current = value;
  useLayoutEffect(() => {
    const element = elementRef.current;
    if (!element) return;
    if (isControlled) {
      const controlledValue = controlledValueRef.current;
      if (element.value !== controlledValue) {
        element.value = controlledValue;
      }
    } else if (defaultValue !== void 0 && element.value === "") {
      element.value = defaultValue;
    }
    const handleInput = (e) => {
      if (onChange) {
        onChange(e);
      }
      if (onInput) {
        onInput(e);
      }
      queueMicrotask(() => {
        const currentControlledValue = controlledValueRef.current;
        if (currentControlledValue !== void 0 && element.value !== currentControlledValue) {
          element.value = currentControlledValue;
        }
      });
    };
    element.addEventListener("input", handleInput);
    return () => element.removeEventListener("input", handleInput);
  });
}
function useControlledChecked(elementRef, checked, defaultChecked, onChange, onInput) {
  const controlledCheckedRef = useRef(void 0);
  const isControlled = checked !== void 0;
  controlledCheckedRef.current = checked;
  useLayoutEffect(() => {
    const element = elementRef.current;
    if (!element) return;
    if (isControlled) {
      const controlledChecked = controlledCheckedRef.current;
      if (element.checked !== controlledChecked) {
        element.checked = !!controlledChecked;
      }
    } else if (defaultChecked !== void 0 && !element.checked) {
      element.checked = defaultChecked;
    }
    const handleInput = (e) => {
      if (onChange) {
        onChange(e);
      }
      if (onInput) {
        onInput(e);
      }
      queueMicrotask(() => {
        const currentControlledChecked = controlledCheckedRef.current;
        if (currentControlledChecked !== void 0 && element.checked !== currentControlledChecked) {
          element.checked = currentControlledChecked;
        }
      });
    };
    element.addEventListener("input", handleInput);
    return () => element.removeEventListener("input", handleInput);
  });
}
export {
  getReactComponent,
  useControlledChecked,
  useControlledValue
};
