import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { validateUrl, validateUrlProtocol } from "../utils/urlValidator.js";
import { normalizeUrl } from "../utils/urlHelper.js";
import React from "react";
import { useTextInputState } from "@pisell/utils";
import { Input } from "antd";
import { LinkOutlined } from "@ant-design/icons";
//#region src/components/pisellUrl/components/EditView.tsx
/**
* PisellUrl 编辑态视图组件
* 
* 职责：
* - 渲染编辑态 URL 输入框
* - 失焦时自动补全协议
* - 实时格式校验
* - 错误提示展示
* 
* @param props 组件 Props
*/
const EditView = (props) => {
	const { placeholder = "请输入 URL 地址", maxLength = 2048, allowClear = false, autoFocus = false, disabled = false, className, style, autoCompleteProtocol = true, defaultProtocol = "https://", strictValidation = true, allowedProtocols, requireProtocol = false, errorMessage, onProtocolComplete } = props;
	const { value, isValid, errors, handleChange, handleBlur, handleFocus } = useTextInputState(_objectSpread2(_objectSpread2({}, props), {}, { validator: (val) => {
		if (!val) return true;
		const normalizedUrl = autoCompleteProtocol ? normalizeUrl(val, defaultProtocol) : val;
		if (strictValidation && !validateUrl(normalizedUrl)) return errorMessage || "URL 地址格式不正确";
		if (requireProtocol && !val.match(/^[a-z]+:\/\//i)) return "URL 必须包含协议（如 http:// 或 https://）";
		if (allowedProtocols && allowedProtocols.length > 0) {
			const protocolValidation = validateUrlProtocol(normalizedUrl, allowedProtocols);
			if (!protocolValidation.isValid) return protocolValidation.errors[0];
		}
		return true;
	} }));
	const handleBlurWithProtocol = () => {
		if (autoCompleteProtocol && value && !value.match(/^[a-z]+:\/\//i)) {
			const normalized = normalizeUrl(value, defaultProtocol);
			if (normalized !== value) {
				handleChange(normalized);
				onProtocolComplete === null || onProtocolComplete === void 0 || onProtocolComplete(normalized);
			}
		}
		handleBlur();
	};
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-url-edit" }, /* @__PURE__ */ React.createElement(Input, {
		type: "url",
		value,
		onChange: (e) => handleChange(e.target.value),
		onBlur: handleBlurWithProtocol,
		onFocus: handleFocus,
		prefix: /* @__PURE__ */ React.createElement(LinkOutlined, null),
		placeholder,
		maxLength,
		allowClear,
		autoFocus,
		disabled,
		status: isValid ? void 0 : "error",
		className,
		style,
		"aria-label": props["aria-label"],
		"aria-describedby": props["aria-describedby"]
	}), !isValid && errors.length > 0 && /* @__PURE__ */ React.createElement("div", { className: "pisell-url-edit-error" }, errors[0]));
};
//#endregion
export { EditView as default };
