import { r as reactExports, j as jsxRuntimeExports, E as compilerRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { az as useEventsTrailSlice, aA as commonSettingsApiArea, aB as DEFAULT_MODULE_ID, y as SearchIcon } from "./index-DfebxkL9.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
import { S as Search } from "./index-CcqTjOWY.js";
import { b as gq } from "./vendor-icons-xWhXK_Ui.js";
import { n as useDisclosure, ac as useClickOutside, $ as Table, a4 as Select, V as LoadingOverlay, c as createStyles, g as Loader } from "./vendor-mantine-h0WlM2Mj.js";
const useFilterEvents = () => {
  const {
    t
  } = useTranslation();
  const {
    useFilters,
    useEvents,
    actions: {
      LoadEvents
    }
  } = useEventsTrailSlice();
  const [filter] = useFilters().useState();
  const [commonSettings] = useEvents().useState();
  reactExports.useEffect(() => {
    LoadEvents();
  }, []);
  const filteredEvents = reactExports.useMemo(() => {
    if (!(commonSettings == null ? void 0 : commonSettings.UserActionPriority)) return [];
    return commonSettings.UserActionPriority.filter((event) => {
      const localizedText = event.TypeName;
      const matchesName = localizedText.toLowerCase().includes(filter.name.toLowerCase());
      const matchesModule = filter.module && filter.module !== "0" ? filter.module.toString() === event.ModuleName.toString() : true;
      return matchesName && matchesModule;
    });
  }, [commonSettings, filter.name, filter.module, t]);
  return {
    commonSettings,
    filteredEvents
  };
};
const headerCell = "_headerCell_u8tiy_1";
const classes$3 = {
  headerCell
};
const tableCell = "_tableCell_18483_1";
const events = "_events_18483_9";
const module$1 = "_module_18483_17";
const select$1 = "_select_18483_25";
const statusHandler = "_statusHandler_18483_30";
const arrow = "_arrow_18483_37";
const classes$2 = {
  tableCell,
  events,
  module: module$1,
  select: select$1,
  statusHandler,
  arrow
};
var LevelUserPriority = /* @__PURE__ */ ((LevelUserPriority2) => {
  LevelUserPriority2[LevelUserPriority2["Emergency"] = 0] = "Emergency";
  LevelUserPriority2[LevelUserPriority2["Alert"] = 1] = "Alert";
  LevelUserPriority2[LevelUserPriority2["Critical"] = 2] = "Critical";
  LevelUserPriority2[LevelUserPriority2["Error"] = 3] = "Error";
  LevelUserPriority2[LevelUserPriority2["Warning"] = 4] = "Warning";
  LevelUserPriority2[LevelUserPriority2["Notice"] = 5] = "Notice";
  LevelUserPriority2[LevelUserPriority2["Info"] = 6] = "Info";
  LevelUserPriority2[LevelUserPriority2["Debug"] = 7] = "Debug";
  return LevelUserPriority2;
})(LevelUserPriority || {});
var CommonSettingType = /* @__PURE__ */ ((CommonSettingType2) => {
  CommonSettingType2[CommonSettingType2["None"] = 0] = "None";
  CommonSettingType2[CommonSettingType2["Commands"] = 1] = "Commands";
  CommonSettingType2[CommonSettingType2["CommonViewSettings"] = 2] = "CommonViewSettings";
  CommonSettingType2[CommonSettingType2["UserActionPriority"] = 3] = "UserActionPriority";
  return CommonSettingType2;
})(CommonSettingType || {});
function RowItem(props) {
  var _a, _b;
  const {
    element
  } = props;
  const {
    t
  } = useTranslation();
  const useCX = createStyles({});
  const {
    cx
  } = useCX();
  const [isLoading, setIsLoading] = reactExports.useState(false);
  const [dropdownOpened, {
    toggle,
    close
  }] = useDisclosure();
  const {
    LoadEventsWithOutLoadingStatus
  } = useEventsTrailSlice().actions;
  const [commonSettings] = useEventsTrailSlice().useEvents().useState();
  const ref = useClickOutside(() => dropdownOpened && close());
  const priorityOptions = Object.entries(LevelUserPriority).filter(([_key, value]) => typeof value === "number").map(([key, value_0]) => ({
    value: String(value_0),
    label: t(`filials.settings.priority.${key}`)
  }));
  const propsValue = (_a = priorityOptions == null ? void 0 : priorityOptions.find((el) => el.value === element.Level.toString())) == null ? void 0 : _a.value;
  const [value_1, setValue] = reactExports.useState(propsValue ? String(propsValue) : void 0);
  const handleChange = (Level) => {
    if (!commonSettings || !Level) {
      return;
    }
    setValue(Level);
    setIsLoading(true);
    let priorityToChange = commonSettings.UserActionPriority.find((e) => e.Id === element.Id);
    if (!priorityToChange) {
      return;
    }
    priorityToChange = {
      ...element,
      Level: Number(Level)
    };
    const params = {
      Type: CommonSettingType.UserActionPriority,
      Data: {
        UserActionPriority: [priorityToChange]
      }
    };
    commonSettingsApiArea().SaveSettings(params).then(LoadEventsWithOutLoadingStatus).finally(() => setIsLoading(false));
  };
  const currentValue = (_b = priorityOptions.find((i) => value_1 === i.value.toString())) == null ? void 0 : _b.label;
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(Table.Tr, { style: {
    position: "relative"
  }, children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Td, { className: classes$2.tableCell, w: "300px", children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: classes$2.events, children: element.TypeName }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Td, { className: classes$2.tableCell, w: "150px", children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: classes$2.module, children: element.ModuleName }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Table.Td, { className: cx(classes$2.tableCell), w: "300px", ref, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes$2.statusHandler, onClick: toggle, children: [
        currentValue,
        /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { className: classes$2.arrow, size: "1rem", color: "#98A2B3" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Select, { value: value_1, dropdownOpened, rightSection: /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3" }), onChange: handleChange, className: classes$2.select, data: priorityOptions, comboboxProps: {
        transitionProps: {
          transition: "pop",
          duration: 200
        },
        offset: -20
      } }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading, style: {
        position: "absolute"
      } })
    ] })
  ] });
}
function EventsTable(props) {
  const $ = compilerRuntimeExports.c(23);
  const {
    t
  } = useTranslation();
  let t0;
  if ($[0] !== t) {
    t0 = t("filials.settings.table.event");
    $[0] = t;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  let t1;
  if ($[2] !== t0) {
    t1 = /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Th, { className: classes$3.headerCell, children: t0 });
    $[2] = t0;
    $[3] = t1;
  } else {
    t1 = $[3];
  }
  let t2;
  if ($[4] !== t) {
    t2 = t("filials.settings.table.module");
    $[4] = t;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  let t3;
  if ($[6] !== t2) {
    t3 = /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Th, { className: classes$3.headerCell, children: t2 });
    $[6] = t2;
    $[7] = t3;
  } else {
    t3 = $[7];
  }
  let t4;
  if ($[8] !== t) {
    t4 = t("filials.settings.table.criticalityLevel");
    $[8] = t;
    $[9] = t4;
  } else {
    t4 = $[9];
  }
  let t5;
  if ($[10] !== t4) {
    t5 = /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Th, { className: classes$3.headerCell, children: t4 });
    $[10] = t4;
    $[11] = t5;
  } else {
    t5 = $[11];
  }
  let t6;
  if ($[12] !== t1 || $[13] !== t3 || $[14] !== t5) {
    t6 = /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Thead, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Table.Tr, { children: [
      t1,
      t3,
      t5
    ] }) });
    $[12] = t1;
    $[13] = t3;
    $[14] = t5;
    $[15] = t6;
  } else {
    t6 = $[15];
  }
  let t7;
  if ($[16] !== props.events) {
    t7 = props.events.map(_temp);
    $[16] = props.events;
    $[17] = t7;
  } else {
    t7 = $[17];
  }
  let t8;
  if ($[18] !== t7) {
    t8 = /* @__PURE__ */ jsxRuntimeExports.jsx(Table.Tbody, { children: t7 });
    $[18] = t7;
    $[19] = t8;
  } else {
    t8 = $[19];
  }
  let t9;
  if ($[20] !== t6 || $[21] !== t8) {
    t9 = /* @__PURE__ */ jsxRuntimeExports.jsxs(Table, { children: [
      t6,
      t8
    ] });
    $[20] = t6;
    $[21] = t8;
    $[22] = t9;
  } else {
    t9 = $[22];
  }
  return t9;
}
function _temp(element, index) {
  return /* @__PURE__ */ jsxRuntimeExports.jsx(RowItem, { element }, index);
}
const select = "_select_1ib71_1";
const classes$1 = {
  select
};
function FilterTable() {
  const [filter, setFilter] = useEventsTrailSlice().useFilters().useState();
  const [commonSettings] = useEventsTrailSlice().useEvents().useState();
  const onChange = (module) => {
    setFilter((prev) => ({
      ...prev,
      module: module || ""
    }));
  };
  const priorities = (commonSettings == null ? void 0 : commonSettings.UserActionPriority) || [];
  const {
    t
  } = useTranslation();
  const moduleOptions = priorities.reduce((acc, p) => {
    const hasPriority = acc.find((i) => i.value === p.ModuleName);
    if (!hasPriority) {
      acc.push({
        value: p.ModuleName,
        label: p.ModuleName
      });
      return acc;
    }
    return acc;
  }, [{
    value: DEFAULT_MODULE_ID,
    label: t("filials.settings.allModules")
  }]);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Select, { rightSection: /* @__PURE__ */ jsxRuntimeExports.jsx(gq, { size: "1rem", color: "#98A2B3" }), placeholder: t("filials.settings.searchByName"), className: classes$1.select, value: filter.module, onChange, data: moduleOptions, comboboxProps: {
    transitionProps: {
      transition: "pop",
      duration: 200
    }
  } });
}
const loadingContainer = "_loadingContainer_khn5c_1";
const input = "_input_khn5c_9";
const container = "_container_khn5c_25";
const content = "_content_khn5c_29";
const title = "_title_khn5c_37";
const classes = {
  loadingContainer,
  input,
  container,
  content,
  title
};
function EventsTrailPage() {
  const {
    filteredEvents
  } = useFilterEvents();
  const {
    useFilters,
    useIsLoading
  } = useEventsTrailSlice();
  const [filter, setFilter] = useFilters().useState();
  const [isLoading] = useIsLoading().useState();
  const {
    t
  } = useTranslation();
  const handleSearch = (name) => {
    setFilter((prev) => ({
      ...prev,
      name
    }));
  };
  if (isLoading) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: classes.loadingContainer, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Loader, { color: "blue" }) });
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.container, children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Search, { style: {
      width: "550px"
    }, icon: /* @__PURE__ */ jsxRuntimeExports.jsx(SearchIcon, {}), className: classes.input, setSearch: handleSearch, value: filter.name, placeholder: t("filials.settings.searchByEvent") }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classes.content, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: classes.title, children: t("filials.settings.eventsTitle") }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(FilterTable, {}),
      /* @__PURE__ */ jsxRuntimeExports.jsx(EventsTable, { events: filteredEvents })
    ] })
  ] });
}
export {
  EventsTrailPage
};
//# sourceMappingURL=index-FLmifAIL.js.map
