import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { u as useStyles } from "./styles-rKP3r-bS.js";
import { S as SelectUserModal } from "./index-DFEP7Sv_.js";
import { b as gq, C as ES } from "./vendor-icons-xWhXK_Ui.js";
import { A as AddButton } from "./index-72JBTuLT.js";
import { b6 as useGetUserEmailsQuery, c as useAppSelector, aT as SettingsEnum, O as themeColors } from "./index-DfebxkL9.js";
import { u as useSaveSettings } from "./useSaveSettings-GkZlPEM1.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { T as Text, a4 as Select, F as Flex, W as useMediaQuery, z as useForm, a0 as SimpleGrid, E as TextInput, h as ActionIcon, b as Box, B as Button } from "./vendor-mantine-h0WlM2Mj.js";
import { o as objectType, n as numberType, s as stringType, l as literalType, u as unionType } from "./types-espwwxTQ.js";
import "./styles-3NoIlzov.js";
import "./index-DqcA8P_y.js";
import "./index-CcqTjOWY.js";
import "./index.es-BTZFX8B-.js";
import "./useDebounce-DTubzIUC.js";
import "./index-C-CFogWu.js";
import "./index-3c9En-DB.js";
import "./index-C_Fw_IUU.js";
const EmailsSelect = (t0) => {
  const $ = compilerRuntimeExports.c(37);
  const {
    form,
    handleChange,
    fieldName,
    value,
    userId
  } = t0;
  const {
    classes
  } = useStyles();
  const {
    t
  } = useTranslation();
  let t1;
  if ($[0] !== userId) {
    t1 = {
      Id: userId
    };
    $[0] = userId;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const {
    data,
    isSuccess
  } = useGetUserEmailsQuery(t1);
  let t2;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = [];
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  const [emails, setEmails] = reactExports.useState(t2);
  let t3;
  let t4;
  if ($[3] !== data) {
    t3 = () => {
      if (data) {
        setEmails(data);
      }
    };
    t4 = [data];
    $[3] = data;
    $[4] = t3;
    $[5] = t4;
  } else {
    t3 = $[4];
    t4 = $[5];
  }
  reactExports.useEffect(t3, t4);
  let t5;
  if ($[6] !== emails) {
    t5 = Array.isArray(emails) ? emails == null ? void 0 : emails.map(_temp$1) : [];
    $[6] = emails;
    $[7] = t5;
  } else {
    t5 = $[7];
  }
  const list = t5;
  const error = form ? form.errors[`${fieldName}.Id`] : "";
  let t6;
  if ($[8] !== emails || $[9] !== fieldName || $[10] !== form || $[11] !== handleChange) {
    t6 = (query) => {
      if (form) {
        const selected = emails.find((email) => email.Id === Number(query));
        form == null ? void 0 : form.setFieldValue(`${fieldName}`, {
          Name: selected == null ? void 0 : selected.Email,
          Id: selected == null ? void 0 : selected.Id
        });
      } else {
        handleChange && handleChange(query);
      }
    };
    $[8] = emails;
    $[9] = fieldName;
    $[10] = form;
    $[11] = handleChange;
    $[12] = t6;
  } else {
    t6 = $[12];
  }
  const handleChangeEmail = t6;
  let t7;
  if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
    t7 = {
      width: "100%"
    };
    $[13] = t7;
  } else {
    t7 = $[13];
  }
  const t8 = classes.textFieldInput;
  let t9;
  if ($[14] !== t) {
    t9 = t("filials.mailSetup.emailPlaceholder");
    $[14] = t;
    $[15] = t9;
  } else {
    t9 = $[15];
  }
  let t10;
  if ($[16] !== t) {
    t10 = t("filials.mailSetup.emailLabel");
    $[16] = t;
    $[17] = t10;
  } else {
    t10 = $[17];
  }
  let t11;
  if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
    t11 = /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3" });
    $[18] = t11;
  } else {
    t11 = $[18];
  }
  let t12;
  if ($[19] !== value) {
    t12 = (value == null ? void 0 : value.toString()) || null;
    $[19] = value;
    $[20] = t12;
  } else {
    t12 = $[20];
  }
  let t13;
  if ($[21] === Symbol.for("react.memo_cache_sentinel")) {
    t13 = {
      width: "100%"
    };
    $[21] = t13;
  } else {
    t13 = $[21];
  }
  let t14;
  if ($[22] !== classes.textFieldInput || $[23] !== error || $[24] !== handleChangeEmail || $[25] !== list || $[26] !== t10 || $[27] !== t12 || $[28] !== t9) {
    t14 = /* @__PURE__ */ jsxRuntimeExports.jsx(Select, { className: t8, required: true, data: list, placeholder: t9, label: t10, rightSectionWidth: 30, rightSection: t11, value: t12, sx: t13, onChange: handleChangeEmail, error });
    $[22] = classes.textFieldInput;
    $[23] = error;
    $[24] = handleChangeEmail;
    $[25] = list;
    $[26] = t10;
    $[27] = t12;
    $[28] = t9;
    $[29] = t14;
  } else {
    t14 = $[29];
  }
  let t15;
  if ($[30] !== (emails == null ? void 0 : emails.length) || $[31] !== isSuccess || $[32] !== t) {
    t15 = !(emails == null ? void 0 : emails.length) && isSuccess ? /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { fz: "xs", color: "red", children: t("filials.mailSetup.noEmail") }) : null;
    $[30] = emails == null ? void 0 : emails.length;
    $[31] = isSuccess;
    $[32] = t;
    $[33] = t15;
  } else {
    t15 = $[33];
  }
  let t16;
  if ($[34] !== t14 || $[35] !== t15) {
    t16 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { direction: "column", sx: t7, children: [
      t14,
      t15
    ] });
    $[34] = t14;
    $[35] = t15;
    $[36] = t16;
  } else {
    t16 = $[36];
  }
  return t16;
};
function _temp$1(Item) {
  return {
    label: String(Item.Email),
    value: Item.Id.toString()
  };
}
const noUserScheme = objectType({
  Smtp: objectType({
    User: literalType(null),
    Email: objectType({
      Name: stringType().optional(),
      Id: numberType().optional(),
      Count: numberType().optional()
    }).nullable()
  })
});
const withUserScheme = objectType({
  Smtp: objectType({
    User: objectType({
      Name: stringType(),
      Id: numberType(),
      Count: numberType().optional()
    }),
    Email: objectType({
      Name: stringType().optional(),
      Id: numberType(),
      Count: numberType().optional()
    })
  })
});
const validateScheme = unionType([noUserScheme, withUserScheme]);
const MailingSetupPage = () => {
  var _a, _b, _c, _d;
  const $ = compilerRuntimeExports.c(77);
  const {
    t
  } = useTranslation();
  const {
    classes
  } = useStyles();
  const [isUserModalOpen, setIsUserModalOpen] = reactExports.useState(false);
  const [selectedUser, setSelectedUser] = reactExports.useState(null);
  const {
    filial
  } = useAppSelector(_temp);
  const isSmallScreen = useMediaQuery("(max-width: 576px)");
  const saveSettings = useSaveSettings();
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = {
      initialValues: {
        Smtp: {
          User: null,
          Email: null
        }
      },
      validateInputOnChange: true,
      validate: _temp2
    };
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const form = useForm(t0);
  let t1;
  if ($[1] !== (filial == null ? void 0 : filial.Settings) || $[2] !== form) {
    t1 = () => {
      const {
        Smtp
      } = (filial == null ? void 0 : filial.Settings) || {};
      if (!Smtp) {
        return;
      }
      const formData = {
        Smtp: {
          Email: Smtp.Email ? Smtp.Email : null,
          User: (Smtp == null ? void 0 : Smtp.User) || null
        }
      };
      form.setValues(formData);
      form.resetDirty(formData);
    };
    $[1] = filial == null ? void 0 : filial.Settings;
    $[2] = form;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[4] !== filial) {
    t2 = [filial];
    $[4] = filial;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  reactExports.useEffect(t1, t2);
  let t3;
  if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = () => {
      setIsUserModalOpen(false);
    };
    $[6] = t3;
  } else {
    t3 = $[6];
  }
  const closeModals = t3;
  let t4;
  if ($[7] !== form) {
    t4 = () => {
      form.setFieldValue("Smtp.User", null);
      form.setFieldValue("Smtp.Email", null);
      setSelectedUser(null);
    };
    $[7] = form;
    $[8] = t4;
  } else {
    t4 = $[8];
  }
  const handeDeleteItem = t4;
  let t5;
  if ($[9] !== filial || $[10] !== form || $[11] !== saveSettings) {
    t5 = (formValues) => {
      var _a2, _b2;
      if (!filial) {
        return;
      }
      saveSettings(filial.Id, SettingsEnum.Smtp, {
        Smtp: !((_b2 = (_a2 = formValues.Smtp) == null ? void 0 : _a2.Email) == null ? void 0 : _b2.Id) ? null : formValues.Smtp
      });
      form.resetDirty();
    };
    $[9] = filial;
    $[10] = form;
    $[11] = saveSettings;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  const handleSubmit = t5;
  let t6;
  if ($[13] !== form || $[14] !== selectedUser) {
    t6 = async () => {
      if (!selectedUser) {
        return;
      }
      form.setFieldValue("Smtp.User", {
        Id: selectedUser == null ? void 0 : selectedUser.Id,
        Name: selectedUser.Name,
        Count: (selectedUser == null ? void 0 : selectedUser.Count) || 0
      });
      form.setFieldValue("Smtp.Email", null);
      closeModals();
    };
    $[13] = form;
    $[14] = selectedUser;
    $[15] = t6;
  } else {
    t6 = $[15];
  }
  const handleConfirmSelectUserModal = t6;
  let t7;
  if ($[16] !== form) {
    t7 = !form.isValid() || !form.isDirty();
    $[16] = form;
    $[17] = t7;
  } else {
    t7 = $[17];
  }
  const isDisabled = t7;
  const t8 = classes.container;
  const t9 = classes.section;
  let t10;
  if ($[18] !== form || $[19] !== handleSubmit) {
    t10 = form.onSubmit(handleSubmit);
    $[18] = form;
    $[19] = handleSubmit;
    $[20] = t10;
  } else {
    t10 = $[20];
  }
  const t11 = classes.title;
  let t12;
  if ($[21] !== t) {
    t12 = t("filials.settings.linkSettings");
    $[21] = t;
    $[22] = t12;
  } else {
    t12 = $[22];
  }
  let t13;
  if ($[23] !== classes.title || $[24] !== t12) {
    t13 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t11, children: t12 });
    $[23] = classes.title;
    $[24] = t12;
    $[25] = t13;
  } else {
    t13 = $[25];
  }
  let t14;
  if ($[26] !== classes.listRow || $[27] !== classes.phoneIcon || $[28] !== classes.phoneList || $[29] !== classes.removeButton || $[30] !== classes.textFieldInput || $[31] !== form || $[32] !== handeDeleteItem || $[33] !== isSmallScreen || $[34] !== t) {
    t14 = form.getInputProps("Smtp.Email").value || form.getInputProps("Smtp.User").value ? /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: {
      base: 1,
      xs: 2
    }, className: isSmallScreen ? classes.phoneList : classes.listRow, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { className: classes.textFieldInput, size: "sm", w: "100%", mr: 32, label: t("filials.mailSetup.userLabel"), withAsterisk: true, readOnly: true, onClick: () => setIsUserModalOpen(true), ...form.getInputProps("Smtp.User.Name") }),
      isSmallScreen && /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { size: 24, mt: 0, ml: 24, className: classes.phoneIcon, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ES, { color: "#667085", onClick: () => handeDeleteItem() }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { display: "flex", children: [
        ((_b = (_a = form.getTransformedValues().Smtp) == null ? void 0 : _a.User) == null ? void 0 : _b.Id) !== null && /* @__PURE__ */ jsxRuntimeExports.jsx(EmailsSelect, { form, fieldName: "Smtp.Email", value: ((_d = (_c = form.getTransformedValues().Smtp) == null ? void 0 : _c.Email) == null ? void 0 : _d.Id) || null, userId: form.getInputProps("Smtp.User.Id").value }),
        !isSmallScreen && /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { size: 24, mt: 32, ml: 24, className: classes.removeButton, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ES, { color: "#667085", onClick: () => handeDeleteItem() }) })
      ] })
    ] }) : /* @__PURE__ */ jsxRuntimeExports.jsx(AddButton, { onClick: () => setIsUserModalOpen(true), w: "max-content", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { fw: "600", fz: "14", children: t("filials.modules.addUser") }) });
    $[26] = classes.listRow;
    $[27] = classes.phoneIcon;
    $[28] = classes.phoneList;
    $[29] = classes.removeButton;
    $[30] = classes.textFieldInput;
    $[31] = form;
    $[32] = handeDeleteItem;
    $[33] = isSmallScreen;
    $[34] = t;
    $[35] = t14;
  } else {
    t14 = $[35];
  }
  let t15;
  if ($[36] !== t) {
    t15 = t("filials.settings.linkDescription");
    $[36] = t;
    $[37] = t15;
  } else {
    t15 = $[37];
  }
  let t16;
  if ($[38] !== t15) {
    t16 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { color: themeColors.brandGray[0], fz: "sm", mt: 24, children: t15 });
    $[38] = t15;
    $[39] = t16;
  } else {
    t16 = $[39];
  }
  let t17;
  if ($[40] !== t) {
    t17 = t("save");
    $[40] = t;
    $[41] = t17;
  } else {
    t17 = $[41];
  }
  let t18;
  if ($[42] !== isDisabled || $[43] !== t17) {
    t18 = /* @__PURE__ */ jsxRuntimeExports.jsx(Flex, { mt: "md", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", px: "32px", radius: "md", disabled: isDisabled, children: t17 }) });
    $[42] = isDisabled;
    $[43] = t17;
    $[44] = t18;
  } else {
    t18 = $[44];
  }
  let t19;
  if ($[45] !== t10 || $[46] !== t13 || $[47] !== t14 || $[48] !== t16 || $[49] !== t18) {
    t19 = /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: t10, children: [
      t13,
      t14,
      t16,
      t18
    ] });
    $[45] = t10;
    $[46] = t13;
    $[47] = t14;
    $[48] = t16;
    $[49] = t18;
    $[50] = t19;
  } else {
    t19 = $[50];
  }
  let t20;
  if ($[51] !== classes.section || $[52] !== t19) {
    t20 = /* @__PURE__ */ jsxRuntimeExports.jsx(Flex, { className: t9, align: "flex-start", direction: "column", children: t19 });
    $[51] = classes.section;
    $[52] = t19;
    $[53] = t20;
  } else {
    t20 = $[53];
  }
  let t21;
  if ($[54] !== t) {
    t21 = t("filials.modules.addUser");
    $[54] = t;
    $[55] = t21;
  } else {
    t21 = $[55];
  }
  const t22 = selectedUser == null ? void 0 : selectedUser.Id;
  let t23;
  if ($[56] !== t) {
    t23 = t("add");
    $[56] = t;
    $[57] = t23;
  } else {
    t23 = $[57];
  }
  let t24;
  if ($[58] !== handleConfirmSelectUserModal || $[59] !== t23) {
    t24 = {
      btnName: t23,
      fn: handleConfirmSelectUserModal
    };
    $[58] = handleConfirmSelectUserModal;
    $[59] = t23;
    $[60] = t24;
  } else {
    t24 = $[60];
  }
  let t25;
  if ($[61] !== t) {
    t25 = t("cancel");
    $[61] = t;
    $[62] = t25;
  } else {
    t25 = $[62];
  }
  let t26;
  if ($[63] !== t25) {
    t26 = {
      btnName: t25,
      fn: closeModals
    };
    $[63] = t25;
    $[64] = t26;
  } else {
    t26 = $[64];
  }
  let t27;
  if ($[65] !== t24 || $[66] !== t26) {
    t27 = {
      confirmProps: t24,
      cancelProps: t26
    };
    $[65] = t24;
    $[66] = t26;
    $[67] = t27;
  } else {
    t27 = $[67];
  }
  let t28;
  if ($[68] !== isUserModalOpen || $[69] !== t21 || $[70] !== t22 || $[71] !== t27) {
    t28 = /* @__PURE__ */ jsxRuntimeExports.jsx(SelectUserModal, { multipleUsers: false, isOpened: isUserModalOpen, title: t21, onClose: closeModals, setSelectedUsers: setSelectedUser, userId: t22, controlSection: true, controlSectionProps: t27 });
    $[68] = isUserModalOpen;
    $[69] = t21;
    $[70] = t22;
    $[71] = t27;
    $[72] = t28;
  } else {
    t28 = $[72];
  }
  let t29;
  if ($[73] !== classes.container || $[74] !== t20 || $[75] !== t28) {
    t29 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: t8, children: [
      t20,
      t28
    ] });
    $[73] = classes.container;
    $[74] = t20;
    $[75] = t28;
    $[76] = t29;
  } else {
    t29 = $[76];
  }
  return t29;
};
function _temp(state) {
  return state.filials;
}
function _temp2(values) {
  return getValidateErrors(validateScheme, values);
}
export {
  MailingSetupPage as default
};
//# sourceMappingURL=index-CrwcKuxe.js.map
