import { r as reactExports, E as compilerRuntimeExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { aS as useUpdateFilialMutation, m as universalNotification, a6 as clearApiCacheByTags, ae as usersApi, c as useAppSelector, i as isSaaS } from "./index-DfebxkL9.js";
import { S as SelectWithPagination } from "./index-CfN7Ll0p.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { u as useStyles } from "./styles-rKP3r-bS.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { s as settingsPageValidateScheme } from "./validate-CA6bwF22.js";
import { z as useForm, E as TextInput, T as Text, a0 as SimpleGrid, B as Button, F as Flex, b as Box } from "./vendor-mantine-h0WlM2Mj.js";
import "./vendor-icons-xWhXK_Ui.js";
import "./errorIcon-PZO0f1IY.js";
import "./index.es-BTZFX8B-.js";
import "./styles-3NoIlzov.js";
import "./types-espwwxTQ.js";
const useSaveFilial = () => {
  const {
    t
  } = useTranslation();
  const [updateFilial] = useUpdateFilialMutation();
  return reactExports.useCallback(async (filial, values) => {
    try {
      const {
        Parent,
        ...restValues
      } = values;
      const res = await updateFilial({
        ...restValues,
        Title: values.Title.trim(),
        Id: filial == null ? void 0 : filial.Id,
        ...(Parent == null ? void 0 : Parent.Id) != null ? {
          Parent
        } : {}
      });
      if ("error" in res) {
        universalNotification({
          error: `${res == null ? void 0 : res.error}`,
          isError: true,
          isSuccess: false
        });
      } else {
        universalNotification({
          successTitle: t("filials.successSave"),
          isSuccess: true,
          isError: false,
          error: null
        });
        clearApiCacheByTags(usersApi, ["Users"]);
      }
    } catch (err) {
      console.warn(t("filials.errors.save"), err);
    }
  }, []);
};
const SettingsPage = () => {
  var _a, _b, _c, _d;
  const $ = compilerRuntimeExports.c(70);
  const {
    classes
  } = useStyles();
  const {
    t
  } = useTranslation();
  const {
    filial
  } = useAppSelector(_temp);
  const {
    Settings
  } = useAppSelector(_temp2);
  const [isSaving, setIsSaving] = reactExports.useState(false);
  const saveFilial = useSaveFilial();
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = {
      Title: "",
      Company: "",
      Parent: {
        Id: null,
        Name: ""
      },
      IsParentSettings: false
    };
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  let t1;
  if ($[1] !== t) {
    t1 = {
      initialValues: t0,
      validateInputOnChange: true,
      validate: (values) => getValidateErrors(settingsPageValidateScheme(t), values)
    };
    $[1] = t;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const form = useForm(t1);
  const errors = form.errors;
  let t2;
  if ($[3] !== form) {
    t2 = form.isDirty();
    $[3] = form;
    $[4] = t2;
  } else {
    t2 = $[4];
  }
  const isDirtyForm = t2;
  let t3;
  if ($[5] !== errors || $[6] !== isDirtyForm) {
    t3 = ((_a = Object.keys(errors)) == null ? void 0 : _a.length) !== 0 || !isDirtyForm;
    $[5] = errors;
    $[6] = isDirtyForm;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  const isDisabled = t3;
  const isMain = (filial == null ? void 0 : filial.Parent) === null && (filial == null ? void 0 : filial.IsRoot);
  let t4;
  if ($[8] !== Settings || $[9] !== filial || $[10] !== form || $[11] !== isMain) {
    t4 = () => {
      var _a2, _b2;
      if (!filial) {
        return;
      }
      const fromData = {
        IsParentSettings: false,
        ...filial,
        Title: filial.Title || "",
        Company: filial.Company || "",
        Settings: filial.Settings || void 0,
        Parent: isMain || isSaaS(Settings) ? null : {
          Id: ((_a2 = filial == null ? void 0 : filial.Parent) == null ? void 0 : _a2.Id) || null,
          Name: ((_b2 = filial == null ? void 0 : filial.Parent) == null ? void 0 : _b2.Name) || ""
        }
      };
      form.setValues(fromData);
      form.resetDirty(fromData);
    };
    $[8] = Settings;
    $[9] = filial;
    $[10] = form;
    $[11] = isMain;
    $[12] = t4;
  } else {
    t4 = $[12];
  }
  let t5;
  if ($[13] !== filial) {
    t5 = [filial];
    $[13] = filial;
    $[14] = t5;
  } else {
    t5 = $[14];
  }
  reactExports.useEffect(t4, t5);
  let t6;
  if ($[15] !== filial || $[16] !== saveFilial) {
    t6 = async (values_0) => {
      if (!values_0.Title || values_0.Company === void 0 || values_0.Parent === void 0 || values_0.IsParentSettings === void 0) {
        return;
      }
      await setIsSaving(true);
      await saveFilial(filial, values_0);
      await setIsSaving(false);
    };
    $[15] = filial;
    $[16] = saveFilial;
    $[17] = t6;
  } else {
    t6 = $[17];
  }
  const handleSubmit = t6;
  let t7;
  if ($[18] !== form) {
    t7 = form.getTransformedValues();
    $[18] = form;
    $[19] = t7;
  } else {
    t7 = $[19];
  }
  const transformedOptions = t7;
  const t8 = classes.container;
  let t9;
  if ($[20] !== form || $[21] !== handleSubmit) {
    t9 = form.onSubmit(handleSubmit);
    $[20] = form;
    $[21] = handleSubmit;
    $[22] = t9;
  } else {
    t9 = $[22];
  }
  const t10 = classes.section;
  const t11 = classes.title;
  let t12;
  if ($[23] !== t) {
    t12 = t("filials.settings.general");
    $[23] = t;
    $[24] = t12;
  } else {
    t12 = $[24];
  }
  let t13;
  if ($[25] !== classes.title || $[26] !== t12) {
    t13 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t11, children: t12 });
    $[25] = classes.title;
    $[26] = t12;
    $[27] = t13;
  } else {
    t13 = $[27];
  }
  let t14;
  if ($[28] === Symbol.for("react.memo_cache_sentinel")) {
    t14 = {
      base: 1,
      xs: 2
    };
    $[28] = t14;
  } else {
    t14 = $[28];
  }
  const t15 = classes.listRow;
  const t16 = classes.textInput;
  let t17;
  if ($[29] !== t) {
    t17 = t("filials.settings.name");
    $[29] = t;
    $[30] = t17;
  } else {
    t17 = $[30];
  }
  let t18;
  if ($[31] !== t) {
    t18 = t("filials.settings.filialName");
    $[31] = t;
    $[32] = t18;
  } else {
    t18 = $[32];
  }
  let t19;
  if ($[33] !== form) {
    t19 = form.getInputProps("Title");
    $[33] = form;
    $[34] = t19;
  } else {
    t19 = $[34];
  }
  let t20;
  if ($[35] !== classes.textInput || $[36] !== t17 || $[37] !== t18 || $[38] !== t19) {
    t20 = /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { className: t16, placeholder: t17, label: t18, withAsterisk: true, "data-value": "Title", ...t19 });
    $[35] = classes.textInput;
    $[36] = t17;
    $[37] = t18;
    $[38] = t19;
    $[39] = t20;
  } else {
    t20 = $[39];
  }
  let t21;
  if ($[40] !== Settings || $[41] !== classes.textInput || $[42] !== (filial == null ? void 0 : filial.Id) || $[43] !== form || $[44] !== isMain || $[45] !== t || $[46] !== ((_b = transformedOptions == null ? void 0 : transformedOptions.Parent) == null ? void 0 : _b.Id)) {
    t21 = !isSaaS(Settings) && /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { className: classes.textInput, placeholder: t("filials.settings.filialCode"), label: t("filials.settings.filialCode"), "data-value": "Company", ...form.getInputProps("Company") }),
      !isMain && /* @__PURE__ */ jsxRuntimeExports.jsx(SelectWithPagination, { form, fieldName: "Parent.Id", value: isMain ? null : (_c = transformedOptions == null ? void 0 : transformedOptions.Parent) == null ? void 0 : _c.Id, label: t("filials.settings.filialParent"), placeholder: t("choose"), SkipCustomers: (filial == null ? void 0 : filial.Id) ? [filial == null ? void 0 : filial.Id] : [], filialName: form.getInputProps("Parent.Name").value, customerField: "Parent.Name" })
    ] });
    $[40] = Settings;
    $[41] = classes.textInput;
    $[42] = filial == null ? void 0 : filial.Id;
    $[43] = form;
    $[44] = isMain;
    $[45] = t;
    $[46] = (_d = transformedOptions == null ? void 0 : transformedOptions.Parent) == null ? void 0 : _d.Id;
    $[47] = t21;
  } else {
    t21 = $[47];
  }
  let t22;
  if ($[48] !== classes.listRow || $[49] !== t20 || $[50] !== t21) {
    t22 = /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: t14, className: t15, children: [
      t20,
      t21
    ] });
    $[48] = classes.listRow;
    $[49] = t20;
    $[50] = t21;
    $[51] = t22;
  } else {
    t22 = $[51];
  }
  let t23;
  if ($[52] === Symbol.for("react.memo_cache_sentinel")) {
    t23 = {
      padding: "0 20px"
    };
    $[52] = t23;
  } else {
    t23 = $[52];
  }
  let t24;
  if ($[53] !== t) {
    t24 = t("save");
    $[53] = t;
    $[54] = t24;
  } else {
    t24 = $[54];
  }
  let t25;
  if ($[55] !== isDisabled || $[56] !== isSaving || $[57] !== t24) {
    t25 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", size: "sm", radius: 8, disabled: isDisabled, mt: "md", style: t23, loading: isSaving, children: t24 });
    $[55] = isDisabled;
    $[56] = isSaving;
    $[57] = t24;
    $[58] = t25;
  } else {
    t25 = $[58];
  }
  let t26;
  if ($[59] !== classes.section || $[60] !== t13 || $[61] !== t22 || $[62] !== t25) {
    t26 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { className: t10, align: "flex-start", direction: "column", children: [
      t13,
      t22,
      t25
    ] });
    $[59] = classes.section;
    $[60] = t13;
    $[61] = t22;
    $[62] = t25;
    $[63] = t26;
  } else {
    t26 = $[63];
  }
  let t27;
  if ($[64] !== t26 || $[65] !== t9) {
    t27 = /* @__PURE__ */ jsxRuntimeExports.jsx("form", { onSubmit: t9, children: t26 });
    $[64] = t26;
    $[65] = t9;
    $[66] = t27;
  } else {
    t27 = $[66];
  }
  let t28;
  if ($[67] !== classes.container || $[68] !== t27) {
    t28 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: t8, children: t27 });
    $[67] = classes.container;
    $[68] = t27;
    $[69] = t28;
  } else {
    t28 = $[69];
  }
  return t28;
};
function _temp(state) {
  return state.filials;
}
function _temp2(state_0) {
  return state_0.userInfo;
}
export {
  SettingsPage as default
};
//# sourceMappingURL=index-DRm13RDQ.js.map
