"use strict";
class CGlobExplorerData {
    constructor(aFs) {
        this.sUrl1 = "";
        this.sUrl2 = "";
        this.sKey1 = "";
        this.sKey2 = "";
        this.sPrevMode = "";
        this.sUrl1 = oSavedData.url1;
        this.sKey1 = oSavedData.accKey1;
        this.sUrl2 = oSavedData.url2;
        this.sKey2 = oSavedData.accKey2;
        const oBDP = { sUrl1: this.sUrl1, sKey1: this.sKey1, sUrl2: this.sUrl2, sKey2: this.sKey2 };
        this.DTP1 = CreateBrowserTree(aFs.file_systems, oBDP, { fBackspace: () => { this.MoveUpstairs('L'); } });
        this.DTP2 = CreateBrowserTree(aFs.file_systems, oBDP, { fBackspace: () => { this.MoveUpstairs('R'); } });
        if (oSavedData.IsWinMode()) {
            this.DTP1.bHide = true;
        }
        const obj = { oDTP1: this.DTP1, oDTP2: this.DTP2, sUrl1: GetPathForShow(oBDP.sUrl1), sUrl2: GetPathForShow(oBDP.sUrl2),
            oFolderContTableL: this.DTP1.oFolderContTable, oFolderContTableR: this.DTP2.oFolderContTable };
        const oBrowseDialog = CreateFloatMenu(jsFloatMenu["MsExplore"], { width: "100%", height: -100 }, obj, { bNoTitle: true, bNoborder: true, bNoResize: true, bNoClose: true });
        this.domExplorer = oBrowseDialog.oMenu;
        this.domRightPanel1 = document.getElementById("FolderContTableL_" + this.domExplorer.id);
        if (this.domRightPanel1) {
            this.domRightPanel1.oncontextmenu = (e) => {
                if (this.DTP1.oFolderContTable && this.DTP1.oFolderContTable.oParentBr && this.DTP1.oFolderContTable.oParentBr.fRightBtn) {
                    e.preventDefault();
                    e.stopPropagation();
                    this.DTP1.oFolderContTable.oParentBr.fRightBtn(e, this.DTP1.oFolderContTable.oParentBr, this.DTP1, true);
                }
            };
        }
        this.domRightPanel2 = document.getElementById("FolderContTableR_" + this.domExplorer.id);
        this.domTree1 = document.getElementById("TreeL_" + this.domExplorer.id);
        this.domTree2 = document.getElementById("TreeR_" + this.domExplorer.id);
        this.pUrl1 = document.getElementById("Url1_" + oBrowseDialog.oMenu.id);
        this.pUrl2 = document.getElementById("Url2_" + oBrowseDialog.oMenu.id);
        if (oSavedData.eExplorerMode == 'two_tree') {
            EnableClassEl("FsOperationsBtn");
            SetDomDisplayAndInnerHtml("first_tree_" + oBrowseDialog.oMenu.id, null, { text: null, class: ["stretch_x", "thick_right_border"] });
            this.domSecondTree = document.getElementById("second_tree_" + oBrowseDialog.oMenu.id);
            if (!this.DTP1.fDoMarkRowsShow()) {
                this.DTP1.ShowMarkRowInCenter();
            }
            if (!this.DTP2.fDoMarkRowsShow()) {
                this.DTP2.ShowMarkRowInCenter();
            }
            SetStretchEl();
            this.SetWgseDTP(this.DTP1, this.pUrl1);
        }
        else {
            document.getElementById("second_tree_" + oBrowseDialog.oMenu.id).style.display = "none";
            DisableClassEl("FsOperationsBtn");
            this.SwitchMode(oSavedData.eExplorerMode, false, true);
            this.sPrevMode = oSavedData.eExplorerMode;
        }
        this.SetWgseDTP(this.DTP2, this.pUrl2, true);
        if (this.pUrl1 && "value" in this.pUrl1) {
            this.pUrl1.fKeyDown = (e) => ExplorerUrlEnter(e, this.pUrl1, {}, this.DTP1);
            this.pUrl1.value = this.sUrl1;
        }
        if (this.pUrl2 && "value" in this.pUrl2) {
            this.pUrl2.fKeyDown = (e) => ExplorerUrlEnter(e, this.pUrl2, {}, this.DTP2);
            this.pUrl2.value = this.sUrl2;
        }
    }
    async ScrollToOldLines() {
        if (this.sUrl1 && this.sUrl2 && this.domSecondTree) {
            await Promise.allSettled([
                ExplorerUrlProc(this.pUrl1.value, { sKey1: this.sKey1 }, this.DTP1),
                ExplorerUrlProc(this.pUrl2.value, { sKey1: this.sKey2 }, this.DTP2)
            ]);
        }
        else if (this.sUrl1) {
            await ExplorerUrlProc(this.pUrl1.value, { sKey1: this.sKey1 }, this.DTP1);
        }
        else if (this.sUrl2 && this.domSecondTree) {
            await ExplorerUrlProc(this.pUrl2.value, { sKey1: this.sKey2 }, this.DTP2);
        }
    }
    GetExprolerWinId() {
        if (this.domExplorer) {
            return this.domExplorer.id;
        }
        return "";
    }
    SetWgseDTP(oDTP, pUrl, bRight) {
        if (oSavedData.IsWinMode()) {
            oDTP.fRowClick = async (oTableBranch, bMouseClick) => {
                if (oDTP.oFolderContTable && bMouseClick) {
                    if (oDTP.oFolderContTable.oParentBr === oTableBranch)
                        return;
                    oDTP.oFolderContTable.oParentBr = oTableBranch;
                    if (oDTP.oFolderContTable.domLimWindow) {
                        oDTP.oFolderContTable.domLimWindow.scrollTop = 0;
                        oDTP.oFolderContTable.nScrollTop = 0;
                        oDTP.oFolderContTable.nFirstShowRow = 0;
                        delete oDTP.oFolderContTable.rectIconLine;
                    }
                    oDTP.oFolderContTable.fSelTabClean();
                }
                const sUrl = ExploreTableRowClick(oTableBranch, {}, pUrl, "");
                const sKey = oTableBranch.fGetData().fGetAccKey ? oTableBranch.fGetData().fGetAccKey() : "";
                SaveExplorerData(sUrl, sKey, bRight);
                if (bMouseClick && oDTP.oFolderContTable) {
                    const domFolderContTable = bRight ? this.domRightPanel2 : this.domRightPanel1;
                    if (!domFolderContTable)
                        return;
                    let domShadow = BlockAndWaitCreate(domFolderContTable, "explore_waiting" + oDTP.oFolderContTable.unicId, 8);
                    const sUnic = "ewb_" + GetCurrTime() + Math.random();
                    domShadow.className = "block_and_wait " + sUnic;
                    try {
                        await TableOpenFolder(oDTP, oTableBranch, true);
                    }
                    catch { }
                    if (domShadow && domShadow.classList.contains(sUnic)) {
                        oDTP.oFolderContTable.root.fAddDataArr(oTableBranch.cont, { bClean: true, oDTP: oDTP.oFolderContTable });
                        if (oSavedData.eExplorerMode == 'med_icon_mode') {
                            oDTP.oFolderContTable.SortBranch(oDTP.oFolderContTable.root.cont);
                            oDTP.oFolderContTable.ShowIcons(true, true);
                        }
                        else {
                            oDTP.oFolderContTable.ShowTable({ bCalcScroll: true });
                        }
                        if (domShadow && domShadow.classList.contains(sUnic)) {
                            domShadow.remove();
                        }
                    }
                }
            };
        }
        else {
            oDTP.fRowClick = function (oTableBranch, bMouseClick) {
                if (oDTP.oFolderContTable) {
                    if (oDTP.oFolderContTable.oParentBr === oTableBranch)
                        return;
                    oDTP.oFolderContTable.oParentBr = oTableBranch;
                    oDTP.oFolderContTable.fSelTabClean();
                }
                const sUrl = ExploreTableRowClick(oTableBranch, {}, pUrl, "");
                const sKey = oTableBranch.fGetData().fGetAccKey ? oTableBranch.fGetData().fGetAccKey() : "";
                SaveExplorerData(sUrl, sKey, bRight);
            };
        }
    }
    async SwitchMode(eExplorerMode, bRightTree, bWasTreeMode) {
        const oDTP = bRightTree ? this.DTP2 : this.DTP1;
        const pUrl = bRightTree ? this.pUrl2 : this.pUrl1;
        const domRightPanel = bRightTree ? this.domRightPanel2 : this.domRightPanel1;
        if (eExplorerMode == 'detal_mode' || eExplorerMode == 'med_icon_mode') {
            if (!domRightPanel)
                return;
            this.domTree1.classList.add("stretch_x");
            oDTP.aHead = jsFloatMenuTableHeads.WinExploreTreeHead;
            oDTP.bHide = true;
            oDTP.bOnlyOneSelRow = true;
            oDTP.bWithoutHead = true;
            let aNames = oDTP.fGetMarkArr("display_name");
            let oParentBr = undefined;
            if (oDTP.fGetSelTabLen() > 0) {
                if ((oDTP.fGetSelBranch(0).fGetData().bIsFile || oDTP.fGetSelTabLen() > 1) && oDTP.fGetSelBranch(0).pParent) {
                    oParentBr = oDTP.fGetSelBranch(0).pParent;
                }
                else {
                    oParentBr = oDTP.fGetSelBranch(0);
                }
            }
            oDTP.CreateListElements();
            domRightPanel.style.display = "";
            SetDomDisplayAndInnerHtml(this.domTree1, null, { text: null, class: ["stretch_x"] });
            if (this.domTree1) {
                this.domTree1.style.flex = "1 1 40%";
            }
            if (!oDTP.oFolderContTable) {
                oDTP.oFolderContTable = new CDynTabProp;
                const oContTab = oDTP.oFolderContTable;
                oContTab.oParentDTP = oDTP;
                oContTab.domLimWindow = domRightPanel;
                oContTab.fBackspace = bRightTree ? () => {
                    this.MoveUpstairs('R');
                } : () => {
                    this.MoveUpstairs('L');
                };
                initDropZone(oDTP);
                domRightPanel.style.flex = "1 1 100%";
                this.SetWgseDTP(oDTP, pUrl);
                oDTP.SetContTable("display_name", "display_name");
            }
            else {
                this.SetWgseDTP(oDTP, pUrl);
                delete oDTP.oFolderContTable.oParentBr;
                oDTP.oFolderContTable.nLastShowRow = -1;
                oDTP.oFolderContTable.nWindowHeight = -1;
                oDTP.oFolderContTable.nLines = -1;
                delete oDTP.oFolderContTable.rectIconLine;
            }
            if (eExplorerMode == 'detal_mode') {
                delete oDTP.oFolderContTable.sIconField;
                oDTP.oFolderContTable.CreateListElements(true);
            }
            else {
                oDTP.oFolderContTable.sIconField = "display_name";
                oDTP.oFolderContTable.CreateIconElements(true);
            }
            if (oParentBr && (bWasTreeMode || !oDTP.oFolderContTable.root.cont || oDTP.oFolderContTable.root.jsBody.length != oDTP.oFolderContTable.aDeploedTable.length)) {
                oDTP.fSelTabClean();
                await TableRowClick(oDTP, oParentBr, true);
                oParentBr.fMarkAndShow(oDTP);
                if (aNames.length > 0) {
                    oDTP.oFolderContTable.fMarkArr(aNames, "display_name");
                    oDTP.oFolderContTable.ShowMarkRowInCenter();
                    if (oDTP.oFolderContTable.fGetSelTabLen() === 1) {
                        await TableRowClick(oDTP.oFolderContTable, oDTP.oFolderContTable.fGetSelBranch(0));
                    }
                }
            }
            else {
                oDTP.oFolderContTable.oParentBr = oParentBr;
            }
        }
        else {
            this.domTree1.classList.remove("stretch_x");
            oDTP.aHead = jsFloatMenuTableHeads.ExploreTableHead;
            oDTP.bOnlyOneSelRow = false;
            oDTP.bWithoutHead = false;
            delete oDTP.bHide;
            oDTP.CreateListElements();
            if (oDTP.oFolderContTable) {
                delete oDTP.oFolderContTable.sIconField;
                if (oDTP.oFolderContTable.oParentBr) {
                    oDTP.oFolderContTable.oParentBr.folder = FOLDER_OPEN;
                    const aMarkedItem = oDTP.oFolderContTable.fGetMarkArr("display_name");
                    if (aMarkedItem.length > 0) {
                        oDTP.fSelTabClean();
                        oDTP.ShowTable();
                        oDTP.fMarkArr(aMarkedItem, "display_name", oDTP.oFolderContTable.oParentBr);
                        oDTP.ShowMarkRowInCenter();
                    }
                }
                oDTP.oFolderContTable.fClear();
            }
            if (this.domRightPanel1) {
                this.domRightPanel1.style.display = "none";
            }
            if (this.domRightPanel2) {
                this.domRightPanel2.style.display = "none";
            }
            this.SetWgseDTP(oDTP, pUrl);
            SetDomDisplayAndInnerHtml(this.domTree1, null, { text: null, delClass: ["stretch_x"] });
            if (this.domTree1) {
                this.domTree1.style.flex = "1 1";
            }
        }
        if (!oDTP.fDoMarkRowsShow()) {
            oDTP.ShowMarkRowInCenter();
        }
    }
    async MoveUpstairs(ind) {
        const oDTP = (ind == "L" ? this.DTP1 : this.DTP2);
        let oBr = undefined;
        if (oDTP.oFolderContTable && oSavedData.IsWinMode()) {
            oBr = oDTP.oFolderContTable.oParentBr;
        }
        else if (oDTP.fGetSelTabLen() > 0) {
            oBr = oDTP.fGetSelBranch(0);
        }
        if (oBr && oBr.pParent && oBr.pParent.pParent) {
            await TableRowClick(oDTP, oBr.pParent, true);
            oBr.pParent.fMarkAndShow(oDTP);
            if (oDTP.oFolderContTable && oSavedData.IsWinMode()) {
                const oData = oBr.fGetData();
                if (oData) {
                    const oElInContTable = oDTP.oFolderContTable.root.fFindEl(oData.display_name, "display_name", "none", true);
                    if (oElInContTable) {
                        oElInContTable.fMarkAndShow(oDTP.oFolderContTable);
                    }
                }
            }
        }
    }
}
document.addEventListener('DOMContentLoaded', async function () {
    sLocPathName = '/web-ui/static/WebGSExplorer.html';
    if (bItIsJobExplorer) {
        request_url = location.origin + location.pathname.replace("/static/WebGSExplorer.html", "");
    }
    else {
        request_url = location.origin;
    }
    const server = location.href.split("/session-")[0];
    if (server != oSavedData.server) {
        oSavedData.url1 = "";
        oSavedData.accKey1 = "";
        oSavedData.url2 = "";
        oSavedData.accKey2 = "";
        oSavedData.server = server;
        localStorage.setItem("oSavedData", JSON.stringify(oSavedData));
    }
    try {
        await LanguagePageDownload();
        const aFs = await SendRecivePostApiWithRetry("ListFileSystems");
        if (aFs.file_systems.length == 0) {
            ErrorMsgShow("There are no FileSystems");
            return;
        }
        ged = new CGlobExplorerData(aFs);
        await ged.ScrollToOldLines();
        const oFileOps = await SendRecivePostApiWithRetry("ListFileOps");
        const aFileOps = oFileOps.file_ops.filter(it => it.state < 2);
        for (const oAns of aFileOps) {
            let sTitle = "Title_Open";
            let sFromTo = "Str_From";
            switch (oAns.description.method_name) {
                case "CopyFiles":
                    sTitle = "Title_CopyItems";
                    sFromTo = "Str_FromTo";
                    break;
                case "MoveFiles":
                    sTitle = "Title_MoveItems";
                    sFromTo = "Str_FromTo";
                    break;
                case "RenameFile":
                    sTitle = "Title_Rename";
                    break;
                case "DeleteFiles":
                    sTitle = "Title_DeleteItem";
                    break;
            }
            await FileOpStatusPolling(null, oAns, null, oAns.description.method_name, [], null, { sDots: " ", sPrevMsg: "", nCycleCnt: 0 }, GetCurrTime(), GetLocaleText(sTitle, [oAns.items_total]), "", [], "", sFromTo, oAns.description.dst_url || "", oAns.description.src_url || "", -1, null);
        }
    }
    catch (catchErr) {
        CatchErrProcess(catchErr, "", { eGoal: "msgbox" });
    }
});
function ChangeExplorerMode(eExplorerMode) {
    if (!ged.domExplorer || ged.sPrevMode === eExplorerMode)
        return;
    ged.sPrevMode = eExplorerMode;
    if (eExplorerMode == 'detal_mode' || eExplorerMode == 'med_icon_mode') {
        const bWasTreeMode = !oSavedData.IsWinMode();
        oSavedData.eExplorerMode = eExplorerMode;
        localStorage.setItem("oSavedData", JSON.stringify(oSavedData));
        ged.SwitchMode(eExplorerMode, false, bWasTreeMode);
        document.getElementById("second_tree_" + ged.domExplorer.id).style.display = "none";
        DisableClassEl("FsOperationsBtn");
        const domFirstTree = SetDomDisplayAndInnerHtml("first_tree_" + ged.domExplorer.id, null, { text: null, delClass: ["stretch_x", "thick_right_border"] });
        if (domFirstTree) {
            domFirstTree.style.flex = "1 1 100%";
            SetStretchEl();
        }
        return;
    }
    ged.SwitchMode(eExplorerMode, false);
    if (eExplorerMode == 'two_tree') {
        SwitchExplorerTo2Tree();
    }
    else {
        SwitchExplorerTo1Tree();
    }
}
function SwitchExplorerTo1Tree() {
    if (!ged.domExplorer)
        return;
    oSavedData.eExplorerMode = 'one_tree';
    localStorage.setItem("oSavedData", JSON.stringify(oSavedData));
    document.getElementById("second_tree_" + ged.domExplorer.id).style.display = "none";
    DisableClassEl("FsOperationsBtn");
    const domFirstTree = SetDomDisplayAndInnerHtml("first_tree_" + ged.domExplorer.id, null, { text: null, delClass: ["stretch_x", "thick_right_border"] });
    if (domFirstTree) {
        domFirstTree.style.flex = "1 1 100%";
    }
    SetStretchEl();
}
function SwitchExplorerTo2Tree() {
    if (!ged.domExplorer)
        return;
    oSavedData.eExplorerMode = 'two_tree';
    localStorage.setItem("oSavedData", JSON.stringify(oSavedData));
    SetDomDisplayAndInnerHtml("first_tree_" + ged.domExplorer.id, null, { text: null, class: ["stretch_x", "thick_right_border"] });
    document.getElementById("second_tree_" + ged.domExplorer.id).style.display = "";
    EnableClassEl("FsOperationsBtn");
    AllUserResize();
    ged.DTP2.bWasStretch = false;
    ged.DTP2.SetColsWidth();
    SetStretchEl();
}
function ShowWgseViewMenu(button) {
    DeleteAllDropMenu();
    const rect = button.getBoundingClientRect();
    const jsWebExplorerView = [
        { title: "Btn_ExplorerAsWin", img: "win_tabl_mode", command: "ChangeExplorerMode('detal_mode')", selected: oSavedData.eExplorerMode == 'detal_mode' },
        { title: "Btn_ExplorerWithIcons", img: "win_icon_mode", command: "ChangeExplorerMode('med_icon_mode')", selected: oSavedData.eExplorerMode == 'med_icon_mode' },
        { title: "Explorer_1Tree", img: "one_tree", command: "ChangeExplorerMode('one_tree')", selected: oSavedData.eExplorerMode == 'one_tree' },
        { title: "Explorer_2Trees", img: "two_tree", command: "ChangeExplorerMode('two_tree')", selected: oSavedData.eExplorerMode == 'two_tree' },
    ];
    CreateDropMenuItems(jsWebExplorerView, rect.left, rect.bottom + document.documentElement.scrollTop, rect.right, button);
}
async function WinExplprerInfoRequest(oBDE) {
    if (oBDE && oBDE.Branch && oBDE.DTP && oBDE.DTP.oParentBr) {
        const oData1 = oBDE.Branch.fGetData();
        if (!oData1 || !oData1.display_name)
            return;
        const oBr = oBDE.DTP.oParentBr.fFindEl(oData1.display_name, "display_name");
        if (!oBr)
            return;
        const oData2 = oBr.fGetData();
        let sPath = "";
        if (oData2.bIsFile) {
            if (oSavedData.IsWinMode()) {
                sPath = oBDE.DTP.oParentBr.fGetData().oConn.sUrlA + oData2.fGetPathAndName(oBDE.Branch, true);
            }
            else {
                sPath = oBDE.Branch.pParent.fGetData().oConn.sUrlA + oData2.fGetPathAndName(oBDE.Branch, true);
            }
        }
        else {
            sPath = oData2.oConn.sUrlA + oData2.fGetPathAndName(oBDE.Branch, true);
        }
        try {
            const oAnswer = await GetFolderInfoWithEnc(oData2, { DTP: oBDE.DTP.oParentDTP, Branch: oBr }, true);
            if (!oAnswer)
                return;
            const sHtml = FolderAndFileInfoProcessing(oAnswer, sPath, oData2.display_name);
            HtmlMsgbox(sHtml, "Title_FolderProperties", { bNoCurrJobName: true, nWidth: 600, icon: "info" });
        }
        catch { }
    }
}
