import { r as reactExports, T as useNavigate, a0 as useParams, j as jsxRuntimeExports, E as compilerRuntimeExports, V as Outlet } from "./vendor-react-DYNCg6N5.js";
import { b as useActions, c as useAppSelector, R as ROUTES, F as CompanyPlaceholder, i as isSaaS, y as SearchIcon, G as useGetFilialsTreeQuery, E as ErrorBoundary, L as Layout } from "./index-DfebxkL9.js";
import { S as SimpleTable } from "./index-DOqL4InQ.js";
import { P as Panels, A as Action } from "./index-DnphFLKH.js";
import { c as createStyles, X as Drawer, b as Box, A as Avatar, T as Text, n as useDisclosure, W as useMediaQuery, B as Button } from "./vendor-mantine-h0WlM2Mj.js";
import { f as formatDate, h as hooks } from "./date-bOIw2uwV.js";
import { O as OverflowTip } from "./index-C-CFogWu.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { u as useStyles$2 } from "./styles-Dnu4P1Zm.js";
import { X as XMe } from "./vendor-icons-xWhXK_Ui.js";
import { M as Modal } from "./index-DqcA8P_y.js";
import { C as CreateFilialForm } from "./createForm-CM9BvDgp.js";
import { S as Search } from "./index-CcqTjOWY.js";
import { u as useDebounce } from "./useDebounce-DTubzIUC.js";
import "./index.es-BTZFX8B-.js";
import "./styles-BC5VOxAU.js";
import "./index-Zaude47i.js";
import "./index-D4BAIihc.js";
import "./useCopyToClipboard-C-S2RacD.js";
import "./index-CslanKI5.js";
import "./styles-3NoIlzov.js";
import "./index-CfN7Ll0p.js";
import "./errorIcon-PZO0f1IY.js";
import "./validation-Ds00Zghx.js";
import "./validate-CA6bwF22.js";
import "./types-espwwxTQ.js";
const useStyles$1 = createStyles((theme) => ({
  root: {
    "& .mantine-Drawer-body": {
      height: "100%",
      padding: theme.spacing.sm,
      overflow: "auto"
    },
    "& .mantine-Paper-root": {
      width: "384px"
    }
  }
}));
const FilialsInfoMain = ({
  setRowData
}) => {
  const {
    classes
  } = useStyles$1();
  const [isOpened, setIsOpened] = reactExports.useState(false);
  const {
    setSelectedFilialsId
  } = useActions();
  const {
    selectedFilialsId
  } = useAppSelector((state) => state.filials);
  const navigate = useNavigate();
  const {
    id
  } = useParams();
  const toNumberId = reactExports.useMemo(() => Number(id), [id]);
  reactExports.useEffect(() => {
    if (toNumberId) {
      setSelectedFilialsId(Number(toNumberId));
      setIsOpened(true);
    } else {
      handleClose();
    }
  }, [toNumberId]);
  const handleClose = reactExports.useCallback(() => {
    setIsOpened(false);
    setSelectedFilialsId(0);
    navigate(ROUTES.filials.path);
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Drawer, { "data-cy": "filialsInfoDrawer", zIndex: 1002, opened: isOpened, trapFocus: false, onClose: handleClose, position: "right", withCloseButton: false, className: classes.root, lockScroll: true, children: Boolean(selectedFilialsId) && /* @__PURE__ */ jsxRuntimeExports.jsx(Panels, { selectedFilialsId, setRowData, handleClose }) });
};
const useStyles = createStyles((theme) => ({
  title: {
    height: "100%",
    width: "100%",
    display: "flex",
    alignItems: "center",
    flexDirection: "row",
    margin: 0,
    padding: 0
  },
  parentWrapper: {
    height: "100%",
    width: "200px",
    display: "flex",
    alignItems: "center",
    flexDirection: "row",
    margin: 0,
    padding: 0
  },
  content: {
    height: "100%",
    width: "100%",
    display: "flex",
    alignItems: "flex-start",
    flexDirection: "column",
    marginLeft: theme.spacing.sm
  }
}));
const ItemTitle = (t0) => {
  const $ = compilerRuntimeExports.c(14);
  const {
    item
  } = t0;
  const {
    classes
  } = useStyles();
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = {
      zIndex: 1
    };
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  let t2;
  if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = /* @__PURE__ */ jsxRuntimeExports.jsx(Avatar, { sx: t1, color: "blue", size: 40, radius: 40, children: /* @__PURE__ */ jsxRuntimeExports.jsx(CompanyPlaceholder, { size: 24 }) });
    $[1] = t2;
  } else {
    t2 = $[1];
  }
  let t3;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = {
      maxWidth: "80%"
    };
    $[2] = t3;
  } else {
    t3 = $[2];
  }
  const t4 = item == null ? void 0 : item.Title;
  let t5;
  if ($[3] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(OverflowTip, { sx: t3, size: "sm", fw: 600, children: t4 });
    $[3] = t4;
    $[4] = t5;
  } else {
    t5 = $[4];
  }
  const t6 = item == null ? void 0 : item.Id;
  let t7;
  if ($[5] !== t6) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Text, { c: "dimmed", size: "xs", children: [
      "ID: ",
      t6
    ] });
    $[5] = t6;
    $[6] = t7;
  } else {
    t7 = $[6];
  }
  let t8;
  if ($[7] !== classes.content || $[8] !== t5 || $[9] !== t7) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.content, children: [
      t5,
      t7
    ] });
    $[7] = classes.content;
    $[8] = t5;
    $[9] = t7;
    $[10] = t8;
  } else {
    t8 = $[10];
  }
  let t9;
  if ($[11] !== classes.title || $[12] !== t8) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.title, children: [
      t2,
      t8
    ] });
    $[11] = classes.title;
    $[12] = t8;
    $[13] = t9;
  } else {
    t9 = $[13];
  }
  return t9;
};
const ParentFilial = (t0) => {
  const $ = compilerRuntimeExports.c(9);
  const {
    item
  } = t0;
  const {
    classes
  } = useStyles();
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = {
      maxWidth: "80%"
    };
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const t2 = item == null ? void 0 : item.ParentName;
  let t3;
  if ($[1] !== t2) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(OverflowTip, { sx: t1, size: "sm", children: t2 });
    $[1] = t2;
    $[2] = t3;
  } else {
    t3 = $[2];
  }
  let t4;
  if ($[3] !== classes.content || $[4] !== t3) {
    t4 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.content, children: t3 });
    $[3] = classes.content;
    $[4] = t3;
    $[5] = t4;
  } else {
    t4 = $[5];
  }
  let t5;
  if ($[6] !== classes.parentWrapper || $[7] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.parentWrapper, children: t4 });
    $[6] = classes.parentWrapper;
    $[7] = t4;
    $[8] = t5;
  } else {
    t5 = $[8];
  }
  return t5;
};
const useGetColumns = () => {
  const $ = compilerRuntimeExports.c(37);
  const {
    t
  } = useTranslation();
  const {
    Settings
  } = useAppSelector(_temp$1);
  let t0;
  if ($[0] !== t) {
    t0 = t("servers.name");
    $[0] = t;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  let t1;
  if ($[2] !== t0) {
    t1 = {
      width: "300px",
      dataIndex: "Title",
      title: t0,
      hideColumn: false,
      render: _temp2
    };
    $[2] = t0;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[4] !== t) {
    t2 = t("filials.settings.filialCode");
    $[4] = t;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  let t3;
  if ($[6] !== t2) {
    t3 = {
      width: "120px",
      dataIndex: "Shortname",
      title: t2,
      hideColumn: false,
      render: _temp3
    };
    $[6] = t2;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  let t4;
  if ($[8] !== t) {
    t4 = t("filials.settings.filialParent");
    $[8] = t;
    $[9] = t4;
  } else {
    t4 = $[9];
  }
  let t5;
  if ($[10] !== t4) {
    t5 = {
      width: "246px",
      dataIndex: "Company",
      title: t4,
      hideColumn: false,
      render: _temp4
    };
    $[10] = t4;
    $[11] = t5;
  } else {
    t5 = $[11];
  }
  let t6;
  if ($[12] !== t) {
    t6 = t("filials.diskSpace.table.users");
    $[12] = t;
    $[13] = t6;
  } else {
    t6 = $[13];
  }
  let t7;
  if ($[14] !== t6) {
    t7 = {
      width: "146px",
      dataIndex: "UsersCount",
      title: t6,
      hideColumn: false,
      render: _temp5
    };
    $[14] = t6;
    $[15] = t7;
  } else {
    t7 = $[15];
  }
  let t8;
  if ($[16] !== t) {
    t8 = t("profile.roles");
    $[16] = t;
    $[17] = t8;
  } else {
    t8 = $[17];
  }
  let t9;
  if ($[18] !== t8) {
    t9 = {
      width: "146px",
      dataIndex: "Roles",
      title: t8,
      hideColumn: false,
      render: _temp6
    };
    $[18] = t8;
    $[19] = t9;
  } else {
    t9 = $[19];
  }
  let t10;
  if ($[20] !== t) {
    t10 = t("dateOfCreation");
    $[20] = t;
    $[21] = t10;
  } else {
    t10 = $[21];
  }
  let t11;
  if ($[22] !== t10) {
    t11 = {
      width: "146px",
      dataIndex: "DateOfCreate",
      title: t10,
      hideColumn: false,
      render: _temp7
    };
    $[22] = t10;
    $[23] = t11;
  } else {
    t11 = $[23];
  }
  let t12;
  if ($[24] === Symbol.for("react.memo_cache_sentinel")) {
    t12 = {
      width: "50px",
      dataIndex: "Action",
      title: "",
      hideColumn: false,
      render: _temp8
    };
    $[24] = t12;
  } else {
    t12 = $[24];
  }
  let t13;
  if ($[25] !== t1 || $[26] !== t11 || $[27] !== t3 || $[28] !== t5 || $[29] !== t7 || $[30] !== t9) {
    t13 = [t1, t3, t5, t7, t9, t11, t12];
    $[25] = t1;
    $[26] = t11;
    $[27] = t3;
    $[28] = t5;
    $[29] = t7;
    $[30] = t9;
    $[31] = t13;
  } else {
    t13 = $[31];
  }
  let t14;
  if ($[32] !== Settings || $[33] !== t13) {
    t14 = t13.filter((item_6) => !isSaaS(Settings) || !["Shortname", "Company"].includes(item_6.dataIndex));
    $[32] = Settings;
    $[33] = t13;
    $[34] = t14;
  } else {
    t14 = $[34];
  }
  const columnsData = t14;
  const [columns, setColumns] = reactExports.useState(columnsData);
  let t15;
  if ($[35] !== columns) {
    t15 = {
      columns,
      setColumns
    };
    $[35] = columns;
    $[36] = t15;
  } else {
    t15 = $[36];
  }
  return t15;
};
function _temp$1(state) {
  return state.userInfo;
}
function _temp2(item) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(ItemTitle, { item });
}
function _temp3(item_0) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { maw: "200px", children: /* @__PURE__ */ jsxRuntimeExports.jsx(OverflowTip, { size: "sm", children: item_0 == null ? void 0 : item_0.Company }) });
}
function _temp4(item_1) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(ParentFilial, { item: item_1 });
}
function _temp5(item_2) {
  return item_2 == null ? void 0 : item_2.UsersCount;
}
function _temp6(item_3) {
  return item_3 == null ? void 0 : item_3.RolesCount;
}
function _temp7(item_4) {
  return formatDate(hooks.utc(item_4 == null ? void 0 : item_4.DateOfCreate));
}
function _temp8(item_5) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Action, { item: item_5 });
}
const CreateFilial = () => {
  const $ = compilerRuntimeExports.c(25);
  const [opened, t0] = useDisclosure(false);
  const {
    open,
    close
  } = t0;
  const {
    classes
  } = useStyles$2();
  const {
    t
  } = useTranslation();
  const isMobile = useMediaQuery("(max-width: 1200px)");
  let t1;
  if ($[0] !== close) {
    t1 = () => () => {
      close();
    };
    $[0] = close;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  let t2;
  if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = [];
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  reactExports.useEffect(t1, t2);
  let t3;
  if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(XMe, {});
    $[3] = t3;
  } else {
    t3 = $[3];
  }
  let t4;
  if ($[4] !== isMobile || $[5] !== t) {
    t4 = isMobile ? "" : t("create");
    $[4] = isMobile;
    $[5] = t;
    $[6] = t4;
  } else {
    t4 = $[6];
  }
  let t5;
  if ($[7] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { fw: "600", children: t4 });
    $[7] = t4;
    $[8] = t5;
  } else {
    t5 = $[8];
  }
  let t6;
  if ($[9] !== classes.createButton || $[10] !== open || $[11] !== t5) {
    t6 = /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { size: "md", "data-cy": "createFilial", leftSection: t3, className: classes.createButton, onClick: open, children: t5 });
    $[9] = classes.createButton;
    $[10] = open;
    $[11] = t5;
    $[12] = t6;
  } else {
    t6 = $[12];
  }
  let t7;
  if ($[13] !== t) {
    t7 = t("filials.create");
    $[13] = t;
    $[14] = t7;
  } else {
    t7 = $[14];
  }
  let t8;
  if ($[15] !== close) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsx(CreateFilialForm, { onClose: close });
    $[15] = close;
    $[16] = t8;
  } else {
    t8 = $[16];
  }
  let t9;
  if ($[17] !== close || $[18] !== opened || $[19] !== t7 || $[20] !== t8) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsx(Modal, { opened, onClose: close, title: t7, zIndex: 1002, children: t8 });
    $[17] = close;
    $[18] = opened;
    $[19] = t7;
    $[20] = t8;
    $[21] = t9;
  } else {
    t9 = $[21];
  }
  let t10;
  if ($[22] !== t6 || $[23] !== t9) {
    t10 = /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      t6,
      t9
    ] });
    $[22] = t6;
    $[23] = t9;
    $[24] = t10;
  } else {
    t10 = $[24];
  }
  return t10;
};
const FilialSearch = () => {
  const $ = compilerRuntimeExports.c(13);
  const {
    classes
  } = useStyles$2();
  const {
    t
  } = useTranslation();
  const {
    setFilialsTableFilters
  } = useActions();
  const {
    filters: filialsFilter
  } = useAppSelector(_temp);
  const [text, setText] = reactExports.useState((filialsFilter == null ? void 0 : filialsFilter.Query) || "");
  const debounced = useDebounce(text, 500);
  let t0;
  if ($[0] !== debounced || $[1] !== filialsFilter || $[2] !== setFilialsTableFilters) {
    t0 = () => {
      setFilialsTableFilters({
        ...filialsFilter,
        Offset: 0,
        Query: debounced
      });
    };
    $[0] = debounced;
    $[1] = filialsFilter;
    $[2] = setFilialsTableFilters;
    $[3] = t0;
  } else {
    t0 = $[3];
  }
  let t1;
  if ($[4] !== debounced) {
    t1 = [debounced];
    $[4] = debounced;
    $[5] = t1;
  } else {
    t1 = $[5];
  }
  reactExports.useEffect(t0, t1);
  let t2;
  if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
    t2 = /* @__PURE__ */ jsxRuntimeExports.jsx(SearchIcon, {});
    $[6] = t2;
  } else {
    t2 = $[6];
  }
  const t3 = classes.search;
  let t4;
  if ($[7] !== t) {
    t4 = t("filials.search");
    $[7] = t;
    $[8] = t4;
  } else {
    t4 = $[8];
  }
  let t5;
  if ($[9] !== classes.search || $[10] !== t4 || $[11] !== text) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(Search, { icon: t2, "data-cy": "searchFilials", setSearch: setText, className: t3, fullSize: true, value: text, placeholder: t4 });
    $[9] = classes.search;
    $[10] = t4;
    $[11] = text;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  return t5;
};
function _temp(state) {
  return state.filials;
}
const Filials = () => {
  const {
    classes
  } = useStyles$2();
  const navigate = useNavigate();
  const {
    setFilials,
    setFilialsTableFilters
    /*, setSelectedFilialsIds, setSelectedFilialsId */
  } = useActions();
  const {
    filials,
    total,
    filters: filialsFilter,
    selected: selectedFilials
  } = useAppSelector((state) => state.filials);
  const {
    Settings
  } = useAppSelector((state_0) => state_0.userInfo);
  const scrollRef = reactExports.useRef(null);
  const [rowData, setRowData] = reactExports.useState(filials);
  const {
    data,
    error,
    isLoading,
    isFetching
  } = useGetFilialsTreeQuery({
    Count: 20,
    Offset: filialsFilter.Offset,
    Query: filialsFilter.Query
  });
  const {
    columns
  } = useGetColumns();
  const handleRowClick = reactExports.useCallback((id) => {
    navigate(`${ROUTES.filials.path}/${id}`);
  }, []);
  const resetFilialsFilters = () => {
    setFilialsTableFilters({
      ...filialsFilter,
      Offset: 0,
      Query: ""
    });
  };
  const handleLoadMore = () => {
    const newOffset = filialsFilter.Offset + 20;
    if (newOffset < total) {
      setFilialsTableFilters({
        ...filialsFilter,
        Offset: newOffset
      });
    }
  };
  reactExports.useEffect(() => {
    if (data) {
      const {
        Offset: offset
      } = filialsFilter;
      if (!offset) {
        setFilials(data);
      } else {
        const filialsData = filials.slice(0, offset).concat(data.Items);
        setFilials({
          ...data,
          Items: filialsData
        });
      }
    }
  }, [data]);
  reactExports.useEffect(() => {
    setRowData(filials || []);
  }, [filials]);
  reactExports.useEffect(() => {
    return () => {
      resetFilialsFilters();
    };
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(ErrorBoundary, { componentTitle: "Filials page", children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Layout, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.container, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.searchWrapper, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(FilialSearch, {}),
        !isSaaS(Settings) ? /* @__PURE__ */ jsxRuntimeExports.jsx(CreateFilial, {}) : null
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(
        SimpleTable,
        {
          dataCy: "filialsTable",
          allDataLength: total,
          rowData,
          setRowData,
          columns,
          height: "calc(100vh - 200px)",
          minWidth: 700,
          width: "calc(100vw - 48px)",
          selection: selectedFilials,
          handleRowClick,
          getNextData: handleLoadMore,
          error,
          scrollRef,
          isLoading,
          isFetching,
          isExpanded: true
        }
      )
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Outlet, {}),
    /* @__PURE__ */ jsxRuntimeExports.jsx(FilialsInfoMain, { setRowData })
  ] }) });
};
export {
  Filials
};
//# sourceMappingURL=index-DYQg5Zv2.js.map
