import { r as reactExports, E as compilerRuntimeExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { c as useAppSelector, b7 as useUpdateFilialTimezoneMutation, ax as defaultLang, m as universalNotification } from "./index-DfebxkL9.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { z as useForm, T as Text, B as Button } from "./vendor-mantine-h0WlM2Mj.js";
import { L as LanguageSelect, T as TimezoneSelect } from "./index-lG79Dd5P.js";
import "./vendor-icons-xWhXK_Ui.js";
const wrapper = "_wrapper_1rqui_1";
const title = "_title_1rqui_10";
const mainTitle = "_mainTitle_1rqui_15";
const subTitle = "_subTitle_1rqui_20";
const classes = {
  wrapper,
  title,
  mainTitle,
  subTitle
};
const useLocaleTimeZone = () => {
  const {
    t
  } = useTranslation();
  const [isLoading, setIsLoading] = reactExports.useState(false);
  const {
    filial
  } = useAppSelector((state) => state.filials);
  const {
    Timezones
  } = useAppSelector((state_0) => state_0.commonSettings.TimezoneSettings);
  const {
    TimeZone,
    Culture: locale
  } = (filial == null ? void 0 : filial.Settings) ?? {};
  const [updateSettings] = useUpdateFilialTimezoneMutation();
  const form = useForm({
    validateInputOnChange: true,
    initialValues: {
      locale: locale || defaultLang,
      timezone: String((TimeZone == null ? void 0 : TimeZone.Id) ?? 0)
    }
  });
  reactExports.useEffect(() => {
    if (TimeZone) {
      form.setValues({
        locale: locale || defaultLang,
        timezone: String((TimeZone == null ? void 0 : TimeZone.Id) ?? 0)
      });
      form.resetDirty();
    }
  }, [TimeZone, locale]);
  const getTimezoneById = (timezoneId) => {
    const timezoneItem = Timezones[timezoneId];
    if (!timezoneItem) {
      return null;
    }
    return {
      Id: Number(timezoneId),
      TzIdName: timezoneItem.tzName,
      UtcOffset: timezoneItem.utc,
      UtcOffsetMinutes: timezoneItem.utcOffsetMinutes,
      Name: timezoneItem.label
    };
  };
  const handleSave = async () => {
    if (!filial || !filial.Settings || isLoading) {
      return;
    }
    const {
      locale: locale_0,
      timezone
    } = form.getValues();
    const timezoneData = getTimezoneById(timezone);
    if (!timezoneData) {
      universalNotification({
        isSuccess: false,
        isError: true,
        error: t("common.savedMessageError"),
        successTitle: ""
      });
      return;
    }
    setIsLoading(true);
    try {
      await updateSettings({
        CustomerId: filial == null ? void 0 : filial.Id,
        Type: 9,
        Settings: {
          Culture: locale_0,
          TimeZone: timezoneData
        }
      });
      universalNotification({
        isSuccess: true,
        isError: false,
        error: "",
        successTitle: t("filials.successSave")
      });
      setIsLoading(false);
    } catch (e) {
      setIsLoading(false);
      universalNotification({
        isSuccess: false,
        isError: true,
        error: t("common.savedMessageError"),
        successTitle: ""
      });
    }
  };
  return {
    form,
    isLoading,
    handleSave
  };
};
const LocaleTimezonePage = () => {
  const $ = compilerRuntimeExports.c(48);
  const {
    t
  } = useTranslation();
  const {
    form,
    isLoading,
    handleSave
  } = useLocaleTimeZone();
  let t0;
  if ($[0] !== t) {
    t0 = t("filials.settings.localePageTitle");
    $[0] = t;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  let t1;
  if ($[2] !== t0) {
    t1 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.mainTitle, children: t0 });
    $[2] = t0;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[4] !== t) {
    t2 = t("filials.settings.localeLanguageLabel");
    $[4] = t;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  let t3;
  if ($[6] !== t2) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.subTitle, children: t2 });
    $[6] = t2;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  let t4;
  if ($[8] !== t) {
    t4 = t("filials.settings.localeLanguageTitle");
    $[8] = t;
    $[9] = t4;
  } else {
    t4 = $[9];
  }
  let t5;
  if ($[10] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.title, children: t4 });
    $[10] = t4;
    $[11] = t5;
  } else {
    t5 = $[11];
  }
  let t6;
  if ($[12] !== form) {
    t6 = form.getInputProps("locale");
    $[12] = form;
    $[13] = t6;
  } else {
    t6 = $[13];
  }
  const t7 = t6.value;
  let t8;
  if ($[14] !== form) {
    t8 = form.getInputProps("locale");
    $[14] = form;
    $[15] = t8;
  } else {
    t8 = $[15];
  }
  let t9;
  if ($[16] !== t6.value || $[17] !== t8) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsx(LanguageSelect, { label: "", value: t7, ...t8 });
    $[16] = t6.value;
    $[17] = t8;
    $[18] = t9;
  } else {
    t9 = $[18];
  }
  let t10;
  if ($[19] !== t5 || $[20] !== t9) {
    t10 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
      t5,
      t9
    ] });
    $[19] = t5;
    $[20] = t9;
    $[21] = t10;
  } else {
    t10 = $[21];
  }
  let t11;
  if ($[22] !== t) {
    t11 = t("filials.settings.localeTimezoneTitle");
    $[22] = t;
    $[23] = t11;
  } else {
    t11 = $[23];
  }
  let t12;
  if ($[24] !== t11) {
    t12 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: classes.title, children: t11 });
    $[24] = t11;
    $[25] = t12;
  } else {
    t12 = $[25];
  }
  let t13;
  if ($[26] !== form) {
    t13 = form.getInputProps("timezone");
    $[26] = form;
    $[27] = t13;
  } else {
    t13 = $[27];
  }
  const t14 = t13.value;
  let t15;
  if ($[28] !== form) {
    t15 = form.getInputProps("timezone");
    $[28] = form;
    $[29] = t15;
  } else {
    t15 = $[29];
  }
  let t16;
  if ($[30] !== t13.value || $[31] !== t15) {
    t16 = /* @__PURE__ */ jsxRuntimeExports.jsx(TimezoneSelect, { label: "", value: t14, ...t15 });
    $[30] = t13.value;
    $[31] = t15;
    $[32] = t16;
  } else {
    t16 = $[32];
  }
  let t17;
  if ($[33] !== t12 || $[34] !== t16) {
    t17 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
      t12,
      t16
    ] });
    $[33] = t12;
    $[34] = t16;
    $[35] = t17;
  } else {
    t17 = $[35];
  }
  let t18;
  if ($[36] !== t) {
    t18 = t("save");
    $[36] = t;
    $[37] = t18;
  } else {
    t18 = $[37];
  }
  let t19;
  if ($[38] !== handleSave || $[39] !== isLoading || $[40] !== t18) {
    t19 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { loading: isLoading, onClick: handleSave, children: t18 });
    $[38] = handleSave;
    $[39] = isLoading;
    $[40] = t18;
    $[41] = t19;
  } else {
    t19 = $[41];
  }
  let t20;
  if ($[42] !== t1 || $[43] !== t10 || $[44] !== t17 || $[45] !== t19 || $[46] !== t3) {
    t20 = /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.wrapper, children: [
      t1,
      t3,
      t10,
      t17,
      t19
    ] });
    $[42] = t1;
    $[43] = t10;
    $[44] = t17;
    $[45] = t19;
    $[46] = t3;
    $[47] = t20;
  } else {
    t20 = $[47];
  }
  return t20;
};
export {
  LocaleTimezonePage as default
};
//# sourceMappingURL=index-CArnrf6K.js.map
