import { r as reactExports, E as compilerRuntimeExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { u as useStyles } from "./styles-CGXtrnyw.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { C as CommonSectionTemplate } from "./index-BRmgVx8q.js";
import { cR as useUpdateProductNotificationMutation, m as universalNotification, a6 as clearApiCacheByTags, cS as commonSettingsApi, cT as useGetProductNotificationQuery } from "./index-DfebxkL9.js";
import { a as validateSchemeProductNotification } from "./validate-DKrglko6.js";
import { D as DatePicker } from "./index-DFZKJmic.js";
import { T as TextFieldInput } from "./index-BvQfhnV_.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { W as useMediaQuery, z as useForm, a0 as SimpleGrid, R as Radio, F as Flex, B as Button, V as LoadingOverlay } from "./vendor-mantine-h0WlM2Mj.js";
import "./vendor-icons-xWhXK_Ui.js";
import "./types-espwwxTQ.js";
import "./ru-Bush41s2.js";
import "./styles-C9HVNQJ0.js";
const useSaveProductNotification = () => {
  const [updateProductNotification] = useUpdateProductNotificationMutation();
  return reactExports.useCallback(async (values) => {
    try {
      const res = await updateProductNotification(values);
      if ("error" in res) {
        universalNotification({
          error: res.error,
          isError: true,
          isSuccess: false
        });
      } else {
        universalNotification({
          error: null,
          isError: false,
          isSuccess: true,
          successTitle: `Настройки успешно сохранены!`
        });
        clearApiCacheByTags(commonSettingsApi, ["ProductNotification"]);
      }
    } catch (err) {
      console.warn("Ошибка сохранения настроек", err);
    }
  }, []);
};
const ProductNotificationsPage = () => {
  var _a, _b, _c;
  const $ = compilerRuntimeExports.c(71);
  const {
    classes
  } = useStyles();
  const {
    t
  } = useTranslation();
  const {
    data: productNotificationData,
    isFetching,
    isError,
    error
  } = useGetProductNotificationQuery();
  const isMobile = useMediaQuery("(max-width: 576px)");
  const updateProductNotification = useSaveProductNotification();
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = {
      initialValues: {
        UpdateId: "",
        Version: "",
        ReleaseDate: "",
        UpdateDate: "",
        ImageUrl: "",
        Topic: "",
        Text: "",
        UpdateUrl: "",
        IsEnabled: false
      },
      validateInputOnChange: true,
      validate: _temp
    };
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const form = useForm(t0);
  const errors = form.errors;
  let t1;
  if ($[1] !== form) {
    t1 = form.isDirty();
    $[1] = form;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const isDirtyForm = t1;
  let t2;
  if ($[3] !== errors || $[4] !== isDirtyForm) {
    t2 = Object.keys(errors || {}).length !== 0 || !isDirtyForm;
    $[3] = errors;
    $[4] = isDirtyForm;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  const isDisabled = t2;
  let t3;
  if ($[6] !== form || $[7] !== productNotificationData) {
    t3 = () => {
      if (productNotificationData) {
        const formData = {
          UpdateId: (productNotificationData == null ? void 0 : productNotificationData.UpdateId) || "",
          Version: (productNotificationData == null ? void 0 : productNotificationData.Version) || "",
          ReleaseDate: (productNotificationData == null ? void 0 : productNotificationData.ReleaseDate) || "",
          UpdateDate: (productNotificationData == null ? void 0 : productNotificationData.UpdateDate) || "",
          ImageUrl: (productNotificationData == null ? void 0 : productNotificationData.ImageUrl) || "",
          Topic: (productNotificationData == null ? void 0 : productNotificationData.Topic) || "",
          Text: (productNotificationData == null ? void 0 : productNotificationData.Text) || "",
          UpdateUrl: (productNotificationData == null ? void 0 : productNotificationData.UpdateUrl) || "",
          IsEnabled: (productNotificationData == null ? void 0 : productNotificationData.IsEnabled) || false
        };
        form.setValues(formData);
        form.resetDirty(formData);
      }
    };
    $[6] = form;
    $[7] = productNotificationData;
    $[8] = t3;
  } else {
    t3 = $[8];
  }
  let t4;
  if ($[9] !== productNotificationData) {
    t4 = [productNotificationData];
    $[9] = productNotificationData;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  reactExports.useEffect(t3, t4);
  let t5;
  let t6;
  if ($[11] !== error || $[12] !== isError || $[13] !== isFetching) {
    t5 = () => {
      if (isError && error && !isFetching) {
        universalNotification({
          error,
          isError,
          isSuccess: false
        });
      }
    };
    t6 = [error, isError, isFetching];
    $[11] = error;
    $[12] = isError;
    $[13] = isFetching;
    $[14] = t5;
    $[15] = t6;
  } else {
    t5 = $[14];
    t6 = $[15];
  }
  reactExports.useEffect(t5, t6);
  let t7;
  if ($[16] !== form || $[17] !== updateProductNotification) {
    t7 = async (values_0) => {
      form.validate();
      if (!form.isValid()) {
        return;
      }
      await updateProductNotification({
        UpdateInfo: values_0
      });
    };
    $[16] = form;
    $[17] = updateProductNotification;
    $[18] = t7;
  } else {
    t7 = $[18];
  }
  const handleSubmit = t7;
  let t8;
  if ($[19] !== form) {
    t8 = () => {
      form.setValues({
        IsEnabled: false
      });
    };
    $[19] = form;
    $[20] = t8;
  } else {
    t8 = $[20];
  }
  const handleChange = t8;
  let t9;
  if ($[21] !== t) {
    t9 = t("commonSettings.systemNotification");
    $[21] = t;
    $[22] = t9;
  } else {
    t9 = $[22];
  }
  let t10;
  if ($[23] !== form || $[24] !== handleSubmit) {
    t10 = form.onSubmit(handleSubmit);
    $[23] = form;
    $[24] = handleSubmit;
    $[25] = t10;
  } else {
    t10 = $[25];
  }
  const t11 = classes.radio;
  const t12 = !((_a = form == null ? void 0 : form.values) == null ? void 0 : _a.IsEnabled);
  let t13;
  if ($[26] !== handleChange) {
    t13 = () => handleChange();
    $[26] = handleChange;
    $[27] = t13;
  } else {
    t13 = $[27];
  }
  let t14;
  if ($[28] !== t) {
    t14 = t("disable");
    $[28] = t;
    $[29] = t14;
  } else {
    t14 = $[29];
  }
  let t15;
  if ($[30] !== classes.radio || $[31] !== t12 || $[32] !== t13 || $[33] !== t14) {
    t15 = /* @__PURE__ */ jsxRuntimeExports.jsx(Radio, { className: t11, size: "xs", mr: "md", checked: t12, onChange: t13, label: t14 });
    $[30] = classes.radio;
    $[31] = t12;
    $[32] = t13;
    $[33] = t14;
    $[34] = t15;
  } else {
    t15 = $[34];
  }
  const t16 = classes.radio;
  const t17 = (_b = form == null ? void 0 : form.values) == null ? void 0 : _b.IsEnabled;
  let t18;
  if ($[35] !== form) {
    t18 = () => form.setValues({
      IsEnabled: true
    });
    $[35] = form;
    $[36] = t18;
  } else {
    t18 = $[36];
  }
  let t19;
  if ($[37] !== t) {
    t19 = t("enable");
    $[37] = t;
    $[38] = t19;
  } else {
    t19 = $[38];
  }
  let t20;
  if ($[39] !== classes.radio || $[40] !== t17 || $[41] !== t18 || $[42] !== t19) {
    t20 = /* @__PURE__ */ jsxRuntimeExports.jsx(Radio, { className: t16, size: "xs", mb: "sm", checked: t17, onChange: t18, label: t19 });
    $[39] = classes.radio;
    $[40] = t17;
    $[41] = t18;
    $[42] = t19;
    $[43] = t20;
  } else {
    t20 = $[43];
  }
  let t21;
  if ($[44] !== t15 || $[45] !== t20) {
    t21 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { children: [
      t15,
      t20
    ] });
    $[44] = t15;
    $[45] = t20;
    $[46] = t21;
  } else {
    t21 = $[46];
  }
  let t22;
  if ($[47] !== form || $[48] !== isMobile || $[49] !== t) {
    t22 = ((_c = form == null ? void 0 : form.values) == null ? void 0 : _c.IsEnabled) ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: isMobile ? 1 : 2, w: "100%", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "Topic", label: t("commonSettings.title"), form, withAsterisk: true }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "Text", label: t("commonSettings.textMessage"), form, withAsterisk: true })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: isMobile ? 1 : 2, w: "100%", mt: 16, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "Version", label: t("commonSettings.version"), form, withAsterisk: true }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "UpdateId", label: t("commonSettings.updateId"), form, withAsterisk: true })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: isMobile ? 1 : 2, w: "100%", mt: 16, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "UpdateUrl", label: t("commonSettings.updateUrl"), form, withAsterisk: true }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { fieldName: "ImageUrl", label: t("commonSettings.imageUrl"), form, withAsterisk: true })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: isMobile ? 1 : 2, mt: 16, w: "100%", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(DatePicker, { fieldName: "ReleaseDate", label: t("commonSettings.releaseDate"), form, withAsterisk: true }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(DatePicker, { fieldName: "UpdateDate", label: t("commonSettings.updateDate"), form, withAsterisk: true })
      ] })
    ] }) : null;
    $[47] = form;
    $[48] = isMobile;
    $[49] = t;
    $[50] = t22;
  } else {
    t22 = $[50];
  }
  const t23 = classes.sectionControls;
  const t24 = classes.submitButton;
  let t25;
  if ($[51] !== t) {
    t25 = t("save");
    $[51] = t;
    $[52] = t25;
  } else {
    t25 = $[52];
  }
  let t26;
  if ($[53] !== classes.submitButton || $[54] !== isDisabled || $[55] !== t25) {
    t26 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { disabled: isDisabled, className: t24, size: "sm", radius: 8, type: "submit", mt: "md", children: t25 });
    $[53] = classes.submitButton;
    $[54] = isDisabled;
    $[55] = t25;
    $[56] = t26;
  } else {
    t26 = $[56];
  }
  let t27;
  if ($[57] !== classes.sectionControls || $[58] !== t26) {
    t27 = /* @__PURE__ */ jsxRuntimeExports.jsx(Flex, { className: t23, w: "100%", children: t26 });
    $[57] = classes.sectionControls;
    $[58] = t26;
    $[59] = t27;
  } else {
    t27 = $[59];
  }
  let t28;
  if ($[60] !== t10 || $[61] !== t21 || $[62] !== t22 || $[63] !== t27) {
    t28 = /* @__PURE__ */ jsxRuntimeExports.jsx(SimpleGrid, { cols: 1, w: "100%", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { onSubmit: t10, children: [
      t21,
      t22,
      t27
    ] }) });
    $[60] = t10;
    $[61] = t21;
    $[62] = t22;
    $[63] = t27;
    $[64] = t28;
  } else {
    t28 = $[64];
  }
  let t29;
  if ($[65] !== isFetching) {
    t29 = /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isFetching });
    $[65] = isFetching;
    $[66] = t29;
  } else {
    t29 = $[66];
  }
  let t30;
  if ($[67] !== t28 || $[68] !== t29 || $[69] !== t9) {
    t30 = /* @__PURE__ */ jsxRuntimeExports.jsxs(CommonSectionTemplate, { title: t9, children: [
      t28,
      t29
    ] });
    $[67] = t28;
    $[68] = t29;
    $[69] = t9;
    $[70] = t30;
  } else {
    t30 = $[70];
  }
  return t30;
};
function _temp(values) {
  return getValidateErrors(validateSchemeProductNotification, values);
}
export {
  ProductNotificationsPage as default
};
//# sourceMappingURL=index-CXiJUPid.js.map
