import { useSenderContext } from "../context.js";
import { createVNode, defineComponent, mergeProps, ref } from "vue";
import { TextArea } from "antdv-next";
//#region components/sender/components/TextArea.tsx
var TextArea_default = /* @__PURE__ */ defineComponent({
	name: "SenderTextArea",
	setup(_, { expose }) {
		const senderCtx = useSenderContext();
		const inputRef = ref();
		const isComposing = ref(false);
		const getNativeEl = () => {
			return inputRef.value?.nativeElement ?? null;
		};
		expose({
			get nativeElement() {
				return getNativeEl();
			},
			focus(options) {
				const el = getNativeEl();
				if (!el) return;
				const { cursor, ...nativeOptions } = options ?? {};
				el.focus(nativeOptions);
				if (cursor) {
					const len = el.value.length;
					if (cursor === "start") el.setSelectionRange(0, 0);
					else if (cursor === "end") el.setSelectionRange(len, len);
					else if (cursor === "all") el.setSelectionRange(0, len);
				}
			},
			blur() {
				getNativeEl()?.blur();
			},
			clear() {
				senderCtx.value.onChange?.("", void 0);
			},
			getValue() {
				return { value: senderCtx.value.value ?? "" };
			},
			insert(text, position = "cursor") {
				const el = getNativeEl();
				const currentValue = senderCtx.value.value ?? "";
				let newValue;
				if (position === "start") newValue = text + currentValue;
				else if (position === "end") newValue = currentValue + text;
				else {
					const cursorPos = el?.selectionStart ?? currentValue.length;
					newValue = currentValue.slice(0, cursorPos) + text + currentValue.slice(cursorPos);
				}
				senderCtx.value.onChange?.(newValue, void 0);
			}
		});
		const onInternalKeyDown = (e) => {
			const ctx = senderCtx.value;
			const eventRes = ctx.onKeyDown?.(e);
			const { key, shiftKey, ctrlKey, altKey, metaKey } = e;
			if (isComposing.value || key !== "Enter" || eventRes === false) return;
			const submitType = ctx.submitType ?? "enter";
			const isModifierPressed = ctrlKey || altKey || metaKey;
			if (submitType === "enter" && !shiftKey && !isModifierPressed || submitType === "shiftEnter" && shiftKey && !isModifierPressed) {
				e.preventDefault();
				ctx.triggerSend?.();
			}
		};
		const onInternalPaste = (e) => {
			const ctx = senderCtx.value;
			const files = e.clipboardData?.files;
			if (!e.clipboardData?.getData("text/plain") && files?.length && ctx.onPasteFile) {
				ctx.onPasteFile(files);
				e.preventDefault();
			}
			ctx.onPaste?.(e);
		};
		return () => {
			const ctx = senderCtx.value;
			return createVNode(TextArea, mergeProps({
				"ref": inputRef,
				"disabled": ctx.disabled,
				"style": ctx.styles?.input,
				"class": [`${ctx.prefixCls}-input`, ctx.classNames?.input],
				"autoSize": ctx.autoSize,
				"value": ctx.value,
				"onChange": (e) => {
					const target = e.target;
					ctx.onChange?.(target.value, e);
				},
				"onKeyup": ctx.onKeyUp,
				"onCompositionstart": () => {
					isComposing.value = true;
				},
				"onCompositionend": () => {
					isComposing.value = false;
				},
				"onKeydown": onInternalKeyDown
			}, { onPaste: onInternalPaste }, {
				"variant": "borderless",
				"readonly": ctx.readOnly,
				"placeholder": ctx.placeholder,
				"onFocus": ctx.onFocus,
				"onBlur": ctx.onBlur
			}), null);
		};
	}
});
//#endregion
export { TextArea_default as default };
