import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { c as createStyles, au as useCombobox, g as Loader, k as Tooltip, aB as CloseButton, a4 as Select, av as Combobox, E as TextInput, b as Box, F as Flex } from "./vendor-mantine-h0WlM2Mj.js";
import { cj as useLazyGetQuotasQuery, I as IFiltersEnumType } from "./index-DfebxkL9.js";
import { E as ErrorIcon } from "./errorIcon-PZO0f1IY.js";
import { b as gq } from "./vendor-icons-xWhXK_Ui.js";
import { I as InfiniteScroll } from "./index.es-BTZFX8B-.js";
import { a as formatDataSizeUnits } from "./format-CMT-x-V3.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
const useStyles = createStyles((theme) => ({
  select: {
    "& .mantine-Select-input": {
      borderRadius: "8px",
      border: "1px solid #D0D5DD"
    }
  },
  selectInput: {
    cursor: "pointer",
    "& .mantine-Input-input": {
      borderRadius: "8px"
    }
  },
  options: {
    "& .mantine-Combobox-option": {
      "&[data-combobox-active=true]": {
        backgroundColor: theme.colors.blue[5],
        color: theme.white
      }
    }
  },
  infiniteScrollMoreData: {
    width: "100%",
    height: "50px",
    display: "flex",
    alignItems: "center",
    justifyContent: "center"
  },
  infiniteScroll: {
    "&::-webkit-scrollbar": {
      width: "8px",
      height: "10px"
    },
    "&::-webkit-scrollbar-thumb": {
      borderRadius: "8px",
      background: "#98A2B3"
    },
    "&::-webkit-scrollbar-track": {
      background: "#F9FAFB"
    },
    scrollbarColor: "#98A2B3 #F9FAFB"
  }
}));
function useLoadQuotas(Customer, createUser) {
  const $ = compilerRuntimeExports.c(29);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = [];
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const [quotas, setQuotas] = reactExports.useState(t0);
  const [quotaKey, setQuotaKey] = reactExports.useState(0);
  const [defaultQuotaId, setDefaultQuotaId] = reactExports.useState("");
  let t1;
  if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = {
      Count: 10,
      Offset: 0,
      LoadUseCount: false
    };
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const [filters, setFilters] = reactExports.useState(t1);
  const [getQuotas, t2] = useLazyGetQuotasQuery();
  const {
    isFetching,
    isError,
    data
  } = t2;
  let t3;
  if ($[2] !== Customer || $[3] !== filters || $[4] !== getQuotas) {
    t3 = () => {
      if (Customer) {
        getQuotas({
          FilterConditions: [{
            Type: IFiltersEnumType.FilialFilter,
            NumValues: [Customer]
          }],
          ...filters
        });
      }
    };
    $[2] = Customer;
    $[3] = filters;
    $[4] = getQuotas;
    $[5] = t3;
  } else {
    t3 = $[5];
  }
  let t4;
  if ($[6] !== Customer || $[7] !== filters) {
    t4 = [Customer, filters];
    $[6] = Customer;
    $[7] = filters;
    $[8] = t4;
  } else {
    t4 = $[8];
  }
  reactExports.useEffect(t3, t4);
  const total = (data == null ? void 0 : data.TotalCount) || 0;
  let t5;
  if ($[9] !== filters || $[10] !== total) {
    t5 = () => {
      if (total - filters.Offset > 0) {
        setFilters({
          ...filters,
          Offset: filters.Offset + 10
        });
      }
    };
    $[9] = filters;
    $[10] = total;
    $[11] = t5;
  } else {
    t5 = $[11];
  }
  const loadMore = t5;
  let t6;
  if ($[12] !== createUser || $[13] !== data || $[14] !== filters || $[15] !== quotas) {
    t6 = () => {
      if (data) {
        setQuotas(filters.Offset > 0 ? quotas.concat(data.Quotas) : data.Quotas);
        setQuotaKey(Math.random());
        if (createUser) {
          if (data.DefaultItemId) {
            setDefaultQuotaId(data.DefaultItemId.toString());
          } else {
            setDefaultQuotaId("");
          }
        }
      }
    };
    $[12] = createUser;
    $[13] = data;
    $[14] = filters;
    $[15] = quotas;
    $[16] = t6;
  } else {
    t6 = $[16];
  }
  let t7;
  if ($[17] !== data) {
    t7 = [data];
    $[17] = data;
    $[18] = t7;
  } else {
    t7 = $[18];
  }
  reactExports.useEffect(t6, t7);
  let t8;
  if ($[19] !== quotas) {
    t8 = Array.isArray(quotas) ? quotas == null ? void 0 : quotas.map(_temp$1) : [];
    $[19] = quotas;
    $[20] = t8;
  } else {
    t8 = $[20];
  }
  const list = t8;
  let t9;
  if ($[21] !== defaultQuotaId || $[22] !== isError || $[23] !== isFetching || $[24] !== list || $[25] !== loadMore || $[26] !== quotaKey || $[27] !== total) {
    t9 = {
      list,
      total,
      isFetching,
      isError,
      quotaKey,
      defaultQuotaId,
      loadMore
    };
    $[21] = defaultQuotaId;
    $[22] = isError;
    $[23] = isFetching;
    $[24] = list;
    $[25] = loadMore;
    $[26] = quotaKey;
    $[27] = total;
    $[28] = t9;
  } else {
    t9 = $[28];
  }
  return t9;
}
function _temp$1(Item) {
  return {
    label: String(Item.name),
    value: Item.id.toString(),
    size: Item.size
  };
}
const SelectQuotas = (t0) => {
  const $ = compilerRuntimeExports.c(58);
  const {
    form,
    handleChange,
    fieldName,
    value,
    QuotaName,
    isLimitedAccess,
    Customer,
    createUser,
    notClearable,
    label,
    quotaField,
    className
  } = t0;
  const {
    classes,
    cx
  } = useStyles();
  const {
    t
  } = useTranslation();
  const {
    list,
    total,
    isFetching,
    isError,
    defaultQuotaId,
    loadMore
  } = useLoadQuotas(Customer, createUser);
  const error = form ? form.errors[`${fieldName}.Id`] : "";
  let t1;
  if ($[0] !== fieldName || $[1] !== form || $[2] !== handleChange) {
    t1 = (query) => {
      if (form) {
        form == null ? void 0 : form.setFieldValue(fieldName, query ? Number(query) : null);
      } else {
        handleChange && handleChange(query);
      }
    };
    $[0] = fieldName;
    $[1] = form;
    $[2] = handleChange;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  const handleChangeQuota = t1;
  let t2;
  if ($[4] !== fieldName || $[5] !== form || $[6] !== quotaField) {
    t2 = () => {
      if (form) {
        form == null ? void 0 : form.setFieldValue(fieldName, null);
        form == null ? void 0 : form.setFieldValue(quotaField, "");
      }
    };
    $[4] = fieldName;
    $[5] = form;
    $[6] = quotaField;
    $[7] = t2;
  } else {
    t2 = $[7];
  }
  const clearQuota = t2;
  let t3;
  if ($[8] !== form || $[9] !== list || $[10] !== quotaField || $[11] !== value) {
    t3 = () => {
      if (value) {
        const quotaItem = list.find((item) => item.value === value.toString());
        if (quotaItem) {
          form == null ? void 0 : form.setFieldValue(quotaField, quotaItem.label);
        }
      }
    };
    $[8] = form;
    $[9] = list;
    $[10] = quotaField;
    $[11] = value;
    $[12] = t3;
  } else {
    t3 = $[12];
  }
  let t4;
  if ($[13] !== list || $[14] !== value) {
    t4 = [value, list];
    $[13] = list;
    $[14] = value;
    $[15] = t4;
  } else {
    t4 = $[15];
  }
  reactExports.useEffect(t3, t4);
  let t5;
  if ($[16] !== defaultQuotaId || $[17] !== fieldName || $[18] !== form || $[19] !== list || $[20] !== quotaField) {
    t5 = () => {
      var _a;
      if (defaultQuotaId) {
        form == null ? void 0 : form.setFieldValue(fieldName, Number(defaultQuotaId));
        form == null ? void 0 : form.setFieldValue(quotaField, (_a = list.find((item_0) => item_0.value === defaultQuotaId)) == null ? void 0 : _a.label);
      }
    };
    $[16] = defaultQuotaId;
    $[17] = fieldName;
    $[18] = form;
    $[19] = list;
    $[20] = quotaField;
    $[21] = t5;
  } else {
    t5 = $[21];
  }
  let t6;
  if ($[22] !== defaultQuotaId) {
    t6 = [defaultQuotaId];
    $[22] = defaultQuotaId;
    $[23] = t6;
  } else {
    t6 = $[23];
  }
  reactExports.useEffect(t5, t6);
  const combobox = useCombobox({
    onDropdownClose: () => combobox.resetSelectedOption()
  });
  let t7;
  if ($[24] !== list || $[25] !== value) {
    let t82;
    if ($[27] !== value) {
      t82 = (item_1) => /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Option, { value: item_1.value, active: item_1.value === (value == null ? void 0 : value.toString()), children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { w: "100%", justify: "space-between", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { children: item_1.label }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { children: [
          " ",
          formatDataSizeUnits(item_1 == null ? void 0 : item_1.size)
        ] })
      ] }) }, item_1.value);
      $[27] = value;
      $[28] = t82;
    } else {
      t82 = $[28];
    }
    t7 = list.map(t82);
    $[24] = list;
    $[25] = value;
    $[26] = t7;
  } else {
    t7 = $[26];
  }
  const options = t7;
  let t8;
  if ($[29] !== clearQuota || $[30] !== combobox || $[31] !== isError || $[32] !== isFetching || $[33] !== list.length || $[34] !== notClearable || $[35] !== t || $[36] !== value) {
    t8 = isFetching ? /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "xs" }) : isError ? /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { zIndex: 1400, color: "red", label: t("profile.failedToLoadData"), children: /* @__PURE__ */ jsxRuntimeExports.jsx(ErrorIcon, {}) }) : list.length > 0 ? notClearable ? /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3", onClick: () => combobox.toggleDropdown() }) : value === null ? /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3", onClick: () => combobox.toggleDropdown() }) : /* @__PURE__ */ jsxRuntimeExports.jsx(CloseButton, { size: "sm", onMouseDown: _temp, onClick: () => clearQuota() }) : null;
    $[29] = clearQuota;
    $[30] = combobox;
    $[31] = isError;
    $[32] = isFetching;
    $[33] = list.length;
    $[34] = notClearable;
    $[35] = t;
    $[36] = value;
    $[37] = t8;
  } else {
    t8 = $[37];
  }
  const rightSectionContent = t8;
  let t9;
  if ($[38] !== QuotaName || $[39] !== className || $[40] !== classes || $[41] !== combobox || $[42] !== cx || $[43] !== error || $[44] !== fieldName || $[45] !== form || $[46] !== handleChangeQuota || $[47] !== isLimitedAccess || $[48] !== label || $[49] !== list || $[50] !== loadMore || $[51] !== notClearable || $[52] !== options || $[53] !== rightSectionContent || $[54] !== t || $[55] !== total || $[56] !== value) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isLimitedAccess ? /* @__PURE__ */ jsxRuntimeExports.jsx(Select, { data: [{
      value: (value == null ? void 0 : value.toString()) || "",
      label: QuotaName || ""
    }], value: `${value || ""}`, readOnly: true, sx: {
      width: "100%"
    }, error, disabled: true }) : /* @__PURE__ */ jsxRuntimeExports.jsxs(Combobox, { store: combobox, position: "bottom", withinPortal: false, zIndex: 1400, onOptionSubmit: (val) => {
      handleChangeQuota(val);
      combobox.closeDropdown();
    }, "data-cy": "addRoleQuotaSelectt", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Target, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(TextInput, { placeholder: list.length ? t("profile.selectDefaultQuota") : t("filials.diskSpace.noQuotas"), readOnly: true, value: QuotaName || "", className: cx(classes.selectInput, className), pointer: true, rightSection: rightSectionContent, onClick: () => combobox.toggleDropdown(), label: label ? label : t("profile.quota"), error: form == null ? void 0 : form.getInputProps(fieldName).error, required: !!notClearable }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Dropdown, { hidden: !list.length, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Combobox.Options, { className: classes.options, children: /* @__PURE__ */ jsxRuntimeExports.jsx(InfiniteScroll, { scrollableTarget: "scrollableDiv", dataLength: list == null ? void 0 : list.length, next: loadMore, height: list.length <= 5 ? "auto" : "140px", hasMore: list ? total > (list == null ? void 0 : list.length) : false, loader: /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: Boolean(list == null ? void 0 : list.length) && /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.infiniteScrollMoreData, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { size: "sm" }) }) }), className: classes.infiniteScroll, children: options }) }) })
    ] }) });
    $[38] = QuotaName;
    $[39] = className;
    $[40] = classes;
    $[41] = combobox;
    $[42] = cx;
    $[43] = error;
    $[44] = fieldName;
    $[45] = form;
    $[46] = handleChangeQuota;
    $[47] = isLimitedAccess;
    $[48] = label;
    $[49] = list;
    $[50] = loadMore;
    $[51] = notClearable;
    $[52] = options;
    $[53] = rightSectionContent;
    $[54] = t;
    $[55] = total;
    $[56] = value;
    $[57] = t9;
  } else {
    t9 = $[57];
  }
  return t9;
};
function _temp(event) {
  return event.preventDefault();
}
export {
  SelectQuotas as S
};
//# sourceMappingURL=index-BoQKW1g7.js.map
