import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { T as TableHeader, P as PaginatedTable } from "./index-DXVFB9sp.js";
import { f as useGetDefaultUserFiltersQuery, I as IFiltersEnumType, h as filtersLabelsKeys, cX as IDiskSpaceSortField, c as useAppSelector, z as transformConditionals, bm as listSizes, b as useActions, cY as useGetDiskSpaceQuery, cZ as useLazyGetDiskSpaceQuery, bu as useGetFilterListItemsQuery, c_ as initialStatisticFilter } from "./index-DfebxkL9.js";
import { F as FilterList } from "./index-CPnWygUh.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { a as formatDataSizeUnits, d as formatNumber } from "./format-CMT-x-V3.js";
import { u as useStyles$1 } from "./styles-BS0QIA50.js";
import { c as createStyles, b as Box, F as Flex, T as Text, K as Divider, a8 as Pagination, V as LoadingOverlay } from "./vendor-mantine-h0WlM2Mj.js";
import { C as CSVLinkComponent } from "./index-C8AOGyjn.js";
import { f as formatDate } from "./date-bOIw2uwV.js";
import { N as NoDataText } from "./index-C_Fw_IUU.js";
import "./styles-BC5VOxAU.js";
import "./index-Zaude47i.js";
import "./vendor-icons-xWhXK_Ui.js";
import "./index-lR9_AURi.js";
import "./index-_NgMYV3i.js";
import "./index-C6QQOvbB.js";
import "./index.es-BTZFX8B-.js";
import "./index-CcqTjOWY.js";
import "./useDebounce-DTubzIUC.js";
const useGetDiskSpaceFilters = () => {
  const $ = compilerRuntimeExports.c(10);
  const {
    t
  } = useTranslation();
  const {
    data: defaultFiltersData,
    isSuccess: defaultFiltersSuccess
  } = useGetDefaultUserFiltersQuery(10);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = [];
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const [filtersMenuList, setFiltersMenuList] = reactExports.useState(t0);
  let t1;
  if ($[1] !== defaultFiltersData || $[2] !== defaultFiltersSuccess || $[3] !== t) {
    t1 = () => {
      if (!(defaultFiltersData && defaultFiltersSuccess)) {
        return;
      }
      const diskSpaceFilter = defaultFiltersData.filter(_temp$1);
      if (!diskSpaceFilter) {
        return;
      }
      const convertDefaultFilters = diskSpaceFilter.map((item) => {
        var _a;
        return {
          ...item,
          label: item.Type in IFiltersEnumType ? t(`filters.${filtersLabelsKeys[item.Type]}`) : t("statistics.filial"),
          Items: (_a = item.Items) == null ? void 0 : _a.map(_temp2$1),
          hideSearch: false
        };
      }).sort(_temp3$1);
      setFiltersMenuList(convertDefaultFilters);
    };
    $[1] = defaultFiltersData;
    $[2] = defaultFiltersSuccess;
    $[3] = t;
    $[4] = t1;
  } else {
    t1 = $[4];
  }
  let t2;
  if ($[5] !== defaultFiltersData || $[6] !== defaultFiltersSuccess) {
    t2 = [defaultFiltersData, defaultFiltersSuccess];
    $[5] = defaultFiltersData;
    $[6] = defaultFiltersSuccess;
    $[7] = t2;
  } else {
    t2 = $[7];
  }
  reactExports.useEffect(t1, t2);
  let t3;
  if ($[8] !== filtersMenuList) {
    t3 = {
      filtersMenuList
    };
    $[8] = filtersMenuList;
    $[9] = t3;
  } else {
    t3 = $[9];
  }
  return t3;
};
function _temp$1(el) {
  return el.Type === IFiltersEnumType.FilialFilter;
}
function _temp2$1(filterItem) {
  return {
    ...filterItem,
    checked: false
  };
}
function _temp3$1(a, b) {
  return a.Type - b.Type;
}
const useGetColumns = () => {
  const $ = compilerRuntimeExports.c(26);
  const {
    t
  } = useTranslation();
  let t0;
  if ($[0] !== t) {
    t0 = t("statistics.user");
    $[0] = t;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  let t1;
  if ($[2] !== t) {
    t1 = (item) => (item == null ? void 0 : item.Name) !== null ? item == null ? void 0 : item.Name : t("statistics.total");
    $[2] = t;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[4] !== t0 || $[5] !== t1) {
    t2 = {
      width: "auto",
      dataIndex: "Name",
      title: t0,
      hasSort: true,
      render: t1
    };
    $[4] = t0;
    $[5] = t1;
    $[6] = t2;
  } else {
    t2 = $[6];
  }
  let t3;
  if ($[7] !== t) {
    t3 = t("statistics.space_used");
    $[7] = t;
    $[8] = t3;
  } else {
    t3 = $[8];
  }
  let t4;
  if ($[9] !== t3) {
    t4 = {
      width: "auto",
      dataIndex: "Space",
      title: t3,
      hasSort: true,
      render: _temp
    };
    $[9] = t3;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  let t5;
  if ($[11] !== t) {
    t5 = t("statistics.documents_count");
    $[11] = t;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  let t6;
  if ($[13] !== t5) {
    t6 = {
      width: "auto",
      dataIndex: "DocsCount",
      title: t5,
      hasSort: true,
      render: _temp2
    };
    $[13] = t5;
    $[14] = t6;
  } else {
    t6 = $[14];
  }
  let t7;
  if ($[15] !== t) {
    t7 = t("statistics.files_count");
    $[15] = t;
    $[16] = t7;
  } else {
    t7 = $[16];
  }
  let t8;
  if ($[17] !== t7) {
    t8 = {
      width: "auto",
      dataIndex: "FilesCount",
      title: t7,
      hasSort: true,
      render: _temp3
    };
    $[17] = t7;
    $[18] = t8;
  } else {
    t8 = $[18];
  }
  let t9;
  if ($[19] !== t2 || $[20] !== t4 || $[21] !== t6 || $[22] !== t8) {
    t9 = [t2, t4, t6, t8];
    $[19] = t2;
    $[20] = t4;
    $[21] = t6;
    $[22] = t8;
    $[23] = t9;
  } else {
    t9 = $[23];
  }
  const columnsData = t9;
  const [columns, setColumns] = reactExports.useState(columnsData);
  let t10;
  if ($[24] !== columns) {
    t10 = {
      columns,
      setColumns
    };
    $[24] = columns;
    $[25] = t10;
  } else {
    t10 = $[25];
  }
  return t10;
};
function _temp(item_0) {
  return formatDataSizeUnits(item_0 == null ? void 0 : item_0.Space);
}
function _temp2(item_1) {
  return formatNumber(item_1 == null ? void 0 : item_1.DocsCount);
}
function _temp3(item_2) {
  return formatNumber(item_2 == null ? void 0 : item_2.FilesCount);
}
const useStyles = createStyles((theme, _, utils) => ({
  paginateWrapper: {
    overflow: "auto"
  },
  wrapper: {
    flexGrow: 1,
    overflow: "auto",
    [utils.smallerThan("md")]: {
      marginRight: "0"
    }
  },
  container: {
    position: "relative",
    backgroundColor: theme.white,
    borderRadius: "8px",
    overflowX: "hidden",
    [utils.smallerThan("md")]: {
      padding: "10px",
      marginTop: "10px"
    },
    "@media (max-width: 992px)": {
      marginRight: "16px"
    },
    "& table": {
      "& tbody": {
        "& tr:first-of-type": {
          fontWeight: "bold",
          backgroundColor: "#F2F4F7"
        }
      },
      "& thead": {
        "& th .mantine-Group-root": {
          gap: "11px!important"
        }
      }
    }
  },
  divider: {
    borderColor: theme.colors.backgroundGray[2]
  },
  listWrapper: {
    display: "flex",
    justifyContent: "space-between",
    width: "100%",
    overflowX: "auto",
    msOverflowStyle: "none",
    scrollbarWidth: "none",
    backgroundColor: "white"
  }
}));
const convertToFilterConditions = (activeFilters) => {
  const filterObjects = [];
  for (const [key, value] of Object.entries(activeFilters)) {
    const filterType = +key;
    const filterObject = {
      Type: filterType
    };
    if (filterType === IFiltersEnumType.DateOfCreateFilter) {
      const [from, to] = value;
      filterObject.DateRange = {
        From: from == null ? void 0 : from.toISOString(),
        To: to.toISOString()
      };
    } else {
      filterObject.NumValues = value;
    }
    filterObjects.push(filterObject);
  }
  return filterObjects;
};
const transformNumberToSortField = (value) => {
  var _a;
  return value in IDiskSpaceSortField ? IDiskSpaceSortField[value] : IDiskSpaceSortField[(_a = IDiskSpaceSortField) == null ? void 0 : _a.None];
};
const transformSortFieldToNumber = (value) => {
  return getValueByKeyForNumberEnum(value);
};
function getValueByKeyForNumberEnum(value) {
  var _a;
  const valueFromKey = (_a = Object.entries(IDiskSpaceSortField).find(([key]) => key === value)) == null ? void 0 : _a[1];
  return typeof valueFromKey === "number" && valueFromKey in IDiskSpaceSortField ? valueFromKey : IDiskSpaceSortField.None;
}
const pageSizeOptions = listSizes.map((page) => ({
  value: page.toString(),
  label: page.toString()
}));
const DiskSpace = () => {
  const {
    t
  } = useTranslation();
  const {
    classes: commonClasses
  } = useStyles$1();
  const {
    classes
  } = useStyles();
  const {
    Total,
    TotalCount,
    filters: actionsFilters,
    Items: actionsDiskSpace
  } = useAppSelector((state) => state.statistics.diskSpace);
  const initialActiveFilters = actionsFilters.FilterConditions ? transformConditionals(actionsFilters.FilterConditions) : {};
  const [activeFilters, setActiveFilters] = reactExports.useState(initialActiveFilters);
  const [rowData, setRowData] = reactExports.useState(actionsDiskSpace);
  const [pageSize, setPageSize] = reactExports.useState(actionsFilters.Count || listSizes[0]);
  const [activePage, setActivePage] = reactExports.useState(1);
  const {
    columns
  } = useGetColumns();
  const scrollRef = reactExports.useRef(null);
  const {
    setDiskSpace,
    setDiskSpaceFilters
  } = useActions();
  const {
    filtersMenuList
  } = useGetDiskSpaceFilters();
  const [sortParams, setSortParams] = reactExports.useState({
    SortOrder: actionsFilters.SortOrder,
    SortField: actionsFilters.SortField
  });
  const {
    data,
    error,
    isLoading,
    isSuccess
  } = useGetDiskSpaceQuery({
    FilterConditions: actionsFilters.FilterConditions ?? [],
    Query: actionsFilters.Query,
    Count: pageSize,
    Offset: actionsFilters.Offset,
    SortOrder: actionsFilters.SortOrder,
    SortField: actionsFilters.SortField
  });
  const fileName = `disk-space-${formatDate(/* @__PURE__ */ new Date())}`;
  const [getDiskSpace, {
    data: csvData,
    isLoading: csvIsLoading
  }] = useLazyGetDiskSpaceQuery();
  const formatCsvData = (csvData_0) => {
    return csvData_0 == null ? void 0 : csvData_0.map((element) => {
      const formatedSpace = formatDataSizeUnits(element.Space);
      return {
        ...element,
        Space: formatedSpace
      };
    });
  };
  reactExports.useEffect(() => {
    if (data) {
      setDiskSpace(data);
    }
  }, [data]);
  const handleFilterChange = (updatedFilters) => {
    var _a, _b;
    setActiveFilters(updatedFilters);
    (_b = (_a = scrollRef == null ? void 0 : scrollRef.current) == null ? void 0 : _a.el) == null ? void 0 : _b.scrollTo(0, 0);
  };
  const defaultTotalValue = {
    Name: "",
    Space: 0,
    DocsCount: 0,
    FilesCount: 0
  };
  const finalRowData = [Total || defaultTotalValue].concat(rowData);
  const handleResetFilters = () => {
    setPageSize(listSizes[0]);
    setActivePage(1);
    setDiskSpaceFilters(initialStatisticFilter);
  };
  reactExports.useEffect(() => {
    return () => {
      handleResetFilters();
    };
  }, []);
  reactExports.useEffect(() => {
    setDiskSpaceFilters({
      ...actionsFilters,
      FilterConditions: convertToFilterConditions(activeFilters),
      Offset: 0
    });
    setActivePage(1);
  }, [activeFilters]);
  reactExports.useEffect(() => {
    setRowData(actionsDiskSpace ?? []);
  }, [actionsDiskSpace]);
  reactExports.useEffect(() => {
    setDiskSpaceFilters({
      ...actionsFilters,
      Offset: 0,
      SortField: sortParams == null ? void 0 : sortParams.SortField,
      SortOrder: sortParams == null ? void 0 : sortParams.SortOrder
    });
    setActivePage(1);
  }, [sortParams]);
  const handleChangePageSize = (value) => {
    setPageSize(value);
    setActivePage(1);
    setDiskSpaceFilters({
      ...actionsFilters,
      Offset: 0,
      Count: value
    });
  };
  const handleChangeActivePage = (value_0) => {
    setActivePage(value_0);
    setDiskSpaceFilters({
      ...actionsFilters,
      Offset: (value_0 >= 1 ? value_0 - 1 : 0) * pageSize
    });
  };
  const handleSortTable = (payload) => {
    setSortParams({
      SortOrder: payload.SortOrder === true ? 1 : 0,
      SortField: transformSortFieldToNumber(payload.SortField)
    });
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.wrapper, mr: "md", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { direction: "column", className: classes.container, my: "md", px: "md", py: "md", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { c: "#101828", fw: "600", children: t("statistics.disk_space") }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.listWrapper, children: /* @__PURE__ */ jsxRuntimeExports.jsx(FilterList, { filtersMenuList, activeFilters, onFilterChange: handleFilterChange, onFilterReset: setActiveFilters, onLoadData: useGetFilterListItemsQuery }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Divider, { className: classes.divider, mx: "-md", mb: "sm" }),
    finalRowData.length && TotalCount ? /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TableHeader, { handleChangePageSize, pageSize, pageSizeOptions, isLoading, startOffset: actionsFilters.Offset + 1, endOffset: Math.min(actionsFilters.Offset + pageSize, TotalCount), totalCount: TotalCount, showButton: true, buttonComponent: /* @__PURE__ */ jsxRuntimeExports.jsx(CSVLinkComponent, { text: t("statistics.export"), filename: fileName, columns, csvData: formatCsvData(csvData == null ? void 0 : csvData.Items), actionsFilters: {
        ...actionsFilters,
        Offset: 0,
        Count: TotalCount
      }, getCsvData: getDiskSpace, isLoading: csvIsLoading }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { mt: "lg", children: /* @__PURE__ */ jsxRuntimeExports.jsx(PaginatedTable, { allDataLength: TotalCount, rowData: finalRowData, columns, height: "100%", width: "100%", scrollRef, error, isLoading, pinnedRows: [0], sort: {
        SortOrder: sortParams.SortOrder !== 0,
        SortField: transformNumberToSortField(sortParams.SortField)
      }, setSort: handleSortTable }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.paginateWrapper, children: TotalCount > pageSize && /* @__PURE__ */ jsxRuntimeExports.jsx(Pagination, { classNames: {
        root: commonClasses.paginateRoot,
        control: commonClasses.paginateControl
      }, value: activePage, onChange: handleChangeActivePage, total: Math.ceil(TotalCount / pageSize), withControls: false, withEdges: true, size: "sm" }) })
    ] }) : isSuccess && !isLoading && !TotalCount && /* @__PURE__ */ jsxRuntimeExports.jsx(Flex, { align: "center", justify: "center", h: "350px", children: /* @__PURE__ */ jsxRuntimeExports.jsx(NoDataText, {}) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading })
  ] }) });
};
export {
  DiskSpace as default
};
//# sourceMappingURL=index-Ds6tAxUN.js.map
