import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { validateEmail, validateEmailDomain, validateEmailStrict } from "../utils/emailValidator.js";
import { COMMON_EMAIL_DOMAINS } from "../constants/emailDomains.js";
import { getEmailSuggestions } from "../utils/domainSuggestions.js";
import React, { useMemo, useState } from "react";
import { useTextInputState } from "@pisell/utils";
import { AutoComplete, Input } from "antd";
import { MailOutlined } from "@ant-design/icons";
//#region src/components/pisellEmail/components/EditView.tsx
/**
* PisellEmail 编辑态视图组件
* 
* 职责：
* - 渲染编辑态邮箱输入框
* - 提供域名智能补全（基于 AutoComplete）
* - 实时格式校验（标准/严格）
* - 错误提示展示
* 
* @param props 组件 Props
*/
const EditView = (props) => {
	const { placeholder = "请输入邮箱地址", maxLength = 254, allowClear = false, autoFocus = false, disabled = false, className, style, enableAutoComplete = true, autoCompleteDomains, strictValidation = false, allowedDomains, blockedDomains, errorMessage, onSelect } = props;
	const [suggestions, setSuggestions] = useState([]);
	const domains = useMemo(() => {
		return autoCompleteDomains || COMMON_EMAIL_DOMAINS;
	}, [autoCompleteDomains]);
	const { value, isValid, errors, handleChange, handleBlur, handleFocus } = useTextInputState(_objectSpread2(_objectSpread2({}, props), {}, { validator: (val) => {
		if (!val) return true;
		if (!(strictValidation ? validateEmailStrict(val) : validateEmail(val))) return errorMessage || "邮箱地址格式不正确";
		const domainValidation = validateEmailDomain(val, allowedDomains, blockedDomains);
		if (!domainValidation.isValid) return domainValidation.errors[0];
		return true;
	} }));
	const handleSearch = (searchText) => {
		if (enableAutoComplete) setSuggestions(getEmailSuggestions(searchText, domains));
	};
	const handleInputChange = (val) => {
		handleChange(val);
		handleSearch(val);
	};
	const handleSelectSuggestion = (val) => {
		handleChange(val);
		onSelect === null || onSelect === void 0 || onSelect(val);
	};
	const options = suggestions.map((email) => ({ value: email }));
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-email-edit" }, /* @__PURE__ */ React.createElement(AutoComplete, {
		value,
		onChange: handleInputChange,
		onSelect: handleSelectSuggestion,
		onBlur: handleBlur,
		onFocus: handleFocus,
		options,
		style: { width: "100%" },
		disabled,
		status: isValid ? void 0 : "error",
		popupClassName: "pisell-email-autocomplete-dropdown"
	}, /* @__PURE__ */ React.createElement(Input, {
		type: "email",
		prefix: /* @__PURE__ */ React.createElement(MailOutlined, null),
		placeholder,
		maxLength,
		allowClear,
		autoFocus,
		className,
		style,
		"aria-label": props["aria-label"],
		"aria-describedby": props["aria-describedby"]
	})), !isValid && errors.length > 0 && /* @__PURE__ */ React.createElement("div", { className: "pisell-email-edit-error" }, errors[0]));
};
//#endregion
export { EditView as default };
