import { r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { ca as useLazyCheckUserExistFieldQuery } from "./index-DfebxkL9.js";
import { a as getNameFromDottedString } from "./utils-D5kCyTRM.js";
import { w as qXe } from "./vendor-icons-xWhXK_Ui.js";
import { u as useStyles } from "./styles-C9HVNQJ0.js";
import { V as ValidateFieldEnum } from "./types-B9VC9FeZ.js";
import { E as EmailActivationControl } from "./useTerminateSessions-Cb3ukMTY.js";
import { u as useDebounce } from "./useDebounce-DTubzIUC.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { E as TextInput, F as Flex, g as Loader, h as ActionIcon } from "./vendor-mantine-h0WlM2Mj.js";
const ValidateTextInput = ({
  form,
  fieldName,
  className,
  disabled,
  label,
  clearable,
  rightSection,
  isRequired,
  isConfirmed,
  userId,
  withAsterisk,
  customerId,
  placeholder,
  isSaasEmail,
  currentId,
  dataAttribute,
  description,
  handleLoginChange,
  setIsValidEmail,
  onChange,
  rightSectionWidth,
  createEmail,
  selectedDomain
}) => {
  const {
    classes,
    cx
  } = useStyles();
  const {
    t
  } = useTranslation();
  const [checkFieldExist, {
    isFetching: isFetchingValidate
  }] = useLazyCheckUserExistFieldQuery();
  const fieldNameEnum = getNameFromDottedString(fieldName);
  const currentInputValue = form.getInputProps(fieldName).value;
  const debounce = useDebounce(currentInputValue, 300);
  reactExports.useEffect(() => {
    handleValidateField(debounce);
  }, [debounce]);
  const handleValidateField = async (field) => {
    var _a, _b;
    if ((field == null ? void 0 : field.length) && !form.getInputProps(fieldName).error && form.isDirty(fieldName)) {
      const payload = isSaasEmail ? {
        existField: ValidateFieldEnum.LoginAndEmail,
        userId: currentId,
        field
      } : {
        existField: fieldNameEnum,
        field
      };
      const response = await checkFieldExist(payload);
      if (setIsValidEmail && fieldNameEnum === ValidateFieldEnum.Email && (response == null ? void 0 : response.data)) {
        setIsValidEmail(!((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.ExistsUser));
      }
      if ((_b = response == null ? void 0 : response.data) == null ? void 0 : _b.ExistsUser) {
        form.setFieldError(fieldName, `${t("profile.such")} ${fieldNameEnum === ValidateFieldEnum.Email ? t("profile.mail") : fieldNameEnum === ValidateFieldEnum.Phone ? t("profile.phone") : t("profile.loginExist")} ${t("profile.exists")}`);
      }
    }
  };
  const showAlertIcon = !isConfirmed && fieldNameEnum === ValidateFieldEnum.Email && !form.isDirty(fieldName) && (currentInputValue == null ? void 0 : currentInputValue.length);
  const clearValue = () => {
    form.setFieldValue(fieldName, "");
  };
  const rightSectionRender = () => {
    return /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { children: [
      isFetchingValidate && !createEmail ? /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "xs", m: "auto 0" }) : rightSection ? rightSection : currentInputValue && clearable ? /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { variant: "transparent", onClick: clearValue, children: /* @__PURE__ */ jsxRuntimeExports.jsx(qXe, { color: "#667085" }) }) : null,
      showAlertIcon && customerId ? /* @__PURE__ */ jsxRuntimeExports.jsx(EmailActivationControl, { Email: currentInputValue, Id: userId, customerId }) : null
    ] });
  };
  const handleChange = (event) => {
    if (onChange) {
      onChange(event);
    } else {
      form.getInputProps(fieldName).onChange(event);
    }
    if (handleLoginChange) {
      handleLoginChange();
    }
  };
  const handleBlur = (event_0) => {
    var _a;
    (_a = form.getInputProps(fieldName)) == null ? void 0 : _a.onBlur(event_0);
    handleValidateField(createEmail ? `${event_0.target.value}@${selectedDomain}` : event_0.target.value);
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { ...form.getInputProps(fieldName), className: cx(classes.textFieldInput, className), label, disabled, withAsterisk, required: isRequired, rightSection: rightSectionRender(), rightSectionWidth: rightSectionWidth ? rightSectionWidth : 60, error: form.getInputProps(fieldName).error, value: !createEmail ? form.getInputProps(fieldName).value || placeholder || "" : form.getInputProps(`Credentials.Email`).value.split("@")[0], onChange: handleChange, onBlur: handleBlur, "data-name": fieldName, "data-cy": dataAttribute, description });
};
export {
  ValidateTextInput as V
};
//# sourceMappingURL=validateTextInput-2E2Z5K9Z.js.map
