// src/components/user-action/Textarea.tsx
import { forwardRef, useId } from "react";
import clsx2 from "clsx";

// src/components/user-action/Label.tsx
import clsx from "clsx";
import { jsx } from "react/jsx-runtime";
var styleMapping = {
  md: "typography-label-md color-label-text",
  lg: "typography-label-lg color-label-text"
};
var Label = ({
  children,
  size = "md",
  className,
  ...props
}) => {
  return /* @__PURE__ */ jsx("label", { ...props, className: clsx(styleMapping[size], className), children });
};

// src/hooks/useDelay.ts
import { useEffect, useState } from "react";
var defaultOptions = {
  delay: 3e3,
  disabled: false
};
function useDelay(options) {
  const [timer, setTimer] = useState(void 0);
  const { delay, disabled } = {
    ...defaultOptions,
    ...options
  };
  const clearTimer = () => {
    clearTimeout(timer);
    setTimer(void 0);
  };
  const restartTimer = (onDelayFinish) => {
    if (disabled) {
      return;
    }
    clearTimeout(timer);
    setTimer(setTimeout(() => {
      onDelayFinish();
      setTimer(void 0);
    }, delay));
  };
  useEffect(() => {
    return () => {
      clearTimeout(timer);
    };
  }, [timer]);
  useEffect(() => {
    if (disabled) {
      clearTimeout(timer);
      setTimer(void 0);
    }
  }, [disabled, timer]);
  return { restartTimer, clearTimer, hasActiveTimer: !!timer };
}

// src/hooks/useOverwritableState.ts
import { useEffect as useEffect2, useState as useState2 } from "react";

// src/utils/resolveSetState.ts
function resolveSetState(action, prev) {
  return typeof action === "function" ? action(prev) : action;
}

// src/hooks/useOverwritableState.ts
var useOverwritableState = (initialValue, onChange) => {
  const [state, setState] = useState2(initialValue);
  useEffect2(() => {
    setState(initialValue);
  }, [initialValue]);
  const onChangeWrapper = (action) => {
    const resolved = resolveSetState(action, state);
    setState(resolved);
    onChange?.(state);
  };
  return [state, onChangeWrapper];
};

// src/components/user-action/Textarea.tsx
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
var Textarea = forwardRef(function Textarea2({
  id,
  onChange,
  onChangeText,
  onBlur,
  onEditCompleted,
  saveDelayOptions,
  defaultStyle = true,
  invalid = false,
  disabled = false,
  className,
  ...props
}, ref) {
  const { restartTimer, clearTimer } = useDelay(saveDelayOptions);
  const onEditCompletedWrapper = (text) => {
    onEditCompleted?.(text);
    clearTimer();
  };
  return /* @__PURE__ */ jsx2(
    "textarea",
    {
      ref,
      id,
      className: clsx2(
        "resize-none w-full h-32 overflow-y-scroll",
        "py-2 px-3 rounded-md border-2 border-transparent focus-style-none",
        {
          "bg-input-background text-input-text hover:border-primary focus:border-primary": !disabled && !invalid,
          "bg-negative/20 text-negative hover:border-negative focus-visible:border-negative": invalid && !disabled && defaultStyle,
          "text-disabled bg-disabled-background border-disabled-border": disabled && defaultStyle
        },
        className
      ),
      onChange: (event) => {
        const value = event.target.value;
        restartTimer(() => {
          onEditCompletedWrapper(value);
        });
        onChange?.(event);
        onChangeText?.(value);
      },
      onBlur: (event) => {
        onBlur?.(event);
        onEditCompletedWrapper(event.target.value);
      },
      disabled,
      ...props
    }
  );
});
var TextareaUncontrolled = ({
  value,
  onChangeText,
  ...props
}) => {
  const [text, setText] = useOverwritableState(value, onChangeText);
  return /* @__PURE__ */ jsx2(
    Textarea,
    {
      ...props,
      value: text,
      onChangeText: setText
    }
  );
};
var TextareaWithHeadline = ({
  id,
  headline,
  headlineProps,
  disabled,
  className,
  containerClassName,
  ...props
}) => {
  const genId = useId();
  const usedId = id ?? genId;
  return /* @__PURE__ */ jsxs(
    "div",
    {
      className: clsx2(
        "group flex-col-3 border-2 rounded-lg",
        {
          "bg-input-background text-input-text hover:border-primary focus-within:border-primary": !disabled,
          "border-disabled-border bg-disabled-background cursor-not-allowed": disabled
        },
        containerClassName
      ),
      children: [
        headline && /* @__PURE__ */ jsx2(Label, { size: "md", ...headlineProps, htmlFor: usedId, children: headline }),
        /* @__PURE__ */ jsx2(
          Textarea,
          {
            ...props,
            id: usedId,
            className: clsx2(
              "border-transparent focus:ring-0 focus-visible:ring-0 resize-none h-32",
              className
            ),
            defaultStyle: false
          }
        )
      ]
    }
  );
};
export {
  Textarea,
  TextareaUncontrolled,
  TextareaWithHeadline
};
//# sourceMappingURL=Textarea.js.map