import { isString } from "../utils/assertion.js";
import { filterUndefined } from "../utils/object.js";
import { cs } from "../utils/styles.js";
import "../utils/index.js";
import { clearIconSize, displayValueOnlyTextSize, helpTextMargin, helpTextSize, inputColors, inputStateMapping } from "./consts.js";
import { useStyleConfig } from "../core/theme.js";
import { styled } from "../core/styled.js";
import { vui } from "../core/vui.js";
import box_default from "../box/box.js";
import "../box/index.js";
import { T } from "../t/t.js";
import "../t/index.js";
import { IconButton } from "../button/buttons.js";
import "../button/index.js";
import "../core/index.js";
import { InputProvider } from "./context.js";
import helpText_default from "./helpText.js";
import label_default from "../label/label.js";
import "../label/index.js";
import autoCompletePopover_default from "./autoCompletePopover.js";
import { getInitialCount } from "./helpers.js";
import inputIcon_default from "./inputIcon.js";
import inputInput_default from "./inputInput.js";
import { useEffect, useId, useMemo, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/input/input.tsx
const InputBase = styled.divBox`
  align-items: center;
  color: sandstone.10;
  background-color: white;
  border-radius: none;
  border-width: 1px;
  display: flex;
  flex-shrink: 0;
  outline: none;
  position: relative;
  transition-duration: 0s;
  transform: translate3d(0, 0, 0);
  width: 100%;

  &[aria-disabled='true'] {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: sandstone.79;
    border-color: sandstone.79;
    & > .vui-icon {
      color: sandstone.50;
    }
    & > .vui-icon > path {
      fill: sandstone.50;
    }
  }
`;
/**
* Displays an input element wrapped in a div to allow extra content, like counter, side icons or buttons.
* Forwards many relevant props to the inner input. Handles different states, like loading or error.
* Exposes some props to the children via context.
*/
const Input = vui((props, ref) => {
	const { allowClear, ariaLabel, autoComplete, autoCompleteMaxHeight, autoCompleteOptions, autoFocus, children, className, defaultValue, disabled, displayValueOnly, errorText, helpText, iconLeft, iconRight, id: idProp, input, inputProps, inputRef, itemLeft, itemRight, label, labelMarkOptional, labelTooltipText, max, maxLength, min, minLength, name, onBlur, onChange: onChangeProp, onFocus, pattern, placeholder, readOnly, required, showCount, size = "lg", state = "", stateMapping, step, type = "text", value, variant, ...rest } = props;
	const generatedId = useId();
	const [count, setCount] = useState(getInitialCount(props));
	const [valueInternal, setValueInternal] = useState(defaultValue || "");
	const states = {
		...inputStateMapping,
		...stateMapping
	};
	const id = idProp || generatedId;
	const styles = useStyleConfig("Input", props);
	const context = useMemo(() => filterUndefined({
		disabled,
		size,
		variant
	}), [
		disabled,
		size,
		variant
	]);
	function onChange(e) {
		const inputValue = e.target.value;
		const value$1 = maxLength ? inputValue.slice(0, maxLength) : inputValue;
		setValueInternal(value$1);
		setCount(value$1?.length);
		onChangeProp?.(e);
	}
	function onAutoCompleteSelect(value$1) {
		onChange({
			target: { value: value$1 },
			type: "autoCompleteSelect"
		});
	}
	function onClear() {
		const value$1 = "";
		const e = {
			target: { value: value$1 },
			type: "onClear"
		};
		setValueInternal(value$1);
		setCount(0);
		onChange(e);
	}
	const aliasedProps = filterUndefined({
		"aria-disabled": disabled,
		bg: readOnly ? "sandstone.97" : void 0,
		borderColor: readOnly ? "sandstone.55" : void 0,
		focusWithinBorderColor: !readOnly ? "transparent" : void 0,
		focusWithinRingColor: readOnly ? "transparent" : void 0
	});
	useEffect(() => {
		if (value === void 0 && defaultValue !== void 0) return;
		setValueInternal(value ?? "");
		setCount(value?.toString()?.length ? `${value}`.length : 0);
	}, [value]);
	const filterAutoCompleteOptions = (i) => valueInternal === i ? false : !valueInternal || i.toLowerCase().includes(`${valueInternal}`.toLowerCase());
	return /* @__PURE__ */ jsx(InputProvider, {
		value: context,
		children: displayValueOnly ? /* @__PURE__ */ jsx(T, {
			className: cs("vui-input-value", className),
			size: displayValueOnlyTextSize[size],
			children: value || defaultValue
		}) : /* @__PURE__ */ jsx(autoCompletePopover_default, {
			autoCompleteMaxHeight,
			autoCompleteOptions,
			filterAutoCompleteOptions,
			onAutoCompleteSelect,
			children: /* @__PURE__ */ jsxs(box_default, {
				className: "vui-inputContainer",
				column: true,
				ref,
				...rest,
				children: [
					isString(label) ? /* @__PURE__ */ jsx(label_default, {
						"aria-disabled": disabled,
						className: "vui-inputLabel",
						htmlFor: id,
						markAsOptional: labelMarkOptional,
						mb: .5,
						text: label,
						tooltipText: labelTooltipText,
						...styles.label
					}) : label,
					/* @__PURE__ */ jsxs(InputBase, {
						className: cs("vui-input", className),
						...styles.container,
						...aliasedProps,
						children: [
							itemLeft,
							isString(iconLeft) ? /* @__PURE__ */ jsx(inputIcon_default, {
								ml: 1,
								name: iconLeft
							}) : iconLeft,
							children ?? input ?? /* @__PURE__ */ jsx(inputInput_default, {
								ref: inputRef,
								autoFocus,
								ariaLabel,
								disabled,
								id,
								max,
								maxLength,
								min,
								minLength,
								name,
								onBlur,
								onChange,
								onFocus,
								pattern,
								placeholder,
								readOnly,
								required,
								step,
								type,
								autoComplete: autoCompleteOptions?.length ? "off" : autoComplete,
								value: valueInternal,
								...inputProps
							}),
							isString(iconRight) ? /* @__PURE__ */ jsx(inputIcon_default, {
								mr: 1,
								name: iconRight
							}) : iconRight,
							itemRight,
							state && /* @__PURE__ */ jsx(inputIcon_default, {
								mr: 1,
								...states[state]?.iconProps
							}),
							allowClear && /* @__PURE__ */ jsx(IconButton, {
								disabled: !valueInternal,
								icon: "uiTimes",
								mr: .5,
								onClick: onClear,
								size: clearIconSize[size],
								title: "Clear"
							}),
							showCount && /* @__PURE__ */ jsxs(T, {
								className: "vui-inputCount",
								color: maxLength && count > maxLength ? inputColors.error : inputColors.helpText,
								position: "absolute",
								right: 0,
								size: "sm",
								top: "calc(100% + 1px)",
								children: [
									count,
									" ",
									maxLength ? `/ ${maxLength}` : null
								]
							})
						]
					}),
					!!helpText && /* @__PURE__ */ jsx(helpText_default, {
						size: helpTextSize[size],
						children: helpText
					}),
					!!errorText && /* @__PURE__ */ jsx(helpText_default, {
						isError: true,
						mr: showCount ? helpTextMargin : void 0,
						size: helpTextSize[size],
						children: errorText
					})
				]
			})
		})
	});
});
Input.Icon = inputIcon_default;
Input.Input = inputInput_default;
Input.HelpText = helpText_default;
Input.displayName = "Input";
var input_default = Input;

//#endregion
export { Input, InputBase, input_default as default };
globalThis.__vuiVersion__ = "4.0.0-beta.1"
//# sourceMappingURL=input.js.map