import { aj as requireInteropRequireDefault, ak as requireCreateSvgIcon, al as requireJsxRuntime, am as getDefaultExportFromCjs, r as reactExports, a0 as smetaStore, b7 as selectedSectionAtom, cd as selectedProcessAtom, ah as AxiosRequest, V as isEmptyObject, ac as tokenProcessingTest, j as jsxRuntimeExports, G as Grid, bV as Typography, T as Tooltip, I as IconButton, ce as Stepper, cf as Step, cg as StepLabel, ch as LinearProgress, ci as TextareaAutosize, E as useAtom, cj as listOfProcessesAtom, u as useAtomValue, ck as useSetAtom, z as currentTheme, cl as XMLrequest, bN as clientExports, R as RefreshIcon, bM as SectionToolsJS, ao as cn, y as styled, cm as ListItem, cn as ListItemButton, co as ListItemText, cp as List } from "./index-D15x63tD.js";
var DoDisturbAlt = {};
var hasRequiredDoDisturbAlt;
function requireDoDisturbAlt() {
  if (hasRequiredDoDisturbAlt) return DoDisturbAlt;
  hasRequiredDoDisturbAlt = 1;
  var _interopRequireDefault = requireInteropRequireDefault();
  Object.defineProperty(DoDisturbAlt, "__esModule", {
    value: true
  });
  DoDisturbAlt.default = void 0;
  var _createSvgIcon = _interopRequireDefault(/* @__PURE__ */ requireCreateSvgIcon());
  var _jsxRuntime = requireJsxRuntime();
  DoDisturbAlt.default = (0, _createSvgIcon.default)(/* @__PURE__ */ (0, _jsxRuntime.jsx)("path", {
    d: "M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2m0 18c-4.42 0-8-3.58-8-8 0-1.85.63-3.55 1.69-4.9L16.9 18.31C15.55 19.37 13.85 20 12 20m6.31-3.1L7.1 5.69C8.45 4.63 10.15 4 12 4c4.42 0 8 3.58 8 8 0 1.85-.63 3.55-1.69 4.9"
  }), "DoDisturbAlt");
  return DoDisturbAlt;
}
var DoDisturbAltExports = /* @__PURE__ */ requireDoDisturbAlt();
const DoDisturbAltIcon = /* @__PURE__ */ getDefaultExportFromCjs(DoDisturbAltExports);
function BackgroundProsessing(props) {
  const [start, setStart] = reactExports.useState(1);
  const [sections, setSections] = reactExports.useState([]);
  const [activeStep, setActiveStep] = reactExports.useState(0);
  const [title, setTitle] = reactExports.useState(props.title);
  const [titleLoad, setTitleLoad] = reactExports.useState("");
  const [Progress, setProgress] = reactExports.useState(0);
  const [StepProgress, setStepProgress] = reactExports.useState(0);
  const [Complete, setComplete] = reactExports.useState(false);
  const [intervall, setIntervall] = reactExports.useState();
  var starts = 0, interval;
  reactExports.useEffect(() => {
    if (props.ticket !== void 0 && interval === void 0) {
      setTitle(props.title);
      getTicketState();
      const selectedSection = smetaStore.get(selectedSectionAtom);
      const selectedProcess = smetaStore.get(selectedProcessAtom);
      interval = setInterval(function() {
        if (selectedSection === "-2" && selectedProcess === props.ticket) {
          getTicketState();
        }
      }, 1e3);
      setIntervall(interval);
    }
  }, [props.ticket]);
  reactExports.useEffect(() => {
    if (Complete) {
      clearInterval(intervall);
    }
  }, [Complete]);
  function getTicketState() {
    let params = /* @__PURE__ */ new Map();
    params.set("prefix", "queue");
    params.set("comand", "TicketState");
    params.set("Ticket", props.ticket);
    params.set("Start", starts);
    AxiosRequest(false, params).then((res) => {
      if (!isEmptyObject(res.WSM)) {
        starts += 1;
        for (const value of Object.values(res.WSM)) {
          setStart(start + 1);
          stateProcessing(value);
        }
      } else if (res.Complete === "1") {
        CompleteTicket();
      }
    });
  }
  function CompleteTicket() {
    const ttle = document.getElementById("TitleBackroundProcess");
    if (ttle.textContent.substring(20) !== " [закончен]") {
      ttle.textContent += " [закончен]";
    }
    clearInterval(interval);
    setComplete(true);
  }
  function stateProcessing(json) {
    switch (json.Token) {
      case "ChangeStatusProgress":
        if (json.Params.Title) {
          setTitleLoad(json.Params.Title);
        }
        setProgress(normalise(json.Params.Index, json.Params.Count));
        break;
      case "ChangeProjectStatus":
        break;
      case "OutPutText":
        const OutPutInput = document.getElementById("OutPutInput");
        OutPutInput.value += json.Params.Text + "\n";
        break;
      case "ShowProgressDialog":
        let steps = json.Params.Sections.split(",");
        setSections(steps);
        break;
      case "StepProgress":
        const StepPRogresParent = document.getElementById("SteppRogressBackground");
        StepPRogresParent.style.display = "";
        setStepProgress(normalise(json.Params.Index, json.Params.Count));
        break;
      case "SetProgressSection":
        let Index;
        Index = Number(json.Params.Index);
        if (isNaN(Index)) Index = 0;
        setActiveStep(Index);
        break;
      case "ShellExecute":
        tokenProcessingTest(json);
        break;
    }
  }
  function ExitProcess() {
    let params = /* @__PURE__ */ new Map();
    params.set("prefix", "queue");
    params.set("comand", "ExitProcess");
    params.set("Ticket", props.ticket);
    params.set("smart", 1);
    CompleteTicket();
    AxiosRequest(false, params).then((res) => {
    });
  }
  const normalise = (value, MAX) => (Number(value) - 0) * 100 / (Number(MAX) - 0);
  return (
    // <>
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { id: props.ticket, style: { width: "100%", height: "100%" }, children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(
        Grid,
        {
          item: true,
          container: true,
          direction: "row",
          justifyContent: "flex-start",
          alignItems: "center",
          variant: "h5",
          style: { height: "40px", width: "100%", paddingLeft: "10px" },
          children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(Typography, { style: { width: "95%" }, id: "TitleBackroundProcess", children: title }),
            /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { title: "Остановить процесс", children: /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { onClick: ExitProcess, children: /* @__PURE__ */ jsxRuntimeExports.jsx(DoDisturbAltIcon, {}) }) }),
            /* @__PURE__ */ jsxRuntimeExports.jsx(
              "button",
              {
                id: props.ticket + "exit",
                style: { display: "none" },
                onClick: () => {
                  clearInterval(interval);
                }
              }
            )
          ]
        }
      ),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(
        Grid,
        {
          item: true,
          container: true,
          direction: "column",
          justifyContent: "center",
          alignItems: "center",
          style: { height: "calc(100% - 40px)", width: "100%" },
          children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs(
              Grid,
              {
                item: true,
                container: true,
                direction: "column",
                justifyContent: "flex-start",
                alignItems: "flex-start",
                style: {
                  height: "50%",
                  width: "100%",
                  borderTop: "1px solid",
                  paddingLeft: "10px"
                },
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, xs: 11, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Stepper, { activeStep, orientation: "vertical", connector: /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {}), children: sections.map((step) => /* @__PURE__ */ jsxRuntimeExports.jsx(Step, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(StepLabel, { children: step.replace('"', "").slice(0, -1) }, step.label) })) }) }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, id: "SteppRogressBackground", style: { display: "none", width: "20%" }, xs: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(LinearProgress, { variant: "determinate", value: StepProgress }) })
                ]
              }
            ),
            /* @__PURE__ */ jsxRuntimeExports.jsx(
              Grid,
              {
                item: true,
                style: {
                  height: "calc(50% - 20px)",
                  width: "100%"
                },
                children: /* @__PURE__ */ jsxRuntimeExports.jsx(
                  TextareaAutosize,
                  {
                    id: "OutPutInputBackGround",
                    style: {
                      height: "100%",
                      width: "inherit",
                      resize: "none",
                      overflow: "auto",
                      outline: "none"
                    }
                  }
                )
              }
            ),
            /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, style: { height: "20px", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
              Grid,
              {
                container: true,
                direction: "row",
                justifyContent: "flex-end",
                alignItems: "center",
                sx: { pl: 2, mt: 0 },
                id: "BGLoading",
                children: [
                  /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, xs: true, sx: { pt: titleLoad === "" ? 1 : 0 }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(LinearProgress, { variant: "determinate", value: Progress }) }),
                  /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, style: { color: "black" }, sx: { pl: 1 }, children: titleLoad })
                ]
              }
            ) })
          ]
        }
      )
    ] })
  );
}
const StyledList = styled(List)({
  // selected and (selected + hover) states
  "&& .Mui-selected, && .Mui-selected:hover": {
    backgroundColor: "#dcd8cc"
  },
  "&.MuiListItemSecondaryAction-root": {
    top: "69%",
    right: "92%"
  }
});
function BackgroundProcess(props) {
  const [title, setTitle] = reactExports.useState("");
  const [ticket, setTicket] = reactExports.useState(void 0);
  const [tickets, setTickets] = reactExports.useState([]);
  const [listOfProcesses, setListOfProcesses] = useAtom(listOfProcessesAtom);
  const selectedSection = useAtomValue(selectedSectionAtom);
  const setSelectedProcess = useSetAtom(selectedProcessAtom);
  const theme = useAtomValue(currentTheme);
  reactExports.useEffect(() => {
    if (selectedSection.ID === "-2") GetTicketState();
    console.log("effect background");
  }, [selectedSection.ID]);
  function GetTicketState() {
    let params = /* @__PURE__ */ new Map();
    params.set("prefix", "queue");
    params.set("comand", "TicketState");
    const json = XMLrequest(params);
    setTickets(json);
    setListOfProcesses(json.items);
    RenderRefreshButton();
    if (!isEmptyObject(json)) {
      console.log(json.items);
      for (const [key, value] of Object.entries(json.items)) {
        if (key === `${json.items.length - 1}`) {
          setTitle(value.Title);
          setTicket(value.Ticket);
          setSelectedProcess(value.Ticket);
          addChild(value);
        }
      }
    }
  }
  function RenderRefreshButton() {
    const buttonBlock = document.getElementById("buttons_for_section-2");
    const buttonBlockRoot = buttonBlock.root || clientExports.createRoot(buttonBlock);
    buttonBlock.root = buttonBlockRoot;
    buttonBlockRoot.render(
      /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { title: "Обновить", arrow: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(IconButton, { color: "primary", component: "span", onClick: handleRefresh, id: "refreshButtonForBackGroudds", children: /* @__PURE__ */ jsxRuntimeExports.jsx(RefreshIcon, { fontSize: "small" }) }) }) })
    );
  }
  function handleRefresh() {
    if (tickets) {
      for (const value of Object.values(tickets.items)) {
        const exitButton = document.getElementById(value.Ticket + "exit");
        exitButton.click();
      }
      let p = document.getElementById("Processing-2");
      if (p) {
        p.innerHTML = "";
      }
      GetTicketState();
    }
  }
  function GetData(CurrnetTicket, List2) {
    let backvalue;
    List2.filter((ticket2) => {
      if (ticket2["Ticket"] === CurrnetTicket) return backvalue = ticket2;
      return false;
    });
    return backvalue;
  }
  function addChild(data) {
    const p = document.getElementById("Processing-2");
    let container = document.createElement("div");
    container.className = "MuiGrid-root MuiGrid-container MuiGrid-item MuiGrid-direction-xs-column css-1bz19vm-MuiGrid-root";
    container.style.height = "100%";
    container.id = data.Ticket;
    p.appendChild(container);
    var par = /* @__PURE__ */ jsxRuntimeExports.jsx(BackgroundProsessing, { ticket: data.Ticket, title: data.Title, SelectedTicket: title });
    clientExports.createRoot(container).render(par);
  }
  function ClickProcess(ev) {
    let ticketID = ev.target.parentNode.getAttribute("ticket");
    let p = document.getElementById("Processing-2");
    const data = GetData(ticketID, tickets.items);
    let page;
    for (const value of Object.values(p.children)) {
      if (value.id === ticketID) {
        page = value;
      } else {
        value.style.display = "none";
      }
    }
    if (!page) {
      addChild(data);
    } else {
      page.style.display = "";
    }
    setTitle(data.Title);
    setTicket(ticketID);
    setSelectedProcess(ticketID);
    ev.stopPropagation();
  }
  function AddItem(item, pl) {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      ListItem,
      {
        ticket: item.Ticket,
        disablePadding: true,
        style: {
          paddingTop: 0,
          paddingBottom: 0,
          paddingLeft: 0,
          paddingRight: 0,
          display: "inherit"
        },
        sx: {
          "& .Mui-selected": { backgroundColor: "rgb(35, 114, 191)" },
          "& .MuiListItemSecondaryAction-root": { right: "auto", left: "0%" }
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsx(
          ListItemButton,
          {
            ticket: item.Ticket,
            style: { paddingRight: 0 },
            sx: { pl, pt: 0, pb: 0 },
            component: "li",
            onClick: ClickProcess,
            selected: title === item.Title,
            children: /* @__PURE__ */ jsxRuntimeExports.jsx(
              ListItemText,
              {
                ticket: item.Ticket,
                className: cn("fontColorStimate", { light: theme === "light" }),
                primary: /* @__PURE__ */ jsxRuntimeExports.jsx(
                  Typography,
                  {
                    ticket: item.Ticket,
                    variant: "body1",
                    style: {
                      fontSize: "0.875rem",
                      whiteSpace: "nowrap",
                      overflow: "hidden",
                      textOverflow: "ellipsis",
                      width: "99.9%"
                    },
                    children: item.Title
                  }
                )
              }
            )
          }
        )
      }
    );
  }
  function CreateTickets(data) {
    if (data !== void 0) {
      let list = [];
      for (let i = 0; i <= data.length - 1; i++) {
        list.push(AddItem(data[i], 3));
      }
      return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { height: "100%" }, className: cn("sidebarStimate", { light: theme === "light" }), children: /* @__PURE__ */ jsxRuntimeExports.jsx(StyledList, { sx: { width: "100%", bgcolor: "#faf9f5", pt: 0 }, style: { border: 0, width: "100%" }, children: list }) });
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { id: "Page-2", style: { height: "100%" }, children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(SectionToolsJS, { ID: "-2" }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(
      Grid,
      {
        container: true,
        direction: "row",
        justifyContent: "flex-start",
        alignItems: "center",
        style: { width: "100%", height: "94.5%" },
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Grid,
            {
              item: true,
              style: { width: "200px", height: "100%" },
              className: cn("sidebarStimate", { light: theme === "light" }),
              children: CreateTickets(listOfProcesses)
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Grid,
            {
              id: "Processing-2",
              item: true,
              container: true,
              direction: "column",
              justifyContent: "center",
              alignItems: "flex-start",
              style: { width: "calc(100% - 200px)", height: "100%" }
            }
          )
        ]
      }
    )
  ] });
}
export {
  BackgroundProcess as default
};
//# sourceMappingURL=BackgroundProcess-DanQPwp2.js.map
