import { E as compilerRuntimeExports, r as reactExports, j as jsxRuntimeExports, T as useNavigate, L as Link } from "./vendor-react-DYNCg6N5.js";
import { a as formatDateTime, h as hooks } from "./date-bOIw2uwV.js";
import { T as TextField } from "./index-D4BAIihc.js";
import { T as TextFieldTags } from "./index-CslanKI5.js";
import { c as useAppSelector, i as isSaaS, b as useActions, cc as useDeleteFilialsMutation, m as universalNotification, a6 as clearApiCacheByTags, cd as filialsApi, a7 as rolesApi, bk as EditProfileIcon, R as ROUTES, aV as EditIcon, F as CompanyPlaceholder, aQ as useGetFilialsInfoCommonQuery } from "./index-DfebxkL9.js";
import { a2 as _Z, v as zKe, a1 as $n } from "./vendor-icons-xWhXK_Ui.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { u as useStyles$3 } from "./styles-Dnu4P1Zm.js";
import { M as Modal } from "./index-DqcA8P_y.js";
import { c as createStyles, b as Box, n as useDisclosure, az as useInputState, F as Flex, h as ActionIcon, T as Text, aA as Code, ae as Input, B as Button, U as UnstyledButton, A as Avatar, g as Loader, a6 as notifications } from "./vendor-mantine-h0WlM2Mj.js";
import { O as OverflowTip } from "./index-C-CFogWu.js";
const useStyles$2 = createStyles((theme) => ({
  content: {
    marginTop: theme.spacing.md,
    display: "flex",
    alignItems: "flex-start",
    flexDirection: "column"
  }
}));
const BaseInfo = () => {
  var _a, _b, _c;
  const $ = compilerRuntimeExports.c(23);
  const {
    classes
  } = useStyles$2();
  const {
    t
  } = useTranslation();
  const {
    filialsInfoCommon
  } = reactExports.useContext(FilialsInfoMainContext);
  const {
    Settings
  } = useAppSelector(_temp);
  const t0 = classes.content;
  let t1;
  if ($[0] !== Settings || $[1] !== (filialsInfoCommon == null ? void 0 : filialsInfoCommon.Children) || $[2] !== (filialsInfoCommon == null ? void 0 : filialsInfoCommon.Company) || $[3] !== ((_a = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Parent) == null ? void 0 : _a.Name) || $[4] !== t) {
    t1 = isSaaS(Settings) ? null : /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextField, { title: t("filials.settings.filialCode"), description: filialsInfoCommon == null ? void 0 : filialsInfoCommon.Company }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextField, { title: t("filials.settings.filialParent"), description: (_b = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Parent) == null ? void 0 : _b.Name }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldTags, { title: t("filials.settings.filialKids"), tags: filialsInfoCommon == null ? void 0 : filialsInfoCommon.Children })
    ] });
    $[0] = Settings;
    $[1] = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Children;
    $[2] = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Company;
    $[3] = (_c = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Parent) == null ? void 0 : _c.Name;
    $[4] = t;
    $[5] = t1;
  } else {
    t1 = $[5];
  }
  let t2;
  if ($[6] !== t) {
    t2 = t("profile.roles");
    $[6] = t;
    $[7] = t2;
  } else {
    t2 = $[7];
  }
  const t3 = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Roles;
  let t4;
  if ($[8] !== t2 || $[9] !== t3) {
    t4 = /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldTags, { title: t2, tags: t3 });
    $[8] = t2;
    $[9] = t3;
    $[10] = t4;
  } else {
    t4 = $[10];
  }
  let t5;
  if ($[11] !== t) {
    t5 = t("dateOfCreation");
    $[11] = t;
    $[12] = t5;
  } else {
    t5 = $[12];
  }
  let t6;
  if ($[13] !== (filialsInfoCommon == null ? void 0 : filialsInfoCommon.DateOfCreate)) {
    t6 = formatDateTime(hooks.utc(filialsInfoCommon == null ? void 0 : filialsInfoCommon.DateOfCreate));
    $[13] = filialsInfoCommon == null ? void 0 : filialsInfoCommon.DateOfCreate;
    $[14] = t6;
  } else {
    t6 = $[14];
  }
  let t7;
  if ($[15] !== t5 || $[16] !== t6) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsx(TextField, { title: t5, description: t6 });
    $[15] = t5;
    $[16] = t6;
    $[17] = t7;
  } else {
    t7 = $[17];
  }
  let t8;
  if ($[18] !== classes.content || $[19] !== t1 || $[20] !== t4 || $[21] !== t7) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: t0, children: [
      t1,
      t4,
      t7
    ] });
    $[18] = classes.content;
    $[19] = t1;
    $[20] = t4;
    $[21] = t7;
    $[22] = t8;
  } else {
    t8 = $[22];
  }
  return t8;
};
function _temp(state) {
  return state.userInfo;
}
const useDeleteFilial = () => {
  const {
    t
  } = useTranslation();
  const {
    setFilialsTableFilters
  } = useActions();
  const {
    filters
  } = useAppSelector((state) => state.filials);
  const [deleteFilialMutation] = useDeleteFilialsMutation();
  const deleteFilial = reactExports.useCallback(async (id) => {
    try {
      const res = await deleteFilialMutation(id);
      if (!res.error) {
        setFilialsTableFilters({
          ...filters,
          Offset: 0
        });
        universalNotification({
          successTitle: t("filials.successDelete"),
          isSuccess: true,
          isError: false,
          error: null
        });
        clearApiCacheByTags(filialsApi, ["Filials"]);
        clearApiCacheByTags(rolesApi, ["Roles"]);
      } else {
        universalNotification({
          error: res == null ? void 0 : res.error,
          isError: true,
          isSuccess: false
        });
      }
    } catch (err) {
      console.warn(t("filials.errors.delete"), err);
    }
  }, [filters]);
  return {
    deleteFilial
  };
};
const Action = ({
  item,
  onDeleteSuccess
}) => {
  const {
    t
  } = useTranslation();
  const navigate = useNavigate();
  const [opened, {
    close,
    open
  }] = useDisclosure(false);
  const [value, setValue] = useInputState("");
  const {
    setSelectedFilialsId
  } = useActions();
  const {
    selectedFilialsId
  } = useAppSelector((state) => state.filials);
  const {
    Settings
  } = useAppSelector((state_0) => state_0.userInfo);
  const {
    deleteFilial
  } = useDeleteFilial();
  const {
    classes
  } = useStyles$3();
  const onClickDelete = reactExports.useCallback(async (event) => {
    event.stopPropagation();
    setSelectedFilialsId(0);
    if (item == null ? void 0 : item.Id) {
      await deleteFilial(item.Id);
      setValue("");
      close();
      onDeleteSuccess == null ? void 0 : onDeleteSuccess();
    }
  }, [item, deleteFilial, setSelectedFilialsId, close]);
  const handleCloseModal = (event_0) => {
    event_0 == null ? void 0 : event_0.preventDefault();
    event_0 == null ? void 0 : event_0.stopPropagation();
    setValue("");
    close();
  };
  const handleClickRemove = (event_1) => {
    event_1.preventDefault();
    event_1.stopPropagation();
    open();
  };
  const validate = () => value === (item == null ? void 0 : item.Title);
  const goEditFilial = (id, event_2) => {
    event_2.stopPropagation();
    navigate(`${ROUTES.filials.edit.clearPath}/${id}`);
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { gap: 4, justify: "flex-end", wrap: "nowrap", children: [
      !selectedFilialsId && /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { className: "action-icon", "data-action": "action", "data-cy": "filialSettingsBtn", onClick: (event_3) => goEditFilial((item == null ? void 0 : item.Id) ?? 0, event_3), children: /* @__PURE__ */ jsxRuntimeExports.jsx(EditProfileIcon, { size: 24 }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { className: "action-icon", "data-action": "action", "data-cy": "viewDetailsBtn", children: /* @__PURE__ */ jsxRuntimeExports.jsx(_Z, { size: "1.5rem", color: "#667085", stroke: 1.5, style: {
          pointerEvents: "none"
        } }) })
      ] }),
      !isSaaS(Settings) && /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { className: "action-icon", onClick: handleClickRemove, "data-action": "action", "data-cy": "deleteFilialBtn", children: /* @__PURE__ */ jsxRuntimeExports.jsx(zKe, { size: "1.5rem", color: "#667085", stroke: 1.5, style: {
        pointerEvents: "none"
      } }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Modal, { opened, onClose: handleCloseModal, title: t("warning"), zIndex: 1002, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalRoot, children: (item == null ? void 0 : item.Parent) === null || (item == null ? void 0 : item.ParentName) === null ? /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalContainer, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { children: t("filials.deleteFilial.rootFilial") }) }) : (item == null ? void 0 : item.UsersCount) ? /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalContainer, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { children: t("filials.deleteFilial.usersFilial") }) }) : /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.modalContainer, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { mb: "1rem", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs(Text, { mb: "1rem", fz: "md", children: [
            t("filials.deleteFilial.confirm"),
            " ",
            /* @__PURE__ */ jsxRuntimeExports.jsx("b", { className: classes.preserveSpaces, children: item == null ? void 0 : item.Title }),
            "?"
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs(Text, { fz: "md", children: [
            t("filials.deleteFilial.insert"),
            " ",
            /* @__PURE__ */ jsxRuntimeExports.jsx(Code, { color: "red", className: classes.preserveSpaces, children: item == null ? void 0 : item.Title })
          ] })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { placeholder: t("login.customerCode"), w: "100%", required: true, value, onChange: setValue })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.modalFooter, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { color: "red", w: "49%", onClick: onClickDelete, disabled: !validate(), children: t("delete") }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(UnstyledButton, { className: classes.modalCancelBtn, w: "49%", onClick: handleCloseModal, children: t("cancel") })
      ] })
    ] }) }) })
  ] });
};
const useStyles$1 = createStyles(() => ({
  action: {
    position: "absolute",
    top: "16px",
    right: "16px",
    display: "flex",
    flexDirection: "row"
  },
  backArrow: {
    display: "none",
    "@media (max-width: 574px)": {
      display: "flex"
    }
  }
}));
const DrawerHeader = () => {
  const {
    classes
  } = useStyles$1();
  const navigate = useNavigate();
  const {
    filialsInfoCommon
  } = reactExports.useContext(FilialsInfoMainContext);
  const {
    setSelectedFilialsId
  } = useActions();
  const goBack = reactExports.useCallback(() => {
    setSelectedFilialsId(0);
    navigate(ROUTES.filials.path);
  }, []);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { onClick: goBack, size: "md", className: classes.backArrow, children: /* @__PURE__ */ jsxRuntimeExports.jsx($n, { color: "#667085" }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.action, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Link, { to: `${ROUTES.filials.edit.clearPath}/${(filialsInfoCommon == null ? void 0 : filialsInfoCommon.Id) || ""}`, replace: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(ActionIcon, { size: "md", children: /* @__PURE__ */ jsxRuntimeExports.jsx(EditIcon, {}) }) }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Action, { item: filialsInfoCommon, onDeleteSuccess: goBack })
    ] })
  ] });
};
const useStyles = createStyles((theme) => ({
  photo: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "center"
  },
  container: {
    marginTop: theme.spacing.sm
  },
  name: {
    fontWeight: 600,
    textAlign: "center",
    lineHeight: "20px"
  },
  email: {
    color: theme.colors.brandGray[0],
    fontSize: "14px",
    textAlign: "center",
    lineHeight: "17px",
    marginTop: theme.spacing.xs
  },
  badge: {
    marginTop: theme.spacing.sm
  }
}));
const PhotoInfo = (t0) => {
  const $ = compilerRuntimeExports.c(14);
  const {
    filialsInfoCommon
  } = t0;
  const {
    classes
  } = useStyles();
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = /* @__PURE__ */ jsxRuntimeExports.jsx(Avatar, { color: "blue", size: 124, radius: 62, children: /* @__PURE__ */ jsxRuntimeExports.jsx(CompanyPlaceholder, { size: 60 }) });
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const t2 = (filialsInfoCommon == null ? void 0 : filialsInfoCommon.Title) || "";
  let t3;
  if ($[1] !== classes.name || $[2] !== t2) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { maw: "300px", children: /* @__PURE__ */ jsxRuntimeExports.jsx(OverflowTip, { className: classes.name, children: t2 }) });
    $[1] = classes.name;
    $[2] = t2;
    $[3] = t3;
  } else {
    t3 = $[3];
  }
  const t4 = filialsInfoCommon == null ? void 0 : filialsInfoCommon.Id;
  let t5;
  if ($[4] !== classes.email || $[5] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Text, { className: classes.email, children: [
      "ID:",
      t4
    ] });
    $[4] = classes.email;
    $[5] = t4;
    $[6] = t5;
  } else {
    t5 = $[6];
  }
  let t6;
  if ($[7] !== classes.container || $[8] !== t3 || $[9] !== t5) {
    t6 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.container, children: [
      t3,
      t5
    ] });
    $[7] = classes.container;
    $[8] = t3;
    $[9] = t5;
    $[10] = t6;
  } else {
    t6 = $[10];
  }
  let t7;
  if ($[11] !== classes.photo || $[12] !== t6) {
    t7 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Box, { className: classes.photo, children: [
      t1,
      t6
    ] });
    $[11] = classes.photo;
    $[12] = t6;
    $[13] = t7;
  } else {
    t7 = $[13];
  }
  return t7;
};
const FilialsInfoMainContext = reactExports.createContext({});
const Panels = (t0) => {
  const $ = compilerRuntimeExports.c(16);
  const {
    selectedFilialsId,
    setRowData,
    handleClose
  } = t0;
  let t1;
  if ($[0] !== selectedFilialsId) {
    t1 = {
      id: selectedFilialsId
    };
    $[0] = selectedFilialsId;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  const {
    data,
    isLoading,
    isError
  } = useGetFilialsInfoCommonQuery(t1);
  const {
    t
  } = useTranslation();
  let t2;
  if ($[2] !== data || $[3] !== selectedFilialsId || $[4] !== setRowData) {
    t2 = {
      selectedFilialsId,
      setRowData,
      filialsInfoCommon: data
    };
    $[2] = data;
    $[3] = selectedFilialsId;
    $[4] = setRowData;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  const initialValues = t2;
  let t3;
  if ($[6] !== handleClose || $[7] !== isError || $[8] !== t) {
    t3 = () => {
      if (isError) {
        notifications.show({
          title: t("requestsErrors.error"),
          message: t("filials.filialNotFound"),
          color: "red"
        });
        handleClose();
      }
    };
    $[6] = handleClose;
    $[7] = isError;
    $[8] = t;
    $[9] = t3;
  } else {
    t3 = $[9];
  }
  let t4;
  if ($[10] !== isError) {
    t4 = [isError];
    $[10] = isError;
    $[11] = t4;
  } else {
    t4 = $[11];
  }
  reactExports.useEffect(t3, t4);
  let t5;
  if ($[12] !== data || $[13] !== initialValues || $[14] !== isLoading) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: isLoading ? /* @__PURE__ */ jsxRuntimeExports.jsx(Flex, { justify: "center", align: "center", h: "100vh", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, {}) }) : /* @__PURE__ */ jsxRuntimeExports.jsxs(FilialsInfoMainContext.Provider, { value: initialValues, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(DrawerHeader, {}),
      /* @__PURE__ */ jsxRuntimeExports.jsx(PhotoInfo, { filialsInfoCommon: data }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(BaseInfo, {})
    ] }) });
    $[12] = data;
    $[13] = initialValues;
    $[14] = isLoading;
    $[15] = t5;
  } else {
    t5 = $[15];
  }
  return t5;
};
export {
  Action as A,
  Panels as P
};
//# sourceMappingURL=index-DnphFLKH.js.map
