import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { S as SelectWithPagination } from "./index-CfN7Ll0p.js";
import { S as SectionTemplate } from "./index-CbTPqI7c.js";
import { E as EditUserInfoContext } from "./index-TL41xEJi.js";
import { c as useAppSelector, bT as isAdmin } from "./index-DfebxkL9.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { b as validateSchemeCustomer, a as validateSchemePositions } from "./validate-Bw-sUlFj.js";
import { u as useUpdateInfo, a as ProfileUpdateTypeEnum, P as ProfileSectionEnum } from "./useUpdateUser-innD962A.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { z as useForm, a4 as Select, a0 as SimpleGrid, B as Button, V as LoadingOverlay, b as Box } from "./vendor-mantine-h0WlM2Mj.js";
import { P as Positions } from "./index-CUcivY9l.js";
import { R as Roles } from "./index-DOixMRp5.js";
import "./errorIcon-PZO0f1IY.js";
import "./index.es-BTZFX8B-.js";
import "./vendor-icons-xWhXK_Ui.js";
import "./index-B7nmdYVM.js";
import "./utils-D5kCyTRM.js";
import "./types-B9VC9FeZ.js";
import "./useGeneratePolicy-B4rO5xUa.js";
import "./passwordUtils-B3CN2t6O.js";
import "./types-espwwxTQ.js";
import "./index-BvQfhnV_.js";
import "./styles-C9HVNQJ0.js";
import "./index-72JBTuLT.js";
import "./index-DFEP7Sv_.js";
import "./index-DqcA8P_y.js";
import "./styles-3NoIlzov.js";
import "./index-CcqTjOWY.js";
import "./useDebounce-DTubzIUC.js";
import "./index-C-CFogWu.js";
import "./index-3c9En-DB.js";
import "./index-C_Fw_IUU.js";
import "./index-Ds4Bnwmy.js";
const UserFilial = () => {
  var _a, _b, _c, _d, _e;
  const $ = compilerRuntimeExports.c(48);
  const {
    t
  } = useTranslation();
  const {
    userInfoCommon,
    isLimitedAccess,
    currentId
  } = reactExports.useContext(EditUserInfoContext);
  const currentUser = useAppSelector(_temp$1);
  const {
    Customer
  } = userInfoCommon;
  const {
    saveUpdate,
    isLoading
  } = useUpdateInfo();
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = {
      Id: null,
      Name: ""
    };
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  let t1;
  if ($[1] !== t) {
    t1 = {
      initialValues: t0,
      validateInputOnChange: true,
      validate: (values) => getValidateErrors(validateSchemeCustomer(t), values)
    };
    $[1] = t;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const userFilialForm = useForm(t1);
  const errors = userFilialForm.errors;
  let t2;
  if ($[3] !== userFilialForm) {
    t2 = userFilialForm.isDirty();
    $[3] = userFilialForm;
    $[4] = t2;
  } else {
    t2 = $[4];
  }
  const isDirtyForm = t2;
  let t3;
  if ($[5] !== errors || $[6] !== isDirtyForm) {
    t3 = Object.keys(errors).length !== 0 || !isDirtyForm;
    $[5] = errors;
    $[6] = isDirtyForm;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  const isDisabled = t3;
  const t4 = (Customer == null ? void 0 : Customer.Id) || null;
  const t5 = (Customer == null ? void 0 : Customer.Name) || "";
  let t6;
  if ($[8] !== t4 || $[9] !== t5) {
    t6 = {
      Id: t4,
      Name: t5
    };
    $[8] = t4;
    $[9] = t5;
    $[10] = t6;
  } else {
    t6 = $[10];
  }
  const fromData = t6;
  let t7;
  if ($[11] !== fromData || $[12] !== userFilialForm) {
    t7 = () => {
      userFilialForm.setValues(fromData);
      userFilialForm.resetDirty(fromData);
    };
    $[11] = fromData;
    $[12] = userFilialForm;
    $[13] = t7;
  } else {
    t7 = $[13];
  }
  let t8;
  if ($[14] !== Customer) {
    t8 = [Customer];
    $[14] = Customer;
    $[15] = t8;
  } else {
    t8 = $[15];
  }
  reactExports.useEffect(t7, t8);
  let t9;
  if ($[16] !== currentId || $[17] !== saveUpdate) {
    t9 = async (values_0) => {
      await saveUpdate({
        Type: ProfileUpdateTypeEnum.Customer,
        Data: {
          [ProfileSectionEnum.Customer]: values_0,
          Id: currentId
        }
      });
    };
    $[16] = currentId;
    $[17] = saveUpdate;
    $[18] = t9;
  } else {
    t9 = $[18];
  }
  const handleSubmit = t9;
  let t10;
  if ($[19] !== handleSubmit || $[20] !== userFilialForm) {
    t10 = userFilialForm.onSubmit(handleSubmit);
    $[19] = handleSubmit;
    $[20] = userFilialForm;
    $[21] = t10;
  } else {
    t10 = $[21];
  }
  let t11;
  if ($[22] !== t) {
    t11 = t("profile.unit");
    $[22] = t;
    $[23] = t11;
  } else {
    t11 = $[23];
  }
  let t12;
  if ($[24] === Symbol.for("react.memo_cache_sentinel")) {
    t12 = {
      base: 1,
      xs: 2
    };
    $[24] = t12;
  } else {
    t12 = $[24];
  }
  let t13;
  if ($[25] !== isLimitedAccess || $[26] !== t || $[27] !== userFilialForm) {
    t13 = isLimitedAccess ? /* @__PURE__ */ jsxRuntimeExports.jsx(Select, { label: t("profile.unit"), required: true, data: [{
      value: ((_a = userFilialForm.values.Id) == null ? void 0 : _a.toString()) || "",
      label: userFilialForm.values.Name || ""
    }], value: (_b = userFilialForm.values.Id) == null ? void 0 : _b.toString(), disabled: true }) : /* @__PURE__ */ jsxRuntimeExports.jsx(SelectWithPagination, { label: t("profile.unit"), placeholder: t("profile.choose"), fieldName: "Id", form: userFilialForm, defaultValue: (_c = userFilialForm == null ? void 0 : userFilialForm.values) == null ? void 0 : _c.Id, value: (_d = userFilialForm == null ? void 0 : userFilialForm.values) == null ? void 0 : _d.Id, filialName: (_e = userFilialForm == null ? void 0 : userFilialForm.values) == null ? void 0 : _e.Name, customerField: "Name" });
    $[25] = isLimitedAccess;
    $[26] = t;
    $[27] = userFilialForm;
    $[28] = t13;
  } else {
    t13 = $[28];
  }
  let t14;
  if ($[29] !== userFilialForm) {
    t14 = userFilialForm.getInputProps("Customer.Id");
    $[29] = userFilialForm;
    $[30] = t14;
  } else {
    t14 = $[30];
  }
  let t15;
  if ($[31] !== t13 || $[32] !== t14.error) {
    t15 = /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: t12, w: "100%", children: [
      t13,
      t14.error
    ] });
    $[31] = t13;
    $[32] = t14.error;
    $[33] = t15;
  } else {
    t15 = $[33];
  }
  let t16;
  if ($[34] !== currentUser || $[35] !== isDisabled || $[36] !== t) {
    t16 = isAdmin(currentUser) && /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", size: "sm", radius: 8, disabled: isDisabled, mt: "md", style: {
      padding: "0 20px"
    }, "data-cy": "saveFilialBtn", children: t("profile.save") });
    $[34] = currentUser;
    $[35] = isDisabled;
    $[36] = t;
    $[37] = t16;
  } else {
    t16 = $[37];
  }
  let t17;
  if ($[38] !== isLoading) {
    t17 = /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading });
    $[38] = isLoading;
    $[39] = t17;
  } else {
    t17 = $[39];
  }
  let t18;
  if ($[40] !== t11 || $[41] !== t15 || $[42] !== t16 || $[43] !== t17) {
    t18 = /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionTemplate, { title: t11, children: [
      t15,
      t16,
      t17
    ] });
    $[40] = t11;
    $[41] = t15;
    $[42] = t16;
    $[43] = t17;
    $[44] = t18;
  } else {
    t18 = $[44];
  }
  let t19;
  if ($[45] !== t10 || $[46] !== t18) {
    t19 = /* @__PURE__ */ jsxRuntimeExports.jsx("form", { onSubmit: t10, children: t18 });
    $[45] = t10;
    $[46] = t18;
    $[47] = t19;
  } else {
    t19 = $[47];
  }
  return t19;
};
function _temp$1(state) {
  return state.userInfo.User;
}
const UserPositions = () => {
  var _a;
  const {
    t
  } = useTranslation();
  const containerRef = reactExports.useRef(null);
  const {
    userInfoCommon,
    isLimitedAccess,
    currentId,
    isLdapUser
  } = reactExports.useContext(EditUserInfoContext);
  const currentUser = useAppSelector((state) => state.userInfo.User);
  const {
    Positions: formPositions
  } = userInfoCommon;
  const {
    saveUpdate,
    isLoading
  } = useUpdateInfo();
  const userPositionsForm = useForm({
    initialValues: {
      Positions: []
    },
    validateInputOnChange: true,
    validate: (values) => {
      const intermediaryValidationErrors = getValidateErrors(validateSchemePositions(t), values.Positions);
      const suffixedValidations = Object.fromEntries(Object.entries(intermediaryValidationErrors).map(([key, value]) => [`Positions.${key}`, value]));
      return suffixedValidations;
    }
  });
  const errors = userPositionsForm.errors;
  const isDirtyForm = userPositionsForm.isDirty();
  const isDisabled = Object.keys(errors).length !== 0 || !isDirtyForm;
  reactExports.useEffect(() => {
    const data = {
      Positions: formPositions
    };
    userPositionsForm.setValues(JSON.parse(JSON.stringify(data)));
    userPositionsForm.resetDirty(JSON.parse(JSON.stringify(data)));
  }, [formPositions]);
  const handleSubmit = async () => {
    var _a2;
    const values_0 = {
      Id: currentId,
      [ProfileSectionEnum.Positions]: ((_a2 = userPositionsForm.values) == null ? void 0 : _a2.Positions) ?? null
    };
    await saveUpdate({
      Type: ProfileUpdateTypeEnum.Positions,
      Data: values_0
    });
  };
  const handleError = (values_1) => {
    var _a2;
    if (Object.keys(values_1).length) {
      for (let i = 0; i < Object.keys(values_1).length; i++) {
        const keyValue = Object.keys(values_1)[i];
        const elementWithInvalidData = (_a2 = containerRef == null ? void 0 : containerRef.current) == null ? void 0 : _a2.querySelector(`[data-name="${keyValue}"]`);
        if (elementWithInvalidData) {
          elementWithInvalidData.scrollIntoView({
            behavior: "smooth",
            block: "center"
          });
          elementWithInvalidData.focus({
            preventScroll: true
          });
          break;
        }
      }
    }
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx("form", { onSubmit: userPositionsForm.onSubmit(handleSubmit, handleError), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { ref: containerRef, style: {
    position: "relative"
  }, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionTemplate, { title: t("profile.position"), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Positions, { Positions: ((_a = userPositionsForm.values) == null ? void 0 : _a.Positions) ?? null, form: userPositionsForm, sectionFormKey: "Positions", isLimitedAccess, disabled: isLdapUser }),
    isAdmin(currentUser) && /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", size: "sm", radius: 8, disabled: isDisabled || isLdapUser, mt: "md", style: {
      padding: "0 20px"
    }, "data-cy": "savePositionsBtn", children: t("profile.save") }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading })
  ] }) }) });
};
const UserRole = () => {
  var _a;
  const $ = compilerRuntimeExports.c(43);
  const {
    t
  } = useTranslation();
  const {
    userInfoCommon,
    isLimitedAccess,
    currentId
  } = reactExports.useContext(EditUserInfoContext);
  const currentUser = useAppSelector(_temp);
  const {
    Roles: userRoles
  } = userInfoCommon;
  const {
    saveUpdate,
    isLoading
  } = useUpdateInfo();
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = {
      initialValues: {
        Roles: []
      },
      validateInputOnChange: true
    };
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const userRolesForm = useForm(t0);
  const errors = userRolesForm.errors;
  let t1;
  if ($[1] !== userRolesForm) {
    t1 = userRolesForm.isDirty();
    $[1] = userRolesForm;
    $[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] !== userRoles) {
    t3 = (userRoles == null ? void 0 : userRoles.map(_temp2)) || [];
    $[6] = userRoles;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  let t4;
  if ($[8] !== t3) {
    t4 = {
      Roles: t3
    };
    $[8] = t3;
    $[9] = t4;
  } else {
    t4 = $[9];
  }
  const fromData = t4;
  let t5;
  if ($[10] !== fromData || $[11] !== userRolesForm) {
    t5 = () => {
      userRolesForm.setValues(fromData);
      userRolesForm.resetDirty(fromData);
    };
    $[10] = fromData;
    $[11] = userRolesForm;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  let t6;
  if ($[13] !== userRoles) {
    t6 = [userRoles];
    $[13] = userRoles;
    $[14] = t6;
  } else {
    t6 = $[14];
  }
  reactExports.useEffect(t5, t6);
  let t7;
  if ($[15] !== currentId || $[16] !== fromData || $[17] !== saveUpdate || $[18] !== userRolesForm) {
    t7 = async () => {
      var _a2;
      const values = {
        Id: currentId,
        [ProfileSectionEnum.Roles]: ((_a2 = userRolesForm.values) == null ? void 0 : _a2.Roles) ?? null
      };
      await saveUpdate({
        Type: ProfileUpdateTypeEnum.Roles,
        Data: values
      });
      userRolesForm.setValues(fromData);
      userRolesForm.resetDirty(fromData);
    };
    $[15] = currentId;
    $[16] = fromData;
    $[17] = saveUpdate;
    $[18] = userRolesForm;
    $[19] = t7;
  } else {
    t7 = $[19];
  }
  const handleSubmit = t7;
  let t8;
  if ($[20] !== handleSubmit || $[21] !== userRolesForm) {
    t8 = userRolesForm.onSubmit(handleSubmit);
    $[20] = handleSubmit;
    $[21] = userRolesForm;
    $[22] = t8;
  } else {
    t8 = $[22];
  }
  let t9;
  if ($[23] !== t) {
    t9 = t("profile.roles");
    $[23] = t;
    $[24] = t9;
  } else {
    t9 = $[24];
  }
  const t10 = ((_a = userRolesForm.values) == null ? void 0 : _a.Roles) ?? null;
  let t11;
  if ($[25] !== isLimitedAccess || $[26] !== t10 || $[27] !== userRolesForm) {
    t11 = /* @__PURE__ */ jsxRuntimeExports.jsx(Roles, { isEditPage: true, Roles: t10, fieldName: "Roles", form: userRolesForm, isLimitedAccess });
    $[25] = isLimitedAccess;
    $[26] = t10;
    $[27] = userRolesForm;
    $[28] = t11;
  } else {
    t11 = $[28];
  }
  let t12;
  if ($[29] !== currentUser || $[30] !== isDisabled || $[31] !== t) {
    t12 = isAdmin(currentUser) && /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", size: "sm", radius: 8, disabled: isDisabled, mt: "md", style: {
      padding: "0 20px"
    }, "data-cy": "saveRolesBtn", children: t("profile.save") });
    $[29] = currentUser;
    $[30] = isDisabled;
    $[31] = t;
    $[32] = t12;
  } else {
    t12 = $[32];
  }
  let t13;
  if ($[33] !== isLoading) {
    t13 = /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading });
    $[33] = isLoading;
    $[34] = t13;
  } else {
    t13 = $[34];
  }
  let t14;
  if ($[35] !== t11 || $[36] !== t12 || $[37] !== t13 || $[38] !== t9) {
    t14 = /* @__PURE__ */ jsxRuntimeExports.jsxs(SectionTemplate, { title: t9, children: [
      t11,
      t12,
      t13
    ] });
    $[35] = t11;
    $[36] = t12;
    $[37] = t13;
    $[38] = t9;
    $[39] = t14;
  } else {
    t14 = $[39];
  }
  let t15;
  if ($[40] !== t14 || $[41] !== t8) {
    t15 = /* @__PURE__ */ jsxRuntimeExports.jsx("form", { onSubmit: t8, children: t14 });
    $[40] = t14;
    $[41] = t8;
    $[42] = t15;
  } else {
    t15 = $[42];
  }
  return t15;
};
function _temp(state) {
  return state.userInfo.User;
}
function _temp2(role) {
  return {
    Id: role == null ? void 0 : role.Id,
    Name: role == null ? void 0 : role.Name,
    IsRemovable: role == null ? void 0 : role.IsRemovable
  };
}
const FilialData = () => {
  const $ = compilerRuntimeExports.c(1);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(UserFilial, {}),
      /* @__PURE__ */ jsxRuntimeExports.jsx(UserPositions, {}),
      /* @__PURE__ */ jsxRuntimeExports.jsx(UserRole, {})
    ] });
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  return t0;
};
export {
  FilialData as default
};
//# sourceMappingURL=index-Bn6PTY2_.js.map
