import { r as reactExports, E as compilerRuntimeExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { b_ as useBulkAddRolesMutation, c as useAppSelector, l as useLazyGetUsersQuery, b as useActions, a6 as clearApiCacheByTags, ae as usersApi, a7 as rolesApi, m as universalNotification, O as themeColors } from "./index-DfebxkL9.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { w as qXe } from "./vendor-icons-xWhXK_Ui.js";
import { c as createStyles, F as Flex, h as ActionIcon } from "./vendor-mantine-h0WlM2Mj.js";
import { O as OverflowTip } from "./index-C-CFogWu.js";
const useAddRoles = (selection, setRowData) => {
  const {
    t
  } = useTranslation();
  const [fetchAddRoles, resultAddRoles] = useBulkAddRolesMutation();
  const {
    filters
  } = useAppSelector((state) => state.users);
  const [fetchGetUsers] = useLazyGetUsersQuery();
  const {
    setUsers
  } = useActions();
  const handleAddRoles = reactExports.useCallback(async (selection_0, roleIds) => {
    const numbersSelection = (selection_0 == null ? void 0 : selection_0.map((item) => Number(item))) ?? [];
    const roleIdsUnique = new Set(roleIds);
    try {
      const res = await fetchAddRoles({
        UserIds: numbersSelection,
        RoleIds: [...roleIdsUnique]
      });
      if (!Object.keys(res).includes("error")) {
        if (setRowData) {
          const {
            data
          } = await fetchGetUsers({
            Count: filters.Offset >= 20 ? filters.Offset + 20 : 20,
            Offset: 0,
            SortOrder: filters.SortOrder,
            SortField: filters.SortField,
            Filters: filters.Filters,
            Query: filters.Query
          });
          if (setRowData && data) {
            setUsers(data);
            setRowData(data.Users);
          }
        } else {
          clearApiCacheByTags(usersApi, ["Users"]);
        }
        clearApiCacheByTags(rolesApi, ["Roles"]);
      }
    } catch (err) {
      console.warn("fetchGetUsers ERROR", err);
    }
  }, [selection]);
  reactExports.useEffect(() => {
    const {
      isSuccess,
      isError,
      error
    } = resultAddRoles;
    const _errorStatus = error == null ? void 0 : error.data;
    universalNotification({
      isSuccess,
      isError,
      error: _errorStatus,
      successTitle: t("roles.addSuccess")
    });
  }, [resultAddRoles]);
  return {
    handleAddRoles
  };
};
const useStyles = createStyles((theme) => ({
  badge: {
    fontSize: "14px",
    fontWeight: 400,
    border: "none",
    borderRadius: "16px",
    backgroundColor: theme.colors.backgroundGray[5],
    marginTop: theme.spacing.xs,
    padding: "4px 12px",
    cursor: "pointer",
    maxWidth: "290px",
    width: "fit-content",
    alignItems: "center"
  }
}));
const Badges = (t0) => {
  const $ = compilerRuntimeExports.c(9);
  const {
    label,
    onRemove
  } = t0;
  const {
    classes
  } = useStyles();
  let t1;
  if ($[0] !== label) {
    t1 = /* @__PURE__ */ jsxRuntimeExports.jsx(OverflowTip, { children: label });
    $[0] = label;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  let t2;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = /* @__PURE__ */ jsxRuntimeExports.jsx(qXe, { size: 16, color: themeColors.brandGray[0] });
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  let t3;
  if ($[3] !== onRemove) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { ml: 5, size: 16, variant: "transparent", onClick: onRemove, children: t2 });
    $[3] = onRemove;
    $[4] = t3;
  } else {
    t3 = $[4];
  }
  let t4;
  if ($[5] !== classes.badge || $[6] !== t1 || $[7] !== t3) {
    t4 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { className: classes.badge, children: [
      t1,
      t3
    ] });
    $[5] = classes.badge;
    $[6] = t1;
    $[7] = t3;
    $[8] = t4;
  } else {
    t4 = $[8];
  }
  return t4;
};
export {
  Badges as B,
  useAddRoles as u
};
//# sourceMappingURL=index-woYEqWvh.js.map
