var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/dataSourceComponents/fields/Input.Subdomain/WithMode.tsx
var WithMode_exports = {};
__export(WithMode_exports, {
  default: () => WithMode_default
});
module.exports = __toCommonJS(WithMode_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_serve = require("./serve");
var import_ReadPretty = __toESM(require("./ReadPretty"));
var import_utils = require("../../dataSourceForm/utils");
var import_useDataSource = __toESM(require("../../hooks/useDataSource"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
var Subdomain = (props) => {
  var _a;
  const {
    onBlur: propsOnBlur,
    onChange: propsOnChange,
    rootDomain = "xzero",
    customDomain,
    value,
    tenantId,
    fieldKey,
    ...rest
  } = props;
  const { __designMode } = (0, import_useDataSource.default)();
  const context = (0, import_useEngineContext.default)();
  const [subdomain, setSubdomain] = (0, import_react.useState)("");
  const [suffix, setSuffix] = (0, import_react.useState)("");
  const coreData = (0, import_react.useMemo)(() => {
    var _a2, _b, _c, _d, _e, _f, _g, _h;
    return (_h = (_g = (_f = (_e = (_d = (_c = (_b = (_a2 = context.appHelper.utils) == null ? void 0 : _a2.getApp) == null ? void 0 : _b.call(_a2)) == null ? void 0 : _c.data) == null ? void 0 : _d.store) == null ? void 0 : _e.getStore) == null ? void 0 : _f.call(_e)) == null ? void 0 : _g.getDataByModel) == null ? void 0 : _h.call(_g, "core", "core");
  }, [context]);
  const tenantData = (0, import_serve.useTenantData)(rootDomain, (_a = coreData == null ? void 0 : coreData.tenant) == null ? void 0 : _a.id);
  const parseExternalValue = (val) => {
    if (!val || !val.trim() || val.trim() === ".") {
      return {
        subdomain: "",
        suffix: suffixEmpty()
      };
    }
    try {
      const normalizedVal = val.replace(/\.+/g, ".");
      const parts = normalizedVal.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, import_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 ? 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, import_react.useCallback)((e) => {
    try {
      const inputValue = e.target.value.toLowerCase();
      const { subdomain: newSubdomain } = parseExternalValue(inputValue);
      setSubdomain(newSubdomain);
      propsOnChange == null ? void 0 : propsOnChange(newSubdomain ? newSubdomain + suffix : "");
    } catch (error) {
      console.error("Error handling change:", error);
    }
  }, [suffix, propsOnChange, parseExternalValue]);
  const onBlur = (0, import_react.useCallback)((e) => {
    try {
      propsOnBlur == null ? void 0 : propsOnBlur(e);
      propsOnChange == null ? 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__ */ import_react.default.createElement(
    import_antd.Input,
    {
      ...rest,
      value: subdomain,
      onChange: handleChange,
      onBlur,
      addonAfter: suffix
    }
  );
};
var SubdomainlWithMode = (0, import_utils.withMode)(Subdomain, import_ReadPretty.default);
var WithMode_default = SubdomainlWithMode;
