import { E as compilerRuntimeExports, j as jsxRuntimeExports, r as reactExports } from "./vendor-react-DYNCg6N5.js";
import { E as EmailInfoPanelsEnum } from "./index-Rv8Lmn4U.js";
import { O as themeColors, ck as useGetModuleRolesQuery, cl as useGetModuleUsersQuery, cm as useAddModuleRoleMutation, cn as useAddModuleUserMutation, co as useDeleteModuleRoleMutation, cp as useDeleteModuleUserMutation, m as universalNotification, E as ErrorBoundary } from "./index-DfebxkL9.js";
import { S as SelectUserModal } from "./index-DFEP7Sv_.js";
import { S as SelectGroupSimpleModal } from "./index-BWhdniX1.js";
import { w as qXe } from "./vendor-icons-xWhXK_Ui.js";
import { c as createStyles, O as Badge, h as ActionIcon, b as Box, T as Text, g as Loader, F as Flex } from "./vendor-mantine-h0WlM2Mj.js";
import { u as useStyles$1 } from "./styles-rKP3r-bS.js";
import { A as AddButton } from "./index-72JBTuLT.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
const useStyles = createStyles((theme) => ({
  badge: {
    paddingRight: 3,
    backgroundColor: theme.colors.backgroundGray[6],
    color: "black",
    borderRadius: "16px",
    marginRight: theme.spacing.xs,
    marginTop: theme.spacing.xs,
    border: "none",
    textTransform: "inherit",
    height: "28px",
    fontSize: "14px",
    padding: theme.spacing.xs,
    fontWeight: 400
  }
}));
const Badges = (t0) => {
  const $ = compilerRuntimeExports.c(9);
  const {
    data,
    onRemove
  } = t0;
  const {
    classes
  } = useStyles();
  let t1;
  if ($[0] !== classes || $[1] !== data || $[2] !== onRemove) {
    let t22;
    if ($[4] !== classes || $[5] !== onRemove) {
      t22 = (t3) => {
        const {
          id,
          name
        } = t3;
        return /* @__PURE__ */ jsxRuntimeExports.jsx(Badge, { className: classes.badge, variant: "outline", rightSection: /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { size: 16, variant: "transparent", onClick: () => onRemove == null ? void 0 : onRemove(id), children: /* @__PURE__ */ jsxRuntimeExports.jsx(qXe, { size: 16, color: themeColors.brandGray[0] }) }), children: name }, id);
      };
      $[4] = classes;
      $[5] = onRemove;
      $[6] = t22;
    } else {
      t22 = $[6];
    }
    t1 = data.map(t22);
    $[0] = classes;
    $[1] = data;
    $[2] = onRemove;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[7] !== t1) {
    t2 = /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: t1 });
    $[7] = t1;
    $[8] = t2;
  } else {
    t2 = $[8];
  }
  return t2;
};
const CommonModuleContainer = (t0) => {
  const $ = compilerRuntimeExports.c(109);
  const {
    currentModule,
    filial
  } = t0;
  const {
    t
  } = useTranslation();
  const [isUserModalOpen, setIsUserModalOpen] = reactExports.useState(false);
  const [isRoleModalOpen, setIsRoleModalOpen] = reactExports.useState(false);
  const [selectedUser, setSelectedUser] = reactExports.useState(null);
  const t1 = filial == null ? void 0 : filial.Id;
  let t2;
  if ($[0] !== currentModule || $[1] !== t1) {
    t2 = {
      module: currentModule,
      id: t1
    };
    $[0] = currentModule;
    $[1] = t1;
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  const {
    data: rolesData,
    error: rolesError,
    isError: isRolesError,
    isLoading: areRolesLoading
  } = useGetModuleRolesQuery(t2);
  const t3 = filial == null ? void 0 : filial.Id;
  let t4;
  if ($[3] !== currentModule || $[4] !== t3) {
    t4 = {
      module: currentModule,
      id: t3
    };
    $[3] = currentModule;
    $[4] = t3;
    $[5] = t4;
  } else {
    t4 = $[5];
  }
  const {
    data: usersData,
    error: usersError,
    isError: isUsersError,
    isLoading: areUsersLoading
  } = useGetModuleUsersQuery(t4);
  let t5;
  let t6;
  if ($[6] !== isUsersError || $[7] !== usersError) {
    t5 = () => {
      if (isUsersError && usersError) {
        universalNotification({
          error: usersError,
          isError: isUsersError,
          isSuccess: false
        });
      }
    };
    t6 = [isUsersError, usersError];
    $[6] = isUsersError;
    $[7] = usersError;
    $[8] = t5;
    $[9] = t6;
  } else {
    t5 = $[8];
    t6 = $[9];
  }
  reactExports.useEffect(t5, t6);
  let t7;
  let t8;
  if ($[10] !== isRolesError || $[11] !== rolesError) {
    t7 = () => {
      if (isRolesError && rolesError) {
        universalNotification({
          error: rolesError,
          isError: isRolesError,
          isSuccess: false
        });
      }
    };
    t8 = [isRolesError, rolesError];
    $[10] = isRolesError;
    $[11] = rolesError;
    $[12] = t7;
    $[13] = t8;
  } else {
    t7 = $[12];
    t8 = $[13];
  }
  reactExports.useEffect(t7, t8);
  const [addRole] = useAddModuleRoleMutation();
  const [addUser] = useAddModuleUserMutation();
  const [deleteRole] = useDeleteModuleRoleMutation();
  const [deleteUser] = useDeleteModuleUserMutation();
  const {
    classes
  } = useStyles$1();
  let t9;
  if ($[14] === Symbol.for("react.memo_cache_sentinel")) {
    t9 = () => {
      setIsUserModalOpen(false);
      setIsRoleModalOpen(false);
    };
    $[14] = t9;
  } else {
    t9 = $[14];
  }
  const closeModals = t9;
  let t10;
  if ($[15] !== addUser || $[16] !== currentModule || $[17] !== selectedUser) {
    t10 = async () => {
      if (!selectedUser) {
        return;
      }
      await addUser({
        module: currentModule,
        userId: selectedUser.Id
      });
      closeModals();
    };
    $[15] = addUser;
    $[16] = currentModule;
    $[17] = selectedUser;
    $[18] = t10;
  } else {
    t10 = $[18];
  }
  const handleConfirmSelectUserModal = t10;
  const t11 = classes.container;
  const t12 = classes.section;
  const t13 = classes.title;
  let t14;
  if ($[19] !== t) {
    t14 = t("filials.modules.rolesAccess");
    $[19] = t;
    $[20] = t14;
  } else {
    t14 = $[20];
  }
  let t15;
  if ($[21] !== classes.title || $[22] !== t14) {
    t15 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t13, children: t14 });
    $[21] = classes.title;
    $[22] = t14;
    $[23] = t15;
  } else {
    t15 = $[23];
  }
  const t16 = classes.description;
  let t17;
  if ($[24] !== t) {
    t17 = t("filials.modules.rolesHint");
    $[24] = t;
    $[25] = t17;
  } else {
    t17 = $[25];
  }
  let t18;
  if ($[26] !== classes.description || $[27] !== t17) {
    t18 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t16, fz: "sm", mt: "xs", children: t17 });
    $[26] = classes.description;
    $[27] = t17;
    $[28] = t18;
  } else {
    t18 = $[28];
  }
  let t19;
  if ($[29] === Symbol.for("react.memo_cache_sentinel")) {
    t19 = () => setIsRoleModalOpen(true);
    $[29] = t19;
  } else {
    t19 = $[29];
  }
  let t20;
  if ($[30] !== t) {
    t20 = t("filials.modules.addRole");
    $[30] = t;
    $[31] = t20;
  } else {
    t20 = $[31];
  }
  let t21;
  if ($[32] !== t20) {
    t21 = /* @__PURE__ */ jsxRuntimeExports.jsx(AddButton, { mt: "sm", onClick: t19, children: t20 });
    $[32] = t20;
    $[33] = t21;
  } else {
    t21 = $[33];
  }
  let t22;
  if ($[34] !== areRolesLoading || $[35] !== currentModule || $[36] !== deleteRole || $[37] !== rolesData) {
    t22 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { mt: 16, children: areRolesLoading ? /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "sm" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(Badges, { data: rolesData || [], onRemove: async (roleId) => {
      await deleteRole({
        module: currentModule,
        roleId
      });
    } }) });
    $[34] = areRolesLoading;
    $[35] = currentModule;
    $[36] = deleteRole;
    $[37] = rolesData;
    $[38] = t22;
  } else {
    t22 = $[38];
  }
  let t23;
  if ($[39] !== classes.section || $[40] !== t15 || $[41] !== t18 || $[42] !== t21 || $[43] !== t22) {
    t23 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: t12, "data-section": EmailInfoPanelsEnum.byRoles, children: [
      t15,
      t18,
      t21,
      t22
    ] });
    $[39] = classes.section;
    $[40] = t15;
    $[41] = t18;
    $[42] = t21;
    $[43] = t22;
    $[44] = t23;
  } else {
    t23 = $[44];
  }
  const t24 = classes.section;
  const t25 = classes.title;
  let t26;
  if ($[45] !== t) {
    t26 = t("filials.modules.userAccess");
    $[45] = t;
    $[46] = t26;
  } else {
    t26 = $[46];
  }
  let t27;
  if ($[47] !== classes.title || $[48] !== t26) {
    t27 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t25, children: t26 });
    $[47] = classes.title;
    $[48] = t26;
    $[49] = t27;
  } else {
    t27 = $[49];
  }
  const t28 = classes.description;
  let t29;
  if ($[50] !== t) {
    t29 = t("filials.modules.userHint");
    $[50] = t;
    $[51] = t29;
  } else {
    t29 = $[51];
  }
  let t30;
  if ($[52] !== classes.description || $[53] !== t29) {
    t30 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { className: t28, fz: "sm", mt: "xs", children: t29 });
    $[52] = classes.description;
    $[53] = t29;
    $[54] = t30;
  } else {
    t30 = $[54];
  }
  let t31;
  if ($[55] === Symbol.for("react.memo_cache_sentinel")) {
    t31 = () => setIsUserModalOpen(true);
    $[55] = t31;
  } else {
    t31 = $[55];
  }
  let t32;
  if ($[56] !== t) {
    t32 = t("filials.modules.addUser");
    $[56] = t;
    $[57] = t32;
  } else {
    t32 = $[57];
  }
  let t33;
  if ($[58] !== t32) {
    t33 = /* @__PURE__ */ jsxRuntimeExports.jsx(AddButton, { mt: "sm", onClick: t31, children: t32 });
    $[58] = t32;
    $[59] = t33;
  } else {
    t33 = $[59];
  }
  let t34;
  if ($[60] !== areUsersLoading || $[61] !== currentModule || $[62] !== deleteUser || $[63] !== usersData) {
    t34 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { mt: 16, children: areUsersLoading ? /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "sm" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(Badges, { data: usersData || [], onRemove: async (userId) => {
      deleteUser({
        module: currentModule,
        userId
      });
    } }) });
    $[60] = areUsersLoading;
    $[61] = currentModule;
    $[62] = deleteUser;
    $[63] = usersData;
    $[64] = t34;
  } else {
    t34 = $[64];
  }
  let t35;
  if ($[65] !== classes.section || $[66] !== t27 || $[67] !== t30 || $[68] !== t33 || $[69] !== t34) {
    t35 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: t24, "data-section": EmailInfoPanelsEnum.byUsers, children: [
      t27,
      t30,
      t33,
      t34
    ] });
    $[65] = classes.section;
    $[66] = t27;
    $[67] = t30;
    $[68] = t33;
    $[69] = t34;
    $[70] = t35;
  } else {
    t35 = $[70];
  }
  let t36;
  if ($[71] !== classes.container || $[72] !== t23 || $[73] !== t35) {
    t36 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { className: t11, direction: "column", children: [
      t23,
      t35
    ] });
    $[71] = classes.container;
    $[72] = t23;
    $[73] = t35;
    $[74] = t36;
  } else {
    t36 = $[74];
  }
  let t37;
  if ($[75] !== t) {
    t37 = t("filials.modules.addRole");
    $[75] = t;
    $[76] = t37;
  } else {
    t37 = $[76];
  }
  let t38;
  if ($[77] !== addRole || $[78] !== currentModule) {
    t38 = async (id) => {
      await addRole({
        module: currentModule,
        roleId: id
      });
      closeModals();
    };
    $[77] = addRole;
    $[78] = currentModule;
    $[79] = t38;
  } else {
    t38 = $[79];
  }
  let t39;
  if ($[80] !== filial || $[81] !== isRoleModalOpen || $[82] !== t37 || $[83] !== t38) {
    t39 = /* @__PURE__ */ jsxRuntimeExports.jsx(SelectGroupSimpleModal, { onClose: closeModals, isOpened: isRoleModalOpen, title: t37, setGroupId: t38, filial });
    $[80] = filial;
    $[81] = isRoleModalOpen;
    $[82] = t37;
    $[83] = t38;
    $[84] = t39;
  } else {
    t39 = $[84];
  }
  let t40;
  if ($[85] !== t) {
    t40 = t("filials.modules.addUserInGroup");
    $[85] = t;
    $[86] = t40;
  } else {
    t40 = $[86];
  }
  const t41 = selectedUser == null ? void 0 : selectedUser.Id;
  let t42;
  if ($[87] !== t) {
    t42 = t("add");
    $[87] = t;
    $[88] = t42;
  } else {
    t42 = $[88];
  }
  let t43;
  if ($[89] !== handleConfirmSelectUserModal || $[90] !== t42) {
    t43 = {
      btnName: t42,
      fn: handleConfirmSelectUserModal
    };
    $[89] = handleConfirmSelectUserModal;
    $[90] = t42;
    $[91] = t43;
  } else {
    t43 = $[91];
  }
  let t44;
  if ($[92] !== t) {
    t44 = t("cancel");
    $[92] = t;
    $[93] = t44;
  } else {
    t44 = $[93];
  }
  let t45;
  if ($[94] !== t44) {
    t45 = {
      btnName: t44,
      fn: closeModals
    };
    $[94] = t44;
    $[95] = t45;
  } else {
    t45 = $[95];
  }
  let t46;
  if ($[96] !== t43 || $[97] !== t45) {
    t46 = {
      confirmProps: t43,
      cancelProps: t45
    };
    $[96] = t43;
    $[97] = t45;
    $[98] = t46;
  } else {
    t46 = $[98];
  }
  let t47;
  if ($[99] !== filial || $[100] !== isUserModalOpen || $[101] !== t40 || $[102] !== t41 || $[103] !== t46) {
    t47 = /* @__PURE__ */ jsxRuntimeExports.jsx(SelectUserModal, { isOpened: isUserModalOpen, title: t40, onClose: closeModals, setSelectedUsers: setSelectedUser, multipleUsers: false, userId: t41, controlSection: true, controlSectionProps: t46, filial });
    $[99] = filial;
    $[100] = isUserModalOpen;
    $[101] = t40;
    $[102] = t41;
    $[103] = t46;
    $[104] = t47;
  } else {
    t47 = $[104];
  }
  let t48;
  if ($[105] !== t36 || $[106] !== t39 || $[107] !== t47) {
    t48 = /* @__PURE__ */ jsxRuntimeExports.jsxs(ErrorBoundary, { componentTitle: "Modules page", children: [
      t36,
      t39,
      t47
    ] });
    $[105] = t36;
    $[106] = t39;
    $[107] = t47;
    $[108] = t48;
  } else {
    t48 = $[108];
  }
  return t48;
};
export {
  CommonModuleContainer as C
};
//# sourceMappingURL=index-DwQBSy2U.js.map
