import { t as React, j as jsxRuntimeExports, a3 as Tree, a4 as treeRequest, a5 as GetDialogParamsForSearch, a6 as GridLoader, a7 as TabItem, a8 as Search, a9 as ContentOrganization, aa as getBookContextMenu, ab as formEdit, ac as tokenProcessingTest, ad as PostHandleTable, ae as getRightsData, af as parseColumns, ag as handleTable, ah as AxiosRequest } from "./index-D15x63tD.js";
async function saveCurrentGrid(props, gridObject, groupID, setSelectedRecordID, mainSources, ObjRef) {
  const json = await PostHandleTable(props.Module, "0", groupID, props.SectionID);
  if (json) {
    gridObject.source.rights = getRightsData(json.Rights);
  }
  const objType = json ? json.ObjType : props.ObjType;
  const columns = parseColumns(json);
  gridObject.grid.setColumns(columns);
  gridObject.source.onHandleRequest = handleTable({
    Module: props.Module,
    ObjType: objType,
    GroupID: groupID,
    selectedRecordID: ObjRef ? ObjRef : "0",
    SectionID: props.SectionID
  });
  await gridObject.source.first();
  gridObject.source.onRecordIndexChanged = function(source) {
    setSelectedRecordID(source.getFieldTextSync("ID"));
  };
  await gridObject.grid.setSource(gridObject.source);
  await gridObject.grid.refreshSource(ObjRef ? ObjRef : "0");
  await gridObject.grid.updateGridSize();
  setSelectedRecordID(gridObject.source.getFieldTextSync("ID"));
  mainSources.set(groupID, {
    columns,
    ID: gridObject.source.getFieldTextSync("ID"),
    request: handleTable,
    objType
  });
}
function DialogPerson({ props }) {
  const [mainGrid, setMainGrid] = React.useState(void 0);
  const [selectedRecordID, setSelectedRecordID] = React.useState(props.ObjRef);
  const [groupID, setGroupID] = React.useState(props.NodeID ? props.NodeID : "0");
  const firstLoad = React.useRef(true);
  const mainSources = React.useRef(/* @__PURE__ */ new Map());
  const [countRecords, setCountRecords] = React.useState("0");
  const [searchResult, setSearchResult] = React.useState();
  const gridPanel = React.useRef();
  const tabsPanel = React.useRef();
  const [tree, setTree] = React.useState();
  const [tab, setTab] = React.useState();
  React.useEffect(() => {
    setTree(null);
    setMainGridLoader();
  }, []);
  React.useEffect(() => {
    if (tree === null) {
      setTree(
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          Tree,
          {
            props: {
              CLSID: props.CLSID,
              multiCheck: false,
              ObjType: props.ObjType,
              SectionID: props.SectionID,
              Module: props.Module,
              Current: groupID,
              ObjRef: groupID,
              selectID: setGroupID,
              UsedDate: "0",
              Info: "0",
              Internal: "0"
            }
          }
        )
      );
      return;
    }
    if (groupID === "0") {
      setGroupID(treeRequest("persons", props.ObjType, props.SectionID).Current);
    }
  }, [tree]);
  React.useLayoutEffect(() => {
    if (mainGrid) {
      setTab(getTabItem());
      if (groupID) {
        saveCurrentGrid(
          props,
          mainGrid,
          groupID,
          setSelectedRecordID,
          mainSources.current,
          selectedRecordID ? selectedRecordID : "0"
        ).then(() => {
          firstLoad.current = false;
          setCountRecords(mainGrid.source.recordCount);
        });
      }
    }
  }, [mainGrid]);
  React.useLayoutEffect(() => {
    console.log(groupID);
    if (groupID) {
      setMainSource().then(() => {
        if (mainGrid) setCountRecords(mainGrid.source.recordCount);
        if (mainSources.current.get(groupID)) {
          setSelectedRecordID(mainSources.current.get(groupID).ID);
        }
      });
    }
  }, [groupID]);
  React.useEffect(() => {
    if (mainGrid && mainSources.current.get(groupID)) {
      mainSources.current.get(groupID).ID = selectedRecordID;
    }
  }, [selectedRecordID]);
  React.useLayoutEffect(() => {
    if (mainGrid && searchResult) {
      const id = GetDialogParamsForSearch(props.Module, props.ObjType, searchResult).NodeID;
      if (id === groupID) {
        findSearchRecordFromTable();
      } else {
        setTree(null);
        setGroupID(id);
      }
    }
  }, [searchResult]);
  async function findSearchRecordFromTable() {
    await mainGrid.grid.refreshSource(searchResult).then();
    setSelectedRecordID(searchResult);
    setSearchResult(void 0);
  }
  async function setMainSource() {
    if (mainGrid && mainSources.current.get(groupID) === void 0) {
      await saveCurrentGrid(
        props,
        mainGrid,
        groupID,
        setSelectedRecordID,
        mainSources.current,
        firstLoad.current ? props.ObjRef : "0"
      );
      firstLoad.current = false;
    } else if (mainGrid) {
      const mainSource = mainSources.current.get(groupID);
      mainGrid.grid.setColumns(mainSource.columns);
      mainGrid.source.onHandleRequest = mainSource.request({
        Module: props.Module,
        ObjType: mainSource.objType,
        GroupID: groupID,
        selectedRecordID: mainSource.ID ? mainSource.ID : "0",
        SectionID: props.SectionID
      });
      if (!searchResult) {
        await mainGrid.grid.refreshSource(mainSource.ID);
      }
      await mainGrid.grid.setSource(mainGrid.source);
      await mainGrid.grid.updateGridSize();
    }
    if (mainGrid) {
      if (searchResult) {
        await findSearchRecordFromTable();
        return;
      }
    }
  }
  async function setMainGridLoader() {
    if (!mainGrid) {
      setMainGrid(
        await GridLoader(gridPanel.current, props, {
          GroupID: groupID,
          setSelectedRecord: setSelectedRecordID,
          selectedRecordID
        })
      );
    }
  }
  function getTabItem() {
    return /* @__PURE__ */ jsxRuntimeExports.jsx(
      TabItem,
      {
        style: {
          textTransform: "none",
          width: "100%",
          height: "100%",
          padding: "0px",
          display: "inline-block"
        },
        label: "Поиск",
        children: /* @__PURE__ */ jsxRuntimeExports.jsx("div", { id: "item:Search", style: { height: "100%", width: "100%" }, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Search, { GroupId: groupID, Module: props.Module, ObjType: props.ObjType, return: setSearchResult }) })
      },
      "tabItem:Search"
    );
  }
  async function onSelect() {
    if (props.onEdit && props.setValue && props.setInputValue) {
      await props.onEdit({
        tag: selectedRecordID,
        groupid: groupID,
        name: props.Name,
        record: props.record,
        textchanged: "0",
        setValue: props.setValue,
        setInputValue: props.setInputValue,
        type: props.Module,
        requestId: props.RequestID,
        addInfo: props.addInfo
      });
      return;
    }
    if (props.RequestID) {
      await formEdit("1", props.func, { Name: "ObjRef", Value: selectedRecordID }, props.RequestID, props.from);
    }
  }
  function BookContextMenuHandler(data) {
    const params = /* @__PURE__ */ new Map();
    params.set("prefix", props.Module).set("comand", "BookContextMenuHandler").set("ID", data.id).set("ObjRef", data.ObjRef).set("ObjType", data.ObjType).set("WSM", "1");
    if (data.SectionID) params.set("SectionID", data.SectionID);
    return AxiosRequest(true, params);
  }
  async function ContextMenuHandler(data) {
    const comand = data.value;
    let json;
    switch (comand) {
      case "":
        break;
      default:
        if (data.ID !== void 0) {
          json = await BookContextMenuHandler({
            id: data.ID,
            ObjRef: selectedRecordID,
            ObjType: mainSources.current.get(groupID).objType
          });
          if (json.Token) {
            tokenProcessingTest(json, {
              func: () => mainGrid.grid.refreshSource(selectedRecordID)
            });
          } else mainGrid.grid.refreshSource(selectedRecordID);
        }
        break;
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(
    ContentOrganization,
    {
      props,
      data: {
        tabs: tab,
        tree,
        gridPanel,
        mainGrid,
        countRecords,
        onSelect: async () => {
          await onSelect();
        },
        onCancel: async () => {
          if (props.RequestID) {
            await formEdit("0", props.func, { Name: "ObjRef", Value: selectedRecordID }, props.RequestID, props.from);
          }
        },
        tabsPanel,
        mainMenuSelector: "organizations",
        mainMenuRequest: getBookContextMenu,
        ContextMenuHandler,
        objType: mainSources.current.get(groupID) ? mainSources.current.get(groupID).objType : props.ObjType,
        selectedRecordID: selectedRecordID ? selectedRecordID : "0"
      },
      buttons: {
        search: 0,
        detail: true,
        tree: true,
        grouping: false,
        contragent: false
      }
    }
  ) });
}
export {
  DialogPerson as default
};
//# sourceMappingURL=DialogPerson-B2tjg4Qp.js.map
