import { r as reactExports, a0 as useParams, E as compilerRuntimeExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { L as LanguageSelect, T as TimezoneSelect } from "./index-lG79Dd5P.js";
import { av as useAppDispatch, b as useActions, c as useAppSelector, aw as useUpdateSettingMutation, ax as defaultLang, ay as userApi, t as extractErrorCode, v as generateErrorMessage } from "./index-DfebxkL9.js";
import "./options-CRIRDAK6.js";
import "./index-leRPH4yM.js";
import { u as useTranslation, i as instance } from "./vendor-i18n-wisIXUFi.js";
import "./index.es-BTZFX8B-.js";
import "./index-C-CFogWu.js";
import "./index-3c9En-DB.js";
import "./date-bOIw2uwV.js";
import { u as useUpdateInfo, P as ProfileSectionEnum, a as ProfileUpdateTypeEnum } from "./useUpdateUser-innD962A.js";
import "./papaparse.min-Ds9XhCoe.js";
import { E as EditUserInfoContext } from "./index-TL41xEJi.js";
import "./passwordUtils-B3CN2t6O.js";
import "./ru-Bush41s2.js";
import "./index-CfN7Ll0p.js";
import "./errorIcon-PZO0f1IY.js";
import "./index-CUcivY9l.js";
import "./index-DnphFLKH.js";
import "./index-C2HWmCvh.js";
import { z as useForm, s as showNotification, d as Checkbox, T as Text, B as Button } from "./vendor-mantine-h0WlM2Mj.js";
import "./vendor-icons-xWhXK_Ui.js";
import "./index-C6QQOvbB.js";
import "./index-CcqTjOWY.js";
import "./useDebounce-DTubzIUC.js";
import "./index-C_Fw_IUU.js";
import "./index-_NgMYV3i.js";
import "./index-D4BAIihc.js";
import "./useCopyToClipboard-C-S2RacD.js";
import "./useTerminateSessions-Cb3ukMTY.js";
import "./index-DqcA8P_y.js";
import "./styles-3NoIlzov.js";
import "./index-DMTgSMwy.js";
import "./index-DFEP7Sv_.js";
import "./index-woYEqWvh.js";
import "./index-Ds4Bnwmy.js";
import "./validation-Ds00Zghx.js";
import "./types-espwwxTQ.js";
import "./format-CMT-x-V3.js";
import "./index-B7nmdYVM.js";
import "./utils-D5kCyTRM.js";
import "./types-B9VC9FeZ.js";
import "./index-BvQfhnV_.js";
import "./styles-C9HVNQJ0.js";
import "./index-72JBTuLT.js";
import "./index-CslanKI5.js";
import "./styles-Dnu4P1Zm.js";
import "./index-BWhdniX1.js";
import "./index-BoQKW1g7.js";
const useProfileLocaleTimezone = () => {
  var _a, _b, _c;
  const {
    t
  } = useTranslation();
  const dispatch = useAppDispatch();
  const {
    setTimezoneSettings
  } = useActions();
  const {
    userInfoCommon,
    currentId
  } = reactExports.useContext(EditUserInfoContext);
  const {
    id
  } = useParams();
  const {
    Settings
  } = useAppSelector((state) => state.userInfo);
  const myUserId = useAppSelector((state_0) => {
    var _a2;
    return (_a2 = state_0.userInfo.User) == null ? void 0 : _a2.Id;
  });
  const Timezone = Settings == null ? void 0 : Settings.Timezone;
  const isCurrentUser = myUserId === Number(id);
  const language = reactExports.useMemo(() => userInfoCommon == null ? void 0 : userInfoCommon.Culture, [userInfoCommon]);
  const [updateInfo] = useUpdateSettingMutation();
  const {
    saveUpdate,
    isLoading: isLoadingUserUpdate
  } = useUpdateInfo({
    isSilent: true
  });
  const [isLoading, setIsLoading] = reactExports.useState(false);
  const form = useForm({
    validateInputOnChange: true,
    initialValues: {
      language: language || defaultLang,
      checkbox: isCurrentUser ? (Timezone == null ? void 0 : Timezone.SecondEnabled) ?? false : Boolean((_a = userInfoCommon == null ? void 0 : userInfoCommon.TimeZone) == null ? void 0 : _a.SecondEnabled),
      mainTimezone: isCurrentUser ? String((Timezone == null ? void 0 : Timezone.Id) ?? 0) : String(((_b = userInfoCommon == null ? void 0 : userInfoCommon.TimeZone) == null ? void 0 : _b.Id) ?? 0),
      secondTimezone: isCurrentUser ? String((Timezone == null ? void 0 : Timezone.SecondId) ?? 0) : String(((_c = userInfoCommon == null ? void 0 : userInfoCommon.TimeZone) == null ? void 0 : _c.SecondId) ?? 0)
    }
  });
  reactExports.useEffect(() => {
    var _a2, _b2, _c2;
    if (userInfoCommon) {
      form.setValues({
        language: userInfoCommon.Culture || defaultLang,
        checkbox: ((_a2 = userInfoCommon.TimeZone) == null ? void 0 : _a2.SecondEnabled) ?? false,
        mainTimezone: String(((_b2 = userInfoCommon.TimeZone) == null ? void 0 : _b2.Id) ?? 0),
        secondTimezone: String(((_c2 = userInfoCommon.TimeZone) == null ? void 0 : _c2.SecondId) ?? 0)
      });
      form.resetDirty();
    }
  }, [userInfoCommon]);
  const handleSave = async () => {
    var _a2, _b2, _c2;
    if (!userInfoCommon) return;
    const data = form.getValues();
    setIsLoading(true);
    try {
      const languageChanged = data.language !== userInfoCommon.Culture;
      const timezoneChanged = Number(data.mainTimezone) !== (isCurrentUser ? Timezone == null ? void 0 : Timezone.Id : (_a2 = userInfoCommon.TimeZone) == null ? void 0 : _a2.Id) || data.checkbox !== (isCurrentUser ? Timezone == null ? void 0 : Timezone.SecondEnabled : (_b2 = userInfoCommon.TimeZone) == null ? void 0 : _b2.SecondEnabled) || Number(data.secondTimezone) !== (isCurrentUser ? Timezone == null ? void 0 : Timezone.SecondId : (_c2 = userInfoCommon.TimeZone) == null ? void 0 : _c2.SecondId);
      if (languageChanged) {
        await saveUpdate({
          Type: ProfileUpdateTypeEnum.Language,
          Data: {
            [ProfileSectionEnum.Language]: data.language,
            Id: currentId
          }
        });
        instance.changeLanguage(data.language);
      }
      if (timezoneChanged) {
        if (!isCurrentUser) {
          await saveUpdate({
            Type: ProfileUpdateTypeEnum.TimeZone,
            Data: {
              [ProfileSectionEnum.TimeZone]: {
                ...userInfoCommon.TimeZone,
                Id: Number(data.mainTimezone) || 0,
                SecondEnabled: data.checkbox || false,
                SecondId: Number(data.secondTimezone) || 0
              },
              Id: currentId
            }
          });
        } else {
          if (!Settings) return;
          const res = await updateInfo({
            ...Settings,
            Timezone: {
              ...Settings.Timezone,
              Id: Number(data.mainTimezone) || 0,
              SecondEnabled: data.checkbox ?? false,
              SecondId: Number(data.secondTimezone) || 0
            }
          });
          if ("error" in res) {
            throw res.error;
          }
          setTimezoneSettings({
            ...Settings == null ? void 0 : Settings.Timezone,
            Id: Number(data.mainTimezone) || 0,
            SecondEnabled: data.checkbox ?? false,
            SecondId: Number(data.secondTimezone) || 0
          });
        }
      }
      dispatch(userApi.util.invalidateTags(["Settings"]));
      showNotification({
        title: t("saved"),
        message: "",
        color: "green"
      });
      form.resetDirty();
    } catch (error) {
      const codeError = extractErrorCode(error);
      showNotification({
        title: generateErrorMessage(codeError),
        message: "",
        autoClose: true,
        color: "red",
        style: {
          zIndex: 1003
        }
      });
    } finally {
      setIsLoading(false);
    }
  };
  return {
    form,
    isLoading: isLoading || isLoadingUserUpdate,
    handleSave,
    isCurrentUser
  };
};
const wrapper = "_wrapper_1am72_1";
const title = "_title_1am72_10";
const mainTitle = "_mainTitle_1am72_16";
const section = "_section_1am72_22";
const classes = {
  wrapper,
  title,
  mainTitle,
  section
};
const ProfileLocale = () => {
  const $ = compilerRuntimeExports.c(72);
  const {
    t
  } = useTranslation();
  const {
    form,
    isLoading,
    handleSave
  } = useProfileLocaleTimezone();
  let t0;
  if ($[0] !== form || $[1] !== handleSave) {
    t0 = form.onSubmit(handleSave);
    $[0] = form;
    $[1] = handleSave;
    $[2] = t0;
  } else {
    t0 = $[2];
  }
  let t1;
  if ($[3] !== t) {
    t1 = t("profile.locale");
    $[3] = t;
    $[4] = t1;
  } else {
    t1 = $[4];
  }
  let t2;
  if ($[5] !== t1) {
    t2 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.mainTitle, children: t1 });
    $[5] = t1;
    $[6] = t2;
  } else {
    t2 = $[6];
  }
  let t3;
  if ($[7] !== t) {
    t3 = t("profile.language");
    $[7] = t;
    $[8] = t3;
  } else {
    t3 = $[8];
  }
  let t4;
  if ($[9] !== t3) {
    t4 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.title, children: t3 });
    $[9] = t3;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  let t5;
  if ($[11] !== t) {
    t5 = t("profile.localeInfo");
    $[11] = t;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  let t6;
  if ($[13] !== form) {
    t6 = form.getInputProps("language");
    $[13] = form;
    $[14] = t6;
  } else {
    t6 = $[14];
  }
  const t7 = t6.value;
  let t8;
  if ($[15] !== form) {
    t8 = form.getInputProps("language");
    $[15] = form;
    $[16] = t8;
  } else {
    t8 = $[16];
  }
  let t9;
  if ($[17] !== t5 || $[18] !== t6.value || $[19] !== t8) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsx(LanguageSelect, { label: t5, value: t7, ...t8 });
    $[17] = t5;
    $[18] = t6.value;
    $[19] = t8;
    $[20] = t9;
  } else {
    t9 = $[20];
  }
  let t10;
  if ($[21] !== t4 || $[22] !== t9) {
    t10 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.section, children: [
      t4,
      t9
    ] });
    $[21] = t4;
    $[22] = t9;
    $[23] = t10;
  } else {
    t10 = $[23];
  }
  let t11;
  if ($[24] !== t) {
    t11 = t("profile.timeZone");
    $[24] = t;
    $[25] = t11;
  } else {
    t11 = $[25];
  }
  let t12;
  if ($[26] !== t11) {
    t12 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.title, children: t11 });
    $[26] = t11;
    $[27] = t12;
  } else {
    t12 = $[27];
  }
  let t13;
  if ($[28] !== t) {
    t13 = t("profile.timezoneInfo");
    $[28] = t;
    $[29] = t13;
  } else {
    t13 = $[29];
  }
  let t14;
  if ($[30] !== form) {
    t14 = form.getInputProps("mainTimezone");
    $[30] = form;
    $[31] = t14;
  } else {
    t14 = $[31];
  }
  const t15 = t14.value;
  let t16;
  if ($[32] !== form) {
    t16 = form.getInputProps("mainTimezone");
    $[32] = form;
    $[33] = t16;
  } else {
    t16 = $[33];
  }
  let t17;
  if ($[34] !== t13 || $[35] !== t14.value || $[36] !== t16) {
    t17 = /* @__PURE__ */ jsxRuntimeExports.jsx(TimezoneSelect, { label: t13, value: t15, ...t16 });
    $[34] = t13;
    $[35] = t14.value;
    $[36] = t16;
    $[37] = t17;
  } else {
    t17 = $[37];
  }
  let t18;
  if ($[38] !== t12 || $[39] !== t17) {
    t18 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.section, children: [
      t12,
      t17
    ] });
    $[38] = t12;
    $[39] = t17;
    $[40] = t18;
  } else {
    t18 = $[40];
  }
  let t19;
  if ($[41] === Symbol.for("react.memo_cache_sentinel")) {
    t19 = {
      label: {
        cursor: "pointer"
      },
      labelWrapper: {
        width: "auto !important"
      }
    };
    $[41] = t19;
  } else {
    t19 = $[41];
  }
  let t20;
  if ($[42] !== t) {
    t20 = t("profile.additionalTimezone");
    $[42] = t;
    $[43] = t20;
  } else {
    t20 = $[43];
  }
  let t21;
  if ($[44] !== form) {
    t21 = form.getInputProps("checkbox", {
      type: "checkbox"
    });
    $[44] = form;
    $[45] = t21;
  } else {
    t21 = $[45];
  }
  let t22;
  if ($[46] !== t20 || $[47] !== t21) {
    t22 = /* @__PURE__ */ jsxRuntimeExports.jsx(Checkbox, { styles: t19, fz: "14px", label: t20, mb: 12, ...t21 });
    $[46] = t20;
    $[47] = t21;
    $[48] = t22;
  } else {
    t22 = $[48];
  }
  let t23;
  if ($[49] !== form || $[50] !== t) {
    t23 = form.getInputProps("checkbox").value && /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.title, children: t("profile.dopTimeZone") }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(TimezoneSelect, { label: t("profile.additionalTimezoneDescription"), value: form.getInputProps("secondTimezone").value, ...form.getInputProps("secondTimezone") })
    ] });
    $[49] = form;
    $[50] = t;
    $[51] = t23;
  } else {
    t23 = $[51];
  }
  let t24;
  if ($[52] !== t22 || $[53] !== t23) {
    t24 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.section, children: [
      t22,
      t23
    ] });
    $[52] = t22;
    $[53] = t23;
    $[54] = t24;
  } else {
    t24 = $[54];
  }
  let t25;
  if ($[55] !== form) {
    t25 = form.isDirty();
    $[55] = form;
    $[56] = t25;
  } else {
    t25 = $[56];
  }
  const t26 = !t25;
  let t27;
  if ($[57] !== t) {
    t27 = t("save");
    $[57] = t;
    $[58] = t27;
  } else {
    t27 = $[58];
  }
  let t28;
  if ($[59] !== isLoading || $[60] !== t26 || $[61] !== t27) {
    t28 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { w: 200, loading: isLoading, h: 36, type: "submit", disabled: t26, children: t27 });
    $[59] = isLoading;
    $[60] = t26;
    $[61] = t27;
    $[62] = t28;
  } else {
    t28 = $[62];
  }
  let t29;
  if ($[63] !== t10 || $[64] !== t18 || $[65] !== t2 || $[66] !== t24 || $[67] !== t28) {
    t29 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.wrapper, children: [
      t2,
      t10,
      t18,
      t24,
      t28
    ] });
    $[63] = t10;
    $[64] = t18;
    $[65] = t2;
    $[66] = t24;
    $[67] = t28;
    $[68] = t29;
  } else {
    t29 = $[68];
  }
  let t30;
  if ($[69] !== t0 || $[70] !== t29) {
    t30 = /* @__PURE__ */ jsxRuntimeExports.jsx("form", { onSubmit: t0, children: t29 });
    $[69] = t0;
    $[70] = t29;
    $[71] = t30;
  } else {
    t30 = $[71];
  }
  return t30;
};
export {
  ProfileLocale
};
//# sourceMappingURL=index-BxZsfbZq.js.map
