const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_useDataSource = require("../../hooks/useDataSource.js");
const require_utils = require("../../dataSourceForm/utils.js");
const require_serve = require("./serve.js");
const require_ReadPretty = require("./ReadPretty.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
//#region src/components/dataSourceComponents/fields/Input.Subdomain/WithMode.tsx
const _excluded = [
	"onBlur",
	"onChange",
	"rootDomain",
	"customDomain",
	"value",
	"tenantId",
	"fieldKey"
];
/**
* 子域名输入组件
* 
* 校验规则：
* 1. 长度限制：
*    - 最小长度：2个字符
*    - 最大长度：60个字符
* 
* 2. 字符限制：
*    - 只允许小写字母、数字和连字符(-)
*    - 不能全为数字
*    - 不能以连字符开头或结尾
*    - 不能包含连续的连字符(--)
* 
* 格式示例：
* - 有效：my-subdomain
* - 有效：web123
* - 无效：123 (全数字)
* - 无效：-subdomain (以连字符开头)
* - 无效：subdomain- (以连字符结尾)
* - 无效：sub--domain (包含连续连字符)
* 
* 使用方式：
* <Subdomain
*   name="subdomain"
*   label="子域名"
*   typeobj={{
*     type: 'subdomain',
*     enabled: true
*   }}
* />
*/
const Subdomain = (props) => {
	var _coreData$tenant;
	const { onBlur: propsOnBlur, onChange: propsOnChange, rootDomain = "xzero", customDomain, value, tenantId, fieldKey } = props, rest = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const { __designMode } = require_useDataSource.default();
	const context = require_useEngineContext.default();
	const [subdomain, setSubdomain] = (0, react.useState)("");
	const [suffix, setSuffix] = (0, react.useState)("");
	const coreData = (0, react.useMemo)(() => {
		var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
		return (_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.data) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.store) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getStore) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.call(_context$appHelper$ut2)) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut3.getDataByModel) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.call(_context$appHelper$ut3, "core", "core");
	}, [context]);
	const tenantData = require_serve.useTenantData(rootDomain, coreData === null || coreData === void 0 || (_coreData$tenant = coreData.tenant) === null || _coreData$tenant === void 0 ? void 0 : _coreData$tenant.id);
	const parseExternalValue = (val) => {
		if (!val || !val.trim() || val.trim() === ".") return {
			subdomain: "",
			suffix: suffixEmpty()
		};
		try {
			const parts = val.replace(/\.+/g, ".").split(".");
			if (parts[0] === "" && parts.length > 1) return {
				subdomain: "",
				suffix: `.${parts.slice(1).join(".")}`
			};
			if (parts.length === 1) return {
				subdomain: parts[0].trim(),
				suffix: suffixEmpty()
			};
			return {
				subdomain: parts[0].trim(),
				suffix: `.${parts.slice(1).join(".")}`
			};
		} catch (error) {
			console.error("Error parsing value:", error);
			return {
				subdomain: "",
				suffix: suffixEmpty()
			};
		}
	};
	(0, react.useEffect)(() => {
		try {
			if (value) {
				const { subdomain: newSubdomain, suffix: newSuffix } = parseExternalValue(value);
				setSubdomain(newSubdomain);
				setSuffix(newSuffix);
			} else {
				let newSuffix = "";
				switch (rootDomain) {
					case "xzero":
						newSuffix = (coreData === null || coreData === void 0 ? void 0 : coreData.tenant_domain) ? `.${coreData.tenant_domain}` : suffixEmpty();
						break;
					case "saas":
						newSuffix = tenantData ? `.${tenantData["saas.domain"]}` : suffixEmpty();
						break;
					case "custom":
						if (customDomain) {
							const normalizedDomain = customDomain.replace(/\.+/g, ".");
							newSuffix = normalizedDomain.startsWith(".") ? normalizedDomain : `.${normalizedDomain}`;
						} else newSuffix = suffixEmpty();
						break;
					default: newSuffix = suffixEmpty();
				}
				setSuffix(newSuffix);
			}
		} catch (error) {
			console.error("Error updating domain:", error);
			setSuffix(suffixEmpty());
		}
	}, [
		value,
		rootDomain,
		customDomain,
		tenantData,
		coreData
	]);
	const handleChange = (0, react.useCallback)((e) => {
		try {
			const { subdomain: newSubdomain } = parseExternalValue(e.target.value.toLowerCase());
			setSubdomain(newSubdomain);
			propsOnChange === null || propsOnChange === void 0 || propsOnChange(newSubdomain ? newSubdomain + suffix : "");
		} catch (error) {
			console.error("Error handling change:", error);
		}
	}, [
		suffix,
		propsOnChange,
		parseExternalValue
	]);
	const onBlur = (0, react.useCallback)((e) => {
		try {
			propsOnBlur === null || propsOnBlur === void 0 || propsOnBlur(e);
			propsOnChange === null || propsOnChange === void 0 || propsOnChange(subdomain ? subdomain + suffix : "");
		} catch (error) {
			console.error("Error handling blur:", error);
		}
	}, [
		propsOnBlur,
		propsOnChange,
		subdomain,
		suffix
	]);
	const suffixEmpty = () => {
		if (__designMode === "design") return ".root-domain.example";
		else return "error";
	};
	return /* @__PURE__ */ react.default.createElement(antd.Input, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, rest), {}, {
		value: subdomain,
		onChange: handleChange,
		onBlur,
		addonAfter: suffix
	}));
};
const SubdomainlWithMode = require_utils.withMode(Subdomain, require_ReadPretty.default);
//#endregion
exports.default = SubdomainlWithMode;
