import { E as compilerRuntimeExports, T as useNavigate, J as useLocation, r as reactExports, j as jsxRuntimeExports, L as Link } from "./vendor-react-DYNCg6N5.js";
import { u as useLazyLoginQuery, a as useLazySettingsQuery, b as useActions, c as useAppSelector, r as redirectIfAuth, d as resetAllApiState, i as isSaaS, s as ssoRedirectLink, k as kerberosRedirectLink, e as isCrypt } from "./index-DfebxkL9.js";
import { L as LoginWraper } from "./index-q4s1wmSu.js";
import { u as useFetchToCrypt, p as preserveCursorPosition } from "./preserveCursorPosition-NFvD_VQV.js";
import { c as createStyles, n as useDisclosure, z as useForm, b as Box, T as Text, B as Button, E as TextInput, J as PasswordInput, G as Group, S as Stack } from "./vendor-mantine-h0WlM2Mj.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { I as IAuthTypeEnum } from "./types-B9VC9FeZ.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { o as objectType, s as stringType } from "./types-espwwxTQ.js";
import "./vendor-icons-xWhXK_Ui.js";
const useFetchToLogin = () => {
  const $ = compilerRuntimeExports.c(7);
  const navigate = useNavigate();
  const [fetchLogin] = useLazyLoginQuery();
  const [fetchSettings] = useLazySettingsQuery();
  const {
    setUserInfo,
    setUserTokens
  } = useActions();
  const {
    Settings
  } = useAppSelector(_temp$1);
  let t0;
  if ($[0] !== Settings || $[1] !== fetchLogin || $[2] !== fetchSettings || $[3] !== navigate || $[4] !== setUserInfo || $[5] !== setUserTokens) {
    t0 = async (t1) => {
      const {
        values,
        from
      } = t1;
      const response = await fetchLogin(values);
      const error = response.error;
      if (!error) {
        if (response.data) {
          const requestParams = new URL(document.location.href).searchParams;
          const returnUrl = requestParams.get("returnUrl");
          if (returnUrl) {
            window.location.href = returnUrl;
            return;
          }
          redirectIfAuth();
          resetAllApiState();
          navigate(from, {
            replace: true
          });
          setUserInfo(response.data.User);
          setUserTokens(response.data.Tokens);
          if (isSaaS(Settings)) {
            localStorage.setItem("isFirstLogin", "true");
          }
        }
        await fetchSettings();
      }
    };
    $[0] = Settings;
    $[1] = fetchLogin;
    $[2] = fetchSettings;
    $[3] = navigate;
    $[4] = setUserInfo;
    $[5] = setUserTokens;
    $[6] = t0;
  } else {
    t0 = $[6];
  }
  return t0;
};
function _temp$1(state) {
  return state.userInfo;
}
const useStyles = createStyles((theme, _, utils) => ({
  wrapper: {
    minHeight: "100vh",
    backgroundSize: "cover",
    backgroundColor: theme.colors.backgroundGray[0],
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    backgroundImage: `url(/src/assets/img/login-bg.jpeg)`
  },
  form: {
    // borderRight: `1px solid ${
    //   theme.colorScheme === 'dark' ? theme.colors.dark[7] : theme.colors.gray[3]
    // }`,
    position: "relative",
    maxWidth: 450,
    width: "100%",
    padding: 40,
    borderRadius: theme.spacing.xs,
    [`@media (max-width: ${theme.breakpoints.sm}px)`]: {
      maxWidth: "100%"
    }
  },
  title: {
    [utils.dark]: {
      color: theme.white
    },
    [utils.light]: {
      color: theme.black
    },
    fontFamily: `Greycliff CF, ${theme.fontFamily}`
  },
  logo: {
    [utils.dark]: {
      color: theme.white
    },
    [utils.light]: {
      color: theme.black
    },
    width: 120,
    display: "block",
    marginLeft: "auto",
    marginRight: "auto"
  },
  responsiveButton: {
    padding: 0,
    [`@media (max-width: 475px)`]: {
      width: "100%"
    }
  },
  responsiveInput: {
    [`@media (max-width: 475px)`]: {
      width: "100%"
    }
  },
  controls: {
    [`@media (max-width: 475px)`]: {
      width: "100%",
      span: {
        fontSize: "12px"
      },
      "button, a": {
        padding: 0
      }
    },
    "& .mantine-Input-input": {
      gap: "13px"
    },
    "& .mantine-InputWrapper-error": {
      marginBottom: "10px",
      marginTop: "10px",
      fontSize: "14px",
      position: "relative"
    }
  },
  itemsGroup: {
    justifyContent: "space-between",
    [`@media (max-width: 475px)`]: {
      justifyContent: "end"
    }
  },
  itemLink: {
    justifyContent: "end"
  },
  linkWrapper: {
    display: "flex",
    alignItems: "center",
    marginBottom: "25px",
    marginTop: "-10px"
  },
  subtitle: {
    color: theme.colors.gray[6],
    [`@media (max-width: 475px)`]: {
      fontSize: "12px"
    }
  },
  link: {
    padding: 0,
    fontWeight: 400,
    marginLeft: "5px",
    fontSize: "16px",
    height: "auto",
    [`@media (max-width: 475px)`]: {
      fontSize: "12px"
    },
    "&:hover": {
      textDecoration: "underline",
      backgroundColor: "transparent"
    }
  },
  footerControls: {
    display: "flex",
    justifyContent: "space-between",
    alignItems: "center",
    [`@media (min-width: 475px)`]: {
      flexWrap: "nowrap"
    }
  },
  subtleButton: {
    fontWeight: 100,
    padding: "0!important",
    marginTop: "-10px",
    alignSelf: "flex-start",
    "&:hover": {
      textDecoration: "underline",
      backgroundColor: "transparent"
    }
  },
  mainWrapper: {
    padding: 10
  }
}));
const Login = () => {
  var _a, _b;
  const $ = compilerRuntimeExports.c(103);
  const {
    t
  } = useTranslation();
  const location = useLocation();
  const {
    Settings
  } = useAppSelector(_temp);
  const [isLoadingSubmit, setIsLoadingSubmit] = reactExports.useState(false);
  const [, t0] = useLazySettingsQuery();
  const {
    data: settingsData
  } = t0;
  const {
    setGlobalSettings
  } = useActions();
  const fetchToLogin = useFetchToLogin();
  const fetchToCrypt = useFetchToCrypt();
  const from = ((_b = (_a = location.state) == null ? void 0 : _a.from) == null ? void 0 : _b.pathname) || "/users";
  const {
    classes
  } = useStyles();
  const passwordInputRef = reactExports.useRef(null);
  const [visible, t1] = useDisclosure(false);
  const {
    toggle: togglePasswordVisibility
  } = t1;
  let t2;
  if ($[0] !== t) {
    t2 = objectType({
      Login: stringType().min(1, {
        message: t("validation.enterLoginOrEmail")
      }).max(255, {
        message: t("validation.maxLengthSymbolsWithCount.symbols", {
          count: 255
        })
      }),
      Password: stringType().min(1, {
        message: t("validation.minLengthSymbolsWithCount.symbols", {
          count: 1
        })
      }).max(30, {
        message: t("validation.maxLengthSymbolsWithCount.symbols", {
          count: 30
        })
      }).refine(_temp2, {
        message: t("validation.passwordStartsWithSpace")
      }),
      CustomerCode: stringType().max(64, {
        message: t("validation.maxLengthSymbolsWithCount.symbols", {
          count: 64
        })
      }).nullable().optional()
    });
    $[0] = t;
    $[1] = t2;
  } else {
    t2 = $[1];
  }
  const validateLoginScheme = t2;
  let t3;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = {
      Login: "",
      Password: "",
      CustomerCode: void 0
    };
    $[2] = t3;
  } else {
    t3 = $[2];
  }
  let t4;
  if ($[3] !== validateLoginScheme) {
    t4 = {
      initialValues: t3,
      validateInputOnChange: true,
      validate: (values) => getValidateErrors(validateLoginScheme, values)
    };
    $[3] = validateLoginScheme;
    $[4] = t4;
  } else {
    t4 = $[4];
  }
  const form = useForm(t4);
  let t5;
  if ($[5] !== (Settings == null ? void 0 : Settings.AuthType)) {
    t5 = () => {
      switch (Settings == null ? void 0 : Settings.AuthType) {
        case IAuthTypeEnum.KERBEROS: {
          return kerberosRedirectLink;
        }
        default: {
          return ssoRedirectLink;
        }
      }
    };
    $[5] = Settings == null ? void 0 : Settings.AuthType;
    $[6] = t5;
  } else {
    t5 = $[6];
  }
  const getSsoRedirectLink = t5;
  let t6;
  if ($[7] !== (Settings == null ? void 0 : Settings.AuthType) || $[8] !== t) {
    t6 = () => {
      switch (Settings == null ? void 0 : Settings.AuthType) {
        case IAuthTypeEnum.ESIA: {
          return t("login.enterESIA");
        }
        case IAuthTypeEnum.GASU: {
          return t("login.enterGASU");
        }
        case IAuthTypeEnum.SAML2: {
          return t("login.enterSSO");
        }
        case IAuthTypeEnum.KERBEROS: {
          return t("login.enterKerberos");
        }
        default: {
          return null;
        }
      }
    };
    $[7] = Settings == null ? void 0 : Settings.AuthType;
    $[8] = t;
    $[9] = t6;
  } else {
    t6 = $[9];
  }
  const getSsoButtonText = t6;
  let t7;
  if ($[10] !== fetchToLogin || $[11] !== from) {
    t7 = (encryptedBase64, values_0) => {
      const newValues = {
        ...values_0,
        Password: "",
        CryptedPassword: encryptedBase64
      };
      fetchToLogin({
        values: newValues,
        from
      });
    };
    $[10] = fetchToLogin;
    $[11] = from;
    $[12] = t7;
  } else {
    t7 = $[12];
  }
  const handleFetchToCryptSuccess = t7;
  let t8;
  if ($[13] !== Settings || $[14] !== fetchToCrypt || $[15] !== fetchToLogin || $[16] !== from || $[17] !== handleFetchToCryptSuccess) {
    t8 = async (values_1) => {
      const trimmedValues = {
        ...values_1,
        Login: values_1.Login.trim()
      };
      setIsLoadingSubmit(true);
      if (isCrypt(Settings)) {
        await fetchToCrypt(trimmedValues.Password, (encryptedBase64_0) => handleFetchToCryptSuccess(encryptedBase64_0, trimmedValues));
      } else {
        await fetchToLogin({
          values: trimmedValues,
          from
        });
      }
      setIsLoadingSubmit(false);
    };
    $[13] = Settings;
    $[14] = fetchToCrypt;
    $[15] = fetchToLogin;
    $[16] = from;
    $[17] = handleFetchToCryptSuccess;
    $[18] = t8;
  } else {
    t8 = $[18];
  }
  const handleSubmit = t8;
  let t9;
  if ($[19] !== setGlobalSettings || $[20] !== settingsData) {
    t9 = () => {
      if (settingsData) {
        setGlobalSettings(settingsData);
      }
    };
    $[19] = setGlobalSettings;
    $[20] = settingsData;
    $[21] = t9;
  } else {
    t9 = $[21];
  }
  let t10;
  if ($[22] !== settingsData) {
    t10 = [settingsData];
    $[22] = settingsData;
    $[23] = t10;
  } else {
    t10 = $[23];
  }
  reactExports.useEffect(t9, t10);
  let t11;
  if ($[24] !== (Settings == null ? void 0 : Settings.AuthType)) {
    t11 = (Settings == null ? void 0 : Settings.AuthType) && (Settings == null ? void 0 : Settings.AuthType) !== "" && Object.values(IAuthTypeEnum).includes(Settings == null ? void 0 : Settings.AuthType);
    $[24] = Settings == null ? void 0 : Settings.AuthType;
    $[25] = t11;
  } else {
    t11 = $[25];
  }
  const isValidAuthType = t11;
  let t12;
  if ($[26] !== togglePasswordVisibility) {
    t12 = () => {
      preserveCursorPosition(passwordInputRef, togglePasswordVisibility);
    };
    $[26] = togglePasswordVisibility;
    $[27] = t12;
  } else {
    t12 = $[27];
  }
  const fixCursorLocation = t12;
  let t13;
  if ($[28] !== t) {
    t13 = t("login.enter");
    $[28] = t;
    $[29] = t13;
  } else {
    t13 = $[29];
  }
  let t14;
  if ($[30] !== Settings || $[31] !== classes.link || $[32] !== classes.linkWrapper || $[33] !== classes.subtitle || $[34] !== t) {
    t14 = isSaaS(Settings) && /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.linkWrapper, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.subtitle, children: t("register.hasNotAccount") }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { className: classes.link, component: Link, to: "/register", variant: "subtle", children: t("login.register") })
    ] });
    $[30] = Settings;
    $[31] = classes.link;
    $[32] = classes.linkWrapper;
    $[33] = classes.subtitle;
    $[34] = t;
    $[35] = t14;
  } else {
    t14 = $[35];
  }
  let t15;
  if ($[36] !== form || $[37] !== handleSubmit) {
    t15 = form.onSubmit(handleSubmit);
    $[36] = form;
    $[37] = handleSubmit;
    $[38] = t15;
  } else {
    t15 = $[38];
  }
  const t16 = classes.controls;
  let t17;
  if ($[39] !== Settings || $[40] !== t) {
    t17 = isSaaS(Settings) ? t("login.login") : t("login.loginBox");
    $[39] = Settings;
    $[40] = t;
    $[41] = t17;
  } else {
    t17 = $[41];
  }
  let t18;
  if ($[42] !== form) {
    t18 = form.getInputProps("Login");
    $[42] = form;
    $[43] = t18;
  } else {
    t18 = $[43];
  }
  let t19;
  if ($[44] !== t17 || $[45] !== t18) {
    t19 = /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { size: "lg", radius: "md", variant: "filled", placeholder: t17, "data-cy": "usernameInput", ...t18 });
    $[44] = t17;
    $[45] = t18;
    $[46] = t19;
  } else {
    t19 = $[46];
  }
  let t20;
  if ($[47] !== t) {
    t20 = t("login.password");
    $[47] = t;
    $[48] = t20;
  } else {
    t20 = $[48];
  }
  let t21;
  if ($[49] !== form) {
    t21 = form.getInputProps("Password");
    $[49] = form;
    $[50] = t21;
  } else {
    t21 = $[50];
  }
  let t22;
  if ($[51] !== fixCursorLocation || $[52] !== t20 || $[53] !== t21 || $[54] !== visible) {
    t22 = /* @__PURE__ */ jsxRuntimeExports.jsx(PasswordInput, { size: "lg", radius: "md", variant: "filled", placeholder: t20, "data-cy": "passwordInput", ref: passwordInputRef, visible, onVisibilityChange: fixCursorLocation, ...t21 });
    $[51] = fixCursorLocation;
    $[52] = t20;
    $[53] = t21;
    $[54] = visible;
    $[55] = t22;
  } else {
    t22 = $[55];
  }
  let t23;
  if ($[56] !== Settings || $[57] !== classes.itemLink || $[58] !== classes.itemsGroup) {
    t23 = isSaaS(Settings) ? classes.itemLink : classes.itemsGroup;
    $[56] = Settings;
    $[57] = classes.itemLink;
    $[58] = classes.itemsGroup;
    $[59] = t23;
  } else {
    t23 = $[59];
  }
  let t24;
  if ($[60] === Symbol.for("react.memo_cache_sentinel")) {
    t24 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, {});
    $[60] = t24;
  } else {
    t24 = $[60];
  }
  const t25 = classes.subtleButton;
  let t26;
  if ($[61] !== t) {
    t26 = t("login.forgotPassword");
    $[61] = t;
    $[62] = t26;
  } else {
    t26 = $[62];
  }
  let t27;
  if ($[63] !== classes.subtleButton || $[64] !== t26) {
    t27 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { component: Link, to: "/password-reset", variant: "subtle", className: t25, size: "md", children: t26 });
    $[63] = classes.subtleButton;
    $[64] = t26;
    $[65] = t27;
  } else {
    t27 = $[65];
  }
  let t28;
  if ($[66] !== t23 || $[67] !== t27) {
    t28 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Group, { justify: "space-between", className: t23, children: [
      t24,
      t27
    ] });
    $[66] = t23;
    $[67] = t27;
    $[68] = t28;
  } else {
    t28 = $[68];
  }
  let t29;
  if ($[69] !== classes.controls || $[70] !== t19 || $[71] !== t22 || $[72] !== t28) {
    t29 = /* @__PURE__ */ jsxRuntimeExports.jsx(Stack, { gap: "xs", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { gap: "xs", className: t16, children: [
      t19,
      t22,
      t28
    ] }) });
    $[69] = classes.controls;
    $[70] = t19;
    $[71] = t22;
    $[72] = t28;
    $[73] = t29;
  } else {
    t29 = $[73];
  }
  const t30 = classes.footerControls;
  let t31;
  if ($[74] !== Settings || $[75] !== classes.responsiveButton || $[76] !== getSsoButtonText || $[77] !== getSsoRedirectLink || $[78] !== isValidAuthType) {
    t31 = !isSaaS(Settings) && isValidAuthType && /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { variant: "subtle", className: classes.responsiveButton, size: "lg", fullWidth: true, onClick: () => window.location.href = getSsoRedirectLink(), children: getSsoButtonText() });
    $[74] = Settings;
    $[75] = classes.responsiveButton;
    $[76] = getSsoButtonText;
    $[77] = getSsoRedirectLink;
    $[78] = isValidAuthType;
    $[79] = t31;
  } else {
    t31 = $[79];
  }
  const t32 = classes.responsiveButton;
  let t33;
  if ($[80] !== form.errors) {
    t33 = Object.keys(form.errors);
    $[80] = form.errors;
    $[81] = t33;
  } else {
    t33 = $[81];
  }
  const t34 = t33.length !== 0;
  let t35;
  if ($[82] !== t) {
    t35 = t("login.enter");
    $[82] = t;
    $[83] = t35;
  } else {
    t35 = $[83];
  }
  let t36;
  if ($[84] !== classes.responsiveButton || $[85] !== isLoadingSubmit || $[86] !== t34 || $[87] !== t35) {
    t36 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { loading: isLoadingSubmit, className: t32, type: "submit", size: "lg", fullWidth: true, "data-cy": "submitButton", disabled: t34, children: t35 });
    $[84] = classes.responsiveButton;
    $[85] = isLoadingSubmit;
    $[86] = t34;
    $[87] = t35;
    $[88] = t36;
  } else {
    t36 = $[88];
  }
  let t37;
  if ($[89] !== classes.footerControls || $[90] !== t31 || $[91] !== t36) {
    t37 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Group, { className: t30, children: [
      t31,
      t36
    ] });
    $[89] = classes.footerControls;
    $[90] = t31;
    $[91] = t36;
    $[92] = t37;
  } else {
    t37 = $[92];
  }
  let t38;
  if ($[93] !== t29 || $[94] !== t37) {
    t38 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { gap: "lg", children: [
      t29,
      t37
    ] });
    $[93] = t29;
    $[94] = t37;
    $[95] = t38;
  } else {
    t38 = $[95];
  }
  let t39;
  if ($[96] !== t15 || $[97] !== t38) {
    t39 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { component: "form", onSubmit: t15, "data-cy": "loginForm", children: t38 });
    $[96] = t15;
    $[97] = t38;
    $[98] = t39;
  } else {
    t39 = $[98];
  }
  let t40;
  if ($[99] !== t13 || $[100] !== t14 || $[101] !== t39) {
    t40 = /* @__PURE__ */ jsxRuntimeExports.jsxs(LoginWraper, { title: t13, children: [
      t14,
      t39
    ] });
    $[99] = t13;
    $[100] = t14;
    $[101] = t39;
    $[102] = t40;
  } else {
    t40 = $[102];
  }
  return t40;
};
function _temp(state) {
  return state.userInfo;
}
function _temp2(value) {
  return !value.startsWith(" ");
}
export {
  Login
};
//# sourceMappingURL=index-DDtKBkCy.js.map
